# text display on hyperref package

I have following code in overleaf.com latex project

%document class
\usepackage[unicode]{hyperref}
\begin{document}
\href{http://www.cs.unc.edu/~tracker/media/pdf/SIGGRAPH2001_CoursePack_08.pdf}
{http://www.cs.unc.edu/~tracker/media/pdf/SIGGRAPH2001_CoursePack_08.pdf}
\end{document}


I am aware that the format is \href{link}{text display} but the text display part doesn't take in special characters. I have also tried

\href{http://www.cs.unc.edu/~tracker/media/pdf/SIGGRAPH2001_CoursePack_08.pdf}
{\url{http://www.cs.unc.edu/~tracker/media/pdf/SIGGRAPH2001_CoursePack_08.pdf}}


but I got invalid url. Furthermore, I tried

\href[http://www.cs.unc.edu/~tracker/media/pdf/SIGGRAPH2001_CoursePack_08.pdf]
{http://www.cs.unc.edu/~tracker/media/pdf/SIGGRAPH2001_CoursePack_08.pdf}


What should I try? I want to display exact weblink in my .pdf output. Thanks for your help in advance.

• Why don't you use simply \url{Your link}? – Ulrike Fischer Apr 13 at 7:05
• As Ulrike says, the example looks like what you actually want is \url. (If you don't like the fact that \url typesets in monospace font, try adding \urlstyle{same} to the preamble.) But if your real-world use case has different links and link texts, then some other tricks may be needed. – moewe Apr 13 at 7:29
• If typing \href{<URL>}{\url{<URL>}} is causing an "invalid URL" error message, it could be because you didn't load the fontenc package with the option T1 and didn't load the xurl (or url) package. FWIW, I encounter no problems when compiling \href{<URL>}{\url{<URL>}}. – Mico Apr 13 at 8:09

Both the ~ (tilde) and _ (underscore) characters present in the second argument of \href are causing trouble. In fact, any TeX-special character -- say, & or #, in the URL string will cause trouble.

One way to address this trouble is to encase the second argument of \href in a \detokenize "wrapper". This removes the TeX-special meanings of all characters in the argument of \detokenize.

A better way is to recognize that since the two arguments of \href are identical, one can replace the two-argument \href directive with a single-argument \url directive.

A significant added benefit of the "better way" is available if you load the xurl package, which permits easy line-breaking of long URL strings encased in \url{...}. Importantly, there is no guarantee that LaTeX will find satisfactory line breaks if \href{<URL string>}{\detokenize{<URL string>} is employed.

This point is illustrated in the following screenshot, which shows the outputs of \href{...}{\detokenize{...}} and \url{...}. (The black vertical lines denote the edges of the text block -- observe that the first URL string protrudes into the right-hand margin.)

\documentclass{article}
\usepackage[T1]{fontenc} % needed to render '_' characters
\usepackage{showframe}
\usepackage{xurl}
\urlstyle{same}
\begin{document}

\href{http://www.cs.unc.edu/~tracker/media/pdf/SIGGRAPH2001_CoursePack_08.pdf}
{\detokenize{http://www.cs.unc.edu/~tracker/media/pdf/SIGGRAPH2001_CoursePack_08.pdf}}

\url{http://www.cs.unc.edu/~tracker/media/pdf/SIGGRAPH2001_CoursePack_08.pdf}

\end{document}

• You can use \nolinkurl in the argument. – Ulrike Fischer Apr 13 at 7:46
• @UlrikeFischer - Indeed, \nolinkurl "works" -- in the sense that the _ and ~ characters don't cause trouble. It's my impression, however, that the main purpose of \nolinkurl is to avoid making its argument (presumably a URL string) into a hyperlink. This does not happen if one writes \href{<URL>}{\nolinkurl{<URL>}}, i.e., the URL string is once more made into a hyperlink even though the second argument is encased in \nolinkurl. – Mico Apr 13 at 7:56
• \nolinkurl doesn't generate a link on its own (but it does not actively suppress surrounding links), its main purpose is "looking and working like \url without generating a link". I agree that it doesn't make a lot of sense in \href{<URL>}{\nolinkurl{<URL>}} because one could just use \url, but it makes perfect sense if link address and typeset text differ (for example because you drop bits of the URL): \href{http://www.cs.unc.edu/~tracker/media/pdf/SIGGRAPH2001_CoursePack_08.pdf}{\nolinkurl{.../~tracker/media/pdf/SIGGRAPH2001_CoursePack_08.pdf}} – moewe Apr 13 at 8:03

First of all please post a working example. Posting snippets of code makes it difficult for users to debug. I think the error is the '_' in your text display.

Just correct it as by adding '\' before the '_' underscore in the text display argument and it works fine.

\documentclass{article}
\usepackage{standalone}
\usepackage[unicode]{hyperref}
\begin{document}
\href{http://www.cs.unc.edu/~tracker/media/pdf/SIGGRAPH2001_CoursePack_08.pdf}
{http://www.cs.unc.edu/~tracker/media/pdf/SIGGRAPH2001\_CoursePack\_08.pdf}
\end{document}


• It's not just the underscore character that's causing a problem: The ~ character isn't rendered correctly either. – Mico Apr 13 at 7:22
• Yes you are technically right!. But I just checked and it didn't matter as the link works fine and links to a document which I am able to download :) @stochastic learner....does your link open a .pdf document about "An introduction to Kalman filter" – elprofessor Apr 13 at 7:30
• I disagree with the claim that the point I made about the ~ character not being rendered correctly doesn't matter. Sooner or later, a reader may attempt to copy and paste the URL string from the pdf file they're reading into a LaTeX file of their own; they'll get mighty frustrated if tilde characters have been replaced with whitespace. Tracking down the source of the problem will be particularly difficult if the whitespace coincides with an end of line instance. – Mico Apr 13 at 7:36
• I agree.The solution you provided is more appropriate and failsafe :) – elprofessor Apr 13 at 7:39
• thank you everyone for your helpful comments. I resolved using \url{} after importing xurl package. @elprofessor , it is supposed to take me to that page but it just shows me "IP not found" when I click on the link in .pdf but I am happy with an exact link that I have in my output . – stochastic learner Apr 13 at 8:39