Having a # (hash-sign) in \href in a longtabu cell

I have a longtabu table (externally generated) with in one of the cells a href with an url that contains a has sign. When compiling this with pdflatex I get:

! Illegal parameter number in definition of \Hy@tempa.
c
l.18 \end{longtabu}


The MWE:

\documentclass[twoside]{book}

\usepackage{longtable}
\usepackage{tabu}
\usepackage{hyperref}

\begin{document}
\hline
{ test  }&{ URL   }\\\cline{1-2}
\hline
\endfoot
\hline
{ test  }&{ URL   }\\\cline{1-2}
test & \href{http://www.stack.nl/~dimitri/doxygen/manual/commands.html#cmdpage}{\texttt{ URL with hash}}   \\\cline{1-2}
\end{longtabu}

\end{document}


In the documentation of the hyperref package I found with the \href command in the paragraph '4 Additional user macros':

\href[options]{URL}{text} The text is made a hyperlink to the URL; this must be a full URL (relative to the base URL, if that is defined). The special characters # and ~ do not need to be escaped in any way.

So looks like some interaction between the href and longtabu.

I can overcome the problem by escaping the #-hash sign in the url:

\href{http://www.stack.nl/~dimitri/doxygen/manual/commands.html\#cmdpage}


but I'm not sure if this is the correct way to do it.

• Is my way the correct way?
• is this a wrong usage of the \href in the longtabu from my side?
• is this a bug ?
• @egreg You edited the escape with a backquote whilst I intended a backslash. Was you edit there necessary / didn't my backslash show or was this a solution ? – albert Aug 24 '18 at 8:28
• Sorry for the misprint, but it was better to make the line into a code line (indented by four spaces). And there was no backslash, but a backquote in the original. – egreg Aug 24 '18 at 8:32
• Oops was already an error at my side ( :-( ). – albert Aug 24 '18 at 8:34

If you don't want to escape #, you need to make it printable:

\documentclass[twoside]{book}

\usepackage{longtable}
\usepackage{tabu}
\usepackage{hyperref}

\begin{document}

\begingroup\catcode#=12
\hline
test & URL \\ \cline{1-2}
\hline
test & URL \\ \cline{1-2}
\hline
\endfoot
test & \href{http://www.stack.nl/~dimitri/doxygen/manual/commands.html#cmdpage}
{\texttt{ URL with hash}}
\\
\cline{1-2}
\end{longtabu}
\endgroup

\end{document}


• So either 1) use the escape or 2) make it printable. Is there ar reason that longtabu doesn't follow non-escaping rule from the \href command? – albert Aug 24 '18 at 8:36
• @albert longtabu reads the table body as the argument to a command, so the self-escaping mechanism of \href cannot work. – egreg Aug 24 '18 at 8:42
• Does this also apply to other commands / environments? – albert Aug 24 '18 at 8:45
• @albert To tabularx, for instance, or the alignment environments of amsmath`. – egreg Aug 24 '18 at 9:00