I'm having a similar problem as described here: How to use \newcommand for \href?
However, I want to specify an URL with an # sign in it. I know that I should escape the # by \#, but as we have many links containing the #, it may be nice to be able to copy and paste the URL directly into the LaTeX code.
My current solution looks like this:
\documentclass{article}
\usepackage{hyperref}
\usepackage{ifthen}
\newcommand*{\myhref}[2][]{\href{http://myserver/#2}%
{\ifthenelse{\equal{#1}{}}{#2}{#1}}}
\begin{document}
\myhref{somewhere} % WORKS
\myhref[LINK TEXT]{somewhere} % WORKS
\myhref{somewhere\#anchor} % WORKS
\myhref[LINK TEXT]{somewhere\#anchor} % WORKS
\myhref{somewhere/#anchor} % WORKS NOT - BUT I WANT IT TO WORK !!!
\myhref[LINK TEXT]{somewhere#anchor} % WORKS NOT - BUT I WANT IT TO WORK !!!
\end{document}
Is there a way to automatically escape the # character in the argument to \myhref?
\catcode'\#=\somenumbera bit but I think I have tried to make#a letter (catcode 11). – Wolfgang Ulmer Nov 18 '11 at 17:26\#anchorwhich would become a unique control sequence. – egreg Nov 18 '11 at 17:37