I would like to have the possibility to have a hyperlink inside a chapter / section title.
MWE:
\documentclass[twoside]{book}
\usepackage[pdftex,pagebackref=true]{hyperref}
\begin{document}
\chapter{documentation for link to Section 1}
%%\chapter{documentation for link to \hyperlink{sec1}{Section 1}}
\hypertarget{sec1}{}\section{Section 1 Reference}
\hypertarget{sec2}{}\section{Section 2 Reference}
documentation for \hyperlink{sec1}{Section 1}
\end{document}
This results in the following output:
We see in section 1.2 the hyperlink
to section 1.1, but I want to have the link also in the chapter title. When adding a second chapter with a hyperlink
(remove %% ) I get the error message:
Chapter 1.
Package hyperref Warning: Token not allowed in a PDF string (PDFDocEncoding):
(hyperref) removing `\<let>-command' on input line 6.
Package hyperref Warning: Token not allowed in a PDF string (PDFDocEncoding):
(hyperref) removing `\Hy@reserved@a' on input line 6.
Package hyperref Warning: Token not allowed in a PDF string (PDFDocEncoding):
(hyperref) removing `\@ifnextchar' on input line 6.
! Undefined control sequence.
\hyper@@link ->\let \Hy@reserved@a
\relax \@ifnextchar [{\hyper@link@ }{\hyp...
l.6 ...on for link to \hyperlink{sec1}{Section 1}}
?
! Emergency stop.
\hyper@@link ->\let \Hy@reserved@a
\relax \@ifnextchar [{\hyper@link@ }{\hyp...
l.6 ...on for link to \hyperlink{sec1}{Section 1}}
! ==> Fatal error occurred, no output PDF file produced!
Question:
How to get the hyperlink in the chapter (/section) title.
Best Answer
I would use
\label
s and then\hyperref[<label>]{<text>}
for custom text or maybe evencleveref
's\cref
.If you want to stick to
\hyperlink
you need to\protect
it because it is a fragile command.You didn't need to protect
\hyperref
in the example above because it is a robust command. See What is the difference between Fragile and Robust commands? for an in-depth explanation of fragile vs. robust commands.