4

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}
% Colored links https://tex.stackexchange.com/questions/23208/i-cannot-get-a-properly-underlined-hyperlink-in-blue
\useunder{\uline}{\ulined}{}%
\DeclareUrlCommand{\blurl}{\def\UrlFont{\color{blue}\ulined}}
\providecommand{\blink}[2]{\href{#1}{\blurl{#2}}}

\begin{document}

\begin{frame}[fragile]
    \frametitle{Nasty Link}
    \blink{https://docs.python.org/3.7/library/stdtypes.html#string-methods}{string methods}
\end{frame}

\end{document}

Error:

! Illegal parameter number in definition of \Hy@tempa.
<to be read again> 
                   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?

3
  • 2
    If you escape the # as \# it seems to work fine
    – BambOo
    Sep 2, 2018 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, 2018 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, 2018 at 12:14

1 Answer 1

3

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}
% Colored links https://tex.stackexchange.com/questions/23208/i-cannot-get-a-properly-underlined-hyperlink-in-blue
\useunder{\uline}{\ulined}{}%
\DeclareUrlCommand{\blurl}{\def\UrlFont{\color{blue}\ulined}}
\def\blink{%
  %% 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%
  % Trigger the URL-reader
  \makeblinkurl%
}
\def\makeblinkurl#1#2{%
  % This will read the link and link description
  % 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]
    \frametitle{Nice Link}
    \blink{https://docs.python.org/3.7/library/stdtypes.html#string-methods}{string methods}
\end{frame}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .