1

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: enter image description here

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.

albert
  • 1,313
  • \chapter{documentation for link to \protect\hyperlink{sec1}{Section 1}}? – moewe May 31 '18 at 11:49
  • 1
    I'd probably go for \section{Section 1 Reference}\label{sec1} instead of \hypertarget{sec1}{}\section{Section 1 Reference}. Then one could write \chapter{documentation for link to \hyperref[sec1]{Section 1}} (i.e. no \protect) because \hyperref is robust. – moewe May 31 '18 at 11:53
  • Thanks for both suggestions, I tried them both in a small program and looks like to work (I didn't doubt). In the original code there is already a \label. I have to see what the effect is on a larger project where the documentation is automatically generated, here the fix with \protect is, probably, easiest. Maybe you can make an answer of your suggestions. – albert May 31 '18 at 12:06

1 Answers1

2

I would use \labels and then \hyperref[<label>]{<text>} for custom text or maybe even cleveref's \cref.

\documentclass[twoside]{book}

\usepackage[pdftex,pagebackref=true]{hyperref}
\begin{document}
\chapter{documentation for link to \hyperref[sec1]{Section 1}}

\section{Section 1 Reference}\label{sec1}

\section{Section 2 Reference}\label{sec2}
documentation for \hyperref[sec1]{Section 1}
\end{document}

If you want to stick to \hyperlink you need to \protect it because it is a fragile command.

\chapter{documentation for link to \protect\hyperlink{sec1}{Section 1}}

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.

moewe
  • 175,683
  • I've posted a followup question: "TOC / Bookmark and hyperlink inside a chapter / section title" (https://tex.stackexchange.com/questions/434316/toc-bookmark-and-hyperlink-inside-a-chapter-section-title) – albert May 31 '18 at 13:54
  • I'd like to add more on @moewe. To avoid warnings, you can write like below: ```latex \chapter[documentation for link to Section 1] {documentation for link to \hyperref[sec1]{Section 1}}
    
    
    – Kazuki.F Jan 08 '22 at 14:49