# $\mathbb{Z}$ as argument of hyperlink fails

$\mathbb{Z}$ (from amsfonts) causes an error as first argument in \hyperlink{}{}, but not as second argument:

\documentclass{book}
\usepackage{amsfonts}

\usepackage[hyperindex]{hyperref}

\begin{document}
Works: Integers \protect\hyperlink{link:$Z$}{$Z$}

Works: Integers \protect\hyperlink{link:$Z$}{$\mathbb{Z}$}

%Fails: Integers \protect\hyperlink{integers $\mathbb{Z}$}{$Z$}

\end{document}


gives an error message when I uncomment the next-to-last line: Undefined control sequence. \GenericError ... #4 \erhelp \@err@... How can I avoid this?

This issue arose while writing a macro that links keywords from the main text into the index, using the keyword as both first and second arguments of a hyperlink. I'd like this to work no matter what the keyword is.

• the first argument is a name like \label it shouldn't have any formatting so math or \mathbb or anything. – David Carlisle May 11 '18 at 17:39
• Real quick: There's nothing unusual about \mathbb not "working" in the first argument of \hyperlink: \mathbf, \mathsf, \mathfrak, \mathcal, etc in the first argument of \hyperlink don't "work" either... – Mico May 11 '18 at 17:40
• if you really want to you could use \hyperlink{\detokenize{integers $\mathbb{Z}$}}{integers $\mathbb{Z}$} but it seems like a misuse. – David Carlisle May 11 '18 at 17:41
• Thanks for the clarification! The workaround with \detokenize did work for the problem posed; but when I then made a NewDocumentCommand containing that \hyperlink line, I ran into problems again - from which I concluded that I should heed your advice and avoid \mathbb inside a hyperlabel altogether. – jvd May 11 '18 at 21:53

You wrote,

$\mathbb{Z}$ (from amsfonts) causes an error [when used] as first argument in \hyperlink{}{}. ... This issue arose while writing a macro that links keywords from the main text into the index, using the keyword as both first and second arguments of a hyperlink. I'd like this to work no matter what the keyword is.

I think the following candidate solution may raise further problems down the line,[*] but "robustifying" the macro name used in the first argument of \hyperlink (and \hypertarget...) -- be it \mathbb or, say, \mathcal -- seems gets the job done. More generally, if a "keyword" isn't "robust" to begin with, make it robust by running \robustify on it.

A general comment: This approach would appear to be a second-best (or even third-best!) solution. Far better would be not to use any macros or any "active" characters (such as $) in the first argument of \hyperlink and \hypertarget. [*] TeXperts will probably point out good reasons for not using \robustify the way it's employed here... Here's a full MWE. \hypertarget sets the "targets", and \hyperlink provides "links" to the targets established by \hypertarget. \documentclass{article} \usepackage{amssymb} % for '\mathbb' \usepackage[hyperindex]{hyperref} \hypersetup{linktocpage=true,colorlinks} \usepackage{etoolbox} % for '\robustify' macro \robustify{\mathbb} \robustify{\mathcal} \begin{document} \hypertarget{$Z$}{Target:$Z$} \clearpage \hypertarget{$\mathbb{Z}$}{Target:$\mathbb{Z}$} \clearpage \hypertarget{$\mathcal{Z}$}{Target:$\mathcal{Z}$} \clearpage \hyperlink{$Z$}{link to$Z$} \hyperlink{$\mathbb{Z}$}{link to$\mathbb{Z}$} \hyperlink{$\mathcal{Z}$}{link to$\mathcal{Z}\$}
\end{document}

• @Mico: thanks for the robustify tip, \robustify{\mathbb} did solve the stated problem. But mysteriously it did not work for \robustify{\mathbbm} from the bbm family. – jvd May 11 '18 at 21:55
• @DavidCarlisle: yes, the links in Micos code do work. – jvd May 11 '18 at 21:55
• @DavidCarlisle - My first MWE was lacking the corresponding \hypertarget statements. That's fixed in the updated MWE. – Mico May 11 '18 at 22:50
• The first argument to \hypertarget and \hyperlink should be a label valid in a URL. Maybe those links work, but are not portable. – egreg May 11 '18 at 22:58
• @egreg - I fully agree with your view that the first argument of \hypertarget and \hyperlink should satisfy the requirements of \label. Note that I'm only claiming that the \robustify trick "works" for \hyperlink and \hypertarget -- I definitely wouldn't recommend actually using this trick... – Mico May 12 '18 at 0:29