# Custom command for a hyperref to a URL with a #

I use a custom command \blink for Martin Scharrer's code to have blue underlined links in a beamer presentation. \blink uses his command \blurl in a \href command. It works, however, I cannot use my custom command \blink with a URL which contains a #. This compiles with an error:

\documentclass{beamer}

\usepackage{ulem}
\useunder{\uline}{\ulined}{}%
\DeclareUrlCommand{\blurl}{\def\UrlFont{\color{blue}\ulined}}

\begin{document}

\begin{frame}[fragile]
\end{frame}

\end{document}


Error:

! Illegal parameter number in definition of \Hy@tempa.
s
l.2 ...dtypes.html#string-methods}{string methods}


I have tried to replace the # in the URL with %23 (I escaped the % sign in latetex code). That version compiled well, but it seems that the site I'm linking to does not accept %23 instead of #, so the clickable link in the resulting document does not meet the goal.

When I don't use my custom command \blink and do it like

\href{https://docs.python.org/3.7/library/stdtypes.html#string-methods}{\blurl{href with blurl}}


then the document compiles well and the link works. How do I have to improve my \blink command so that it can handle URLs with a # and possibly other Latex control characters?

• If you escape the # as \# it seems to work fine – BambOo Sep 2 '18 at 8:44
• You may have come across a bug in the hyperref package. Bug reports are off-topic for this site. Please consider contacting the package's maintainer by email and asking him why \blink{https://docs.#.zzz}{string methods} generates an error whereas \href{https://docs.#.zzz}{\blurl{string methods}} does not. – Mico Sep 2 '18 at 11:14
• Might it be that it is a similar proble as in tex.stackexchange.com/questions/447461/…, see the answer from egreg there – albert Sep 2 '18 at 12:14

I tried to make a generic version of your code, which prompted this question: Macro char # doubles when made letter?, which in turn allowed me to give you the answer below. It basically makes the #-character a letter and then reads the link and it's description. Then it makes the # back to the macro char (by scoping)

\documentclass{beamer}
\usepackage{ulem}
\useunder{\uline}{\ulined}{}%
\DeclareUrlCommand{\blurl}{\def\UrlFont{\color{blue}\ulined}}
%% We want the #-sign only to be a letter (catcode 11) temporarily, so begin the scope group
\bgroup%
% Below, # is made a letter until \egroup is called
\catcode\#=11\relax%
}
% after the # is made a letter by the \blink macro
\href{#1}{\blurl{#2}}%
% We want the #-sign only to be a letter up til here, so we group it.
% the broup will start in the below \blink macro,
% and we wnd it here:
\egroup%
}

\begin{document}

\begin{frame}[fragile]
`