[Tex/LaTex] How to include a # sign in a url using the \endnotes package

charactersendnotesurls

I am trying to include a URL in my Latex document which contains a pound sign, #, in it. If I run it without escaping the # sign, I get an error. If I escape it, then the escape character is printed out in the URL also. ("\#" instead of "#") Any ideas on how to fix this?

\documentclass[12pt, a4paper, twoside]{article}
\usepackage[T1]{fontenc}
\usepackage{endnotes}
\usepackage{url}
\begin{document}
Hello World\endnote{\url{http://www.rossettiarchive.org/docs/nb0005.duke.rad.html#0.1.19}}
\end{document}

Best Answer

As you noticed, the # confuses TeX, because the endnotes are eventually read in as a macro replacement text.

Use \urldef for the problematic entry:

\documentclass[12pt, a4paper, twoside]{article}
\usepackage[T1]{fontenc}
\usepackage{endnotes}
\usepackage{url}
\urldef\rossetti\url|http://www.rossettiarchive.org/docs/nb0005.duke.rad.html#0.1.19|


\begin{document}
Hello World\endnote{\rossetti}
\end{document}

Alternative way: put this in the preamble

\newcommand\specialendnote{\begingroup\catcode`\#=12 \specialendnoteaux}
\newcommand\specialendnoteaux[1]{\endnote{#1}\endgroup}

and call

\specialendnote{\url{http://www.rossettiarchive.org/docs/nb0005.duke.rad.html#0.1.19}}
Related Question