14

Following the previous questions on this topic, when you produce a website in LaTeX what is the best way to produce a url that contains a tilde? \verb produces the upper tilde that does not read well, and $\sim$ does not copy/pase well (adding a space when I do it). Solutions?

It seems like this should be one of those things that has a very easy fix... if it doesn't, why not?

Community
  • 1
  • 1
physicsmichael
  • 4,793
  • 11
  • 35
  • 54
  • What exactly do you mean by "produce a website in LaTeX"? If you are using some sort of LaTeX to HTML translator, which one, and have you looked at its documentation? – Jouni K. Seppänen Apr 05 '09 at 09:58

9 Answers9

13

I'd look at the url package.

Jon 'links in bio' Ericson
  • 20,880
  • 12
  • 98
  • 148
8

I know this is an old question, but I recently came up with something that, despite a severe lack of elegance, works beautifully.

\catcode`~=11 % make LaTeX treat tilde (~) like a normal character
\newcommand{\urltilde}{\kern -.15em\lower .7ex\hbox{~}\kern .04em}
\catcode`~=13 % revert back to treating tilde (~) as an active character

Now you can use \urltilde inside of a \url tag (even in a .bib file) and: 1) the URL will render perfectly; 2) clicking on the URL will take you to the correct address; and, 3) copy-paste will put the correct address in the clipboard.

This is the only solution I have found that satisfies all three of these requirements. I hope it helps somebody out there.

Ryan
  • 81
  • 1
  • 1
  • 2
    Oops. Actually this does not work with the standard url.sty. However, this (even uglier) hack does: \catcode`~=11 \def\UrlSpecials{\do\~{\kern -.15em\lower .7ex\hbox{~}\kern .04em}} \catcode`~=13 % paste this immediately *after* you include url.sty, and you can just put tildes right in your URL without any special commands. – Ryan Oct 11 '10 at 13:57
  • I replaced \hbox{~} with \hbox{\texttt{~}} to get a tilde that copied and pasted correctly. I used \urltilde inside an \href command (on Mac OS X to Safari using default fonts on a texlive distribution of latex ) – Alec Jacobson Feb 08 '13 at 14:55
4

url package didn't work for me. hyperref does the job.

