# Is it necessary to escape underscores in \href with dollar signs or \?

The tutorial LaTeX/Hyperlinks shows that the following should be compilable.

\documentclass{article}
\usepackage{hyperref}

\begin{document}

\href{mailto:a_b@c.def}{a_b@c.def}

\end{document}


However, I cannot compile it. Is it necessary to escape underscores in \href with a pair of $s or \? Note that escaping the underscores with a pair of $ does not make sense in the above context, of course!

-

You need to escape the printed text (the second argument) since that is interpreted by LaTeX. The first argument is set according to the PDF specification which doesn't follow the same rules:

\documentclass{article}
\usepackage{hyperref}

\begin{document}

\href{mailto:a_b@c.def}{a\_b@c.def}

\end{document}


You could also use \textunderscore in place of \_.

-
Should this be edited in the Wikibook the OP linked to? Or could there be a case when the code as shown there would just work? –  Jake Apr 26 '12 at 6:53
@Jake: It looks to me like a bad example, since \nolinkurl removes the requirement to escape the underscore. Perhaps if the example used my-address@wikibooks.org instead, it would be less error-prone. –  Werner Apr 26 '12 at 6:59
I really miss an \email macro by hyperref which handles this itself. –  Martin Scharrer Apr 26 '12 at 7:07