0

This is a followup question on hyperlink inside chapter / section title in respect to the display in the TOC and bookmarks section of the resulting pdf.

\documentclass[twoside]{book}

\usepackage[pdftex,pagebackref=true]{hyperref}
\begin{document}
\tableofcontents

\chapter{documentation for link to Section 1}
\chapter{documentation for link to \mbox{\protect\hyperlink{sec1}{Section 1}}}
\chapter{documentation for link to \hyperref[sec1]{Section 1}}

\hypertarget{sec1}{}\section{Section 1 Reference}
\label{sec1}\index{ref1@{ref1}}

\hypertarget{sec2}{}\section{Section 2 Reference}
documentation for \hyperlink{sec1}{Section 1}

\end{document}

This results in the contents page: enter image description here

and a Bookmark section:

enter image description here

In the contents page we see for chapter 2 and 3 (\protect and \hyperref implementation) an extra vertical red bar before the "Section 1" text, this is a an extra hyperref when clicking on "documentation for link" we jump to the relevant chapter, but wen clicking on the "Section 1" part we jump to the subsection. I think that in the the TOC part it is not good that depending on where one clicks on the line a different jump is made.

Looking at the bookmarks section we see here for the chapters 2 and 3 the texts "sec1Section" and "[sec1]Section" so with extra "sec1" and "[sec1]".

How can I get a "clean" TOC / Bookmarks part?

  • Have a look at tex.stackexchange.com/q/227086/35864 – moewe May 31 '18 at 13:59
  • is there a reason you can't use the optional "short title" for these titles, e.g., \chapter[documentation for link to Section 1]{<full title>} ? – barbara beeton May 31 '18 at 14:18
  • @barbarabeeton Thanks for the suggestion, very interesting possibility, I have to investigate but first test looks like to work for me as well. – albert May 31 '18 at 14:54
  • the "short title" option is the canonical mechanism for specifying an entry for the table of contents and running head, and that should be the first thing tried for the book class. but some document classes do things differently, and it's worth knowing that that's the case. – barbara beeton May 31 '18 at 15:56
3

\texorpdfstring is your friend to put the TeX stuff in its first argument and the stuff that belongs to the PDF bookmark to the second:

\documentclass[twoside]{book}

\usepackage{hyperref}
\begin{document}
\tableofcontents

\chapter{documentation for link to Section 1}
\chapter{documentation for link to
  \texorpdfstring{\hyperref[sec1]{Section \ref*{sec1}}}{Section \ref{sec1}}}

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

\section{Section 2 Reference}
documentation for \autoref{sec1}

\end{document}

The table of contents is an example, why links in chapter/section titles should be avoided. The nested link "Section 1" makes it unclear, if the target should go to the outer link, the chapter/section title or to the link target to the inner link.

Workaround A: Option linktocpage of package hyperref to make the page numbers instead of the chapter/section titles to links.

Workaround B:

\documentclass[twoside]{book}

\usepackage[pagebackref=true]{hyperref}

\newif\ifInToc
\DeclareRobustCommand*{\notoclink}[1]{%
  \ifInToc
    \begin{NoHyper}#1\end{NoHyper}%
  \else
    #1%
  \fi
}

\begin{document}

\InToctrue
\tableofcontents
\InTocfalse

\chapter{documentation for link to Section 1}
\chapter{documentation for link to
  \texorpdfstring{%
    \notoclink{\hyperref[sec1]{Section \ref*{sec1}}}%
  }{Section \ref{sec1}}}

\section{Section 1 Reference}
\label{sec1}\index{ref1@{ref1}}

\section{Section 2 Reference}
documentation for \autoref{sec1}

\end{document}
  • Thanks, this solves the Bookmark part, but in the TOC I still have that extra link. – albert May 31 '18 at 14:10
  • As I need fixed texts as labels, although the MWE maybe did suggest a little bit different (sorry), but still very valuable for future reference, I think Workaround B is the way to go for me. – albert May 31 '18 at 14:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.