\usepackage{hyperref}
\url{http://website.com/~username/some_stuff/}
Mohammad Moghimi
  • 4,636
  • 14
  • 50
  • 76
  • Had to resort to this after reading all the previous comments. Copying the link adds a space before and after the ~, but its still better than all the previous options. – ias Jun 11 '19 at 02:59
3

I think it is better to use URL encoding in such a case (see, e.g., http://www.blooberry.com/indexdot/html/topics/urlencoding.htm).

It means replacing the tilde in the link with %7E.

Maybe it does not look so good in the final document (readers will see %7E instead of the tilde), but at least the copy-paste functionality works for sure, which I think is the most important thing.

For instance, for the link www.example.com/~someuser/somepage.htm I use the following code:

{\tt http://www.example.com/\%7Esomeuser/somepage.htm}

PS: The same applies for all links with white spaces or any other special characters.

Simon
  • 31
  • 1
2

I want to suggest using %7e

\tt{http://example.com/\%7etest}

tt is for making it monospace.

It looks a bit different, but it allows copy-and-paste.

Naoto Usuyama
  • 845
  • 1
  • 7
  • 13
2

I think $_{\widetilde{~}}$ works good for the tilde issue.

Diego Medaglia
  • 253
  • 2
  • 9
1

After wasting a lot of time on a related problem with LaTeXing a tilde, I thought I should record my results here in case it is a help to anyone else.

tldr: To avoid some of the difficulties of typesetting a proper tilde ~ character, I recommend adding

 \usepackage[T1]{fontenc}

in the preamble of your latex file to get more modern versions of font encoding. On modern TeX installations, this automatically loads the T1-encoded version of Knuth's Computer Modern fonts.


Details:

As has been noted by previous posters, using standard pdflatex/latex with the default fonts (the original OT1-encoded Computer Modern), there are difficulties in trying to render a regular tilde character, ~, and there are various workarounds with varying degrees of satisfaction. One workaround is to use the textasciitilde command. For example, you can use it to put a tilde in a URL, like:

 https://w3.pppl.gov/\textasciitilde{}hammett

This gives a raised tilde (the kind of tilde intended for accents over another character), and methods to lower it to a more normal tilde are discussed by other posters. While this works as expected if the resulting PDF file is viewed in Adobe Acrobat, a downside is that if the PDF is viewed with the built-in Preview app on a Mac (or if viewed in TeXShop's previewer, which uses the same Mac libraries for rendering), then this URL link visually looks correct as https://w3.pppl.gov/~hammett, but if you actually click on it, it gets interpreted only as

 https://w3.pppl.gov/

If you try to cut and paste the whole URL from Preview into a browser, the tilde in the pdf is replaced with a blank

 https://w3.pppl.gov/%20hammett

so it doesn't work. (If you paste into some browsers, there are other special characters added also). (The \url{...} command will display a URL with tildes correctly, but forces it to use a fixed-space terminal font, and there are times when you want to use a tilde somewhere besides in a URL.)

Investigating further, I learned that the character that latex puts into the pdf file is not a regular tilde character but a "Combining Tilde"

  COMBINING TILDE Unicode: U+0303, UTF-8: CC 83

(previously known as a "non-spacing tilde", indicating its usage for accents over another character) and that is what Mac's Preview renders. This means that cut-and-paste from Mac's Preview into a browser fails.

However, Adobe Acrobat somehow implemented a workaround and when displaying the pdf converts this into a regular "Tilde"

  TILDE Unicode: U+007E, UTF-8: 7E

so cut-and-paste of a URL with a tilde from an Adobe-displayed pdf file into a web browser works fine.

(I determined these unicode values by copying the rendered tilde character from the TeXShop Preview window and pasting it into the Character Viewer app in the menu bar on a Mac. Then right-click on the character to select "Copy Character Info". The Character Viewer app can be turned on in Mac Preferences, Keyboard, and select "Show keyboard and emoji viewers in menu bar".)

This problem goes away if you use T1-encoded fonts by adding

\usepackage[T1]{fontenc}

to your latex file preamble. Furthermore, if you use the Adobe Times-like font

\usepackage{newtxtext}

then tilde is rendered at mid height automatically and doesn't need to be lowered. (The above command uses the newtx font only for regular text, while leaving the math fonts unchanged. I like newtxtext because it is slightly darker than CM. But for math I prefer to keep Knuth's traditional Computer Modern CM font, because the newtxmath font, like other Times math fonts, renders math italic v in way that is confusingly like a Greek nu.)

P.S.: For more info on why it's always good to use the fontenc package for a more modern approach than the 1970's original encoding, see (1) https://www.texfaq.org/FAQ-why-inp-font, (2) https://tex.stackexchange.com/questions/664/why-should-i-use-usepackaget1fontenc, and (3) https://en.wikibooks.org/wiki/LaTeX/Fonts.

0

\symbol{126} would be another way, but in the default font it also yields a superscripted tilde. An ugly hack (but what isn't in LaTeX) would be to use

${}_{\textrm{\symbol{126}}}$

which produces a text tilde in Math mode and subscripts it. So it appears in the middle of the line. Seems to work for a clickable link as well. You can always put that into a command on its own :)

Joey
  • 344,408
  • 85
  • 689
  • 683
0

I'm not a latex user admittedly, but does this page help?

http://www.cse.wustl.edu/~mgeorg/html/tildalatex.html

They do the following:

\def\urltilda{\kern -.15em\lower .7ex\hbox{\~{}}\kern .04em}
\def\urldot{\kern -.10em.\kern -.10em}
\def\urlhttp{http\kern -.10em\lower -.1ex\hbox{:}\kern -.12em\lower 0ex\hbox{/}\kern -.18em\lower 0ex\hbox{/}}

The way this is used is

{\tt mgeorg@cse\urldot wustl\urldot edu}
{\tt \urlhttp www\urldot cse\urldot wustl\urldot edu/\urltilda mgeorg}
Jeremy Stanley
  • 5,896
  • 1
  • 29
  • 21