# Pound sign # in \href link in beamer's frame environment

While there's normally no problem with having the pound sign # in an \href link (see Hyperlink with # (number sign)), it does not work within a frame environment in beamer.

\documentclass{beamer}

\usepackage{hyperref}

\begin{document}
\begin{frame}
\href{http://de.wikipedia.org/wiki/Hybridorbital#Beispiele}{Wiki}
\end{frame}
\end{document}


This results in the error ! Illegal parameter number in definition of \iterate.

Remove the frame environment, though, and it compiles fine. So my question is, how can I include an \href link with the symbol # in a frame environment in beamer?

EDIT: A comment suggests to add the option [fragile] to the frame environment. While that takes care of the MWE above, it won't fix the fact the MWE below also crashes because of the pound sign # in the \href link:

\documentclass{beamer}

\usepackage{hyperref}

\begin{document}
\begin{frame}[fragile]
\only<1>{\href{http://de.wikipedia.org/wiki/Hybridorbital#Beispiele}}{Wiki}
\end{frame}
\end{document}


This gives the error ! Illegal parameter number in definition of \beamer@doifinframe. Remove the pound sign # from the link, though, and it compiles without error.

• Frames with verbatim content (\href is sort of verbatim) require the fragile option: \begin{frame}[fragile]. – Phelype Oleinik Aug 16 '19 at 12:36
• @PhelypeOleinik Add \only<1>{} to the frame content, and it breaks again, even with [fragile]. – Sverre Aug 16 '19 at 12:48
• Only if you do \only<1>{\href{...}}. As with all verbatim commands, \href usually won't work as argument to another command. – Phelype Oleinik Aug 16 '19 at 12:51
• @PhelypeOleinik It works without the pound sign #, so it's not the \href itself which is the problem here. It's its combination with #. – Sverre Aug 16 '19 at 12:53
• without the # it works as \href doesn't have to handle a special char, as soon as you use special chars you can't use \href in the argument of another command. Use an environment instead of \only. – Ulrike Fischer Aug 16 '19 at 13:00

beamer scans the entire frame environment before actually typesetting anything, and this leads to problems with verbatim content, because it cannot be "seen" before it is actually being typeset, or the tokenization will happen too early and it will not work.

Essentially, what happens in your first example is:

\def\iterate{Hybridorbital#Beispiele}


which throws the error:

! Illegal parameter number in definition of \iterate.
B
l.4 \def\iterate{Hybridorbital#B


The next example shows the same issue, except that the problem doesn't happen while beamer is scanning the frame, but when it grabs the argument of \only. As a rule of thumb, con't use verbatim commands as argument to other commands.

To work around the issue, you can escape the #. The documentation of \href says:

So this works (because \# is not the "special" character #):

\documentclass{beamer}
\usepackage{hyperref}
\begin{document}
\begin{frame}
\only<1>{\href{http://de.wikipedia.org/wiki/Hybridorbital\#Beispiele}{Wiki}}
\end{frame}
\end{document}

• I thought the escape character \ was only for the text argument, not the url argument, and that putting \ in the url would be interpreted literally. Good to know! – Sverre Aug 16 '19 at 13:11
• @Sverre TeX in general is tricky: everything can have a different meaning in a different context. \href makes sure that both # and \# mean the character #. – Phelype Oleinik Aug 16 '19 at 13:20