31

I have a document with a (logical) section that does not have a standard heading (i.e., no \section or whatever), and I would like it to appear in the table of contents and in the PDF bookmarks. I can almost do it by using \addcontentsline: It is detected by hyperref, but the bookmark that it generates is broken. Clicking on it takes me to the previous bookmark.

I know how to generate a working bookmark with \pdfbookmark[1]{Section without standard heading}{uniqueID}, but I also want to see the section in the TOC. (Naturally, using both commands gives me two bookmarks, one of them broken.)

The problem is unchanged when I set hypertexnames=false (which is supposed to generate unique identifiers). Here is a complete example:

\documentclass{article}

\usepackage[bookmarks,hypertexnames=false,debug]{hyperref}

\begin{document}
\section{First page}

\newpage
\textbf{Text on page 2}

\addcontentsline{toc}{section}{Section without standard heading} % broken bookmark
% This will work, but I still need the TOC
%\pdfbookmark[1]{Section without standard heading}{page2}
Second page text

\newpage
\section{Third section}

\end{document}
2
  • 3
    Add an target for the link with \phantomsection Commented May 27, 2015 at 8:09
  • That did it, thanks! I'll accept Heiko's answer, unless you want to add your own...
    – alexis
    Commented May 27, 2015 at 9:38

1 Answer 1

23

You need an anchor for \addcontentsline. The anchor is usually set by a section command. This allows the bookmark by \addcontentsline to point to the section title and not to the place, where \addcontentsline is issued.

An anchor can be set by \phantomsection. If the bookmark should point to the bold text on page 2, then put \phantomsection right after \newpage:

\documentclass{article}

\usepackage[bookmarks,hypertexnames=false,debug]{hyperref}
\usepackage{bookmark}

\begin{document}
\section{First page}

\newpage
\phantomsection
\textbf{Text on page 2}

\addcontentsline{toc}{section}{Section without standard heading}
Second page text

\newpage
\section{Third section}

\end{document}

I have added package bookmark for faster updated bookmarks.

5
  • Out of curiosity: Would you know why hyperref doesn't do this automatically? I.e. hook into \addtocontents (or lower-level equivalent), rather than \section et al. directly. Is it an oversight/design flaw or are there good reasons?
    – alexis
    Commented May 27, 2015 at 17:33
  • @alexis It does not hook into \addtocontents, but \addcontentsline. This is the markup command for adding entries to the table of contents. Bookmarks of the PDF format are a similar feature semantically. Commented May 27, 2015 at 17:48
  • 1
    Oops, \addcontentsline is what I meant (and what I used in my question). Hyperref makes bookmarks from them, but it does not create corresponding destinations as it does with \section etc. Is this for a good reason?
    – alexis
    Commented May 27, 2015 at 22:43
  • 2
    @alexis Often \addcontentsline is added after the section command. If \addcontentsline sets an anchor, then the bookmark would point after the section title. Therefore no anchor is set and the latest current anchor is used. If \addcontentsline is used before the section title, then it is easy to add \phantomsection. Commented May 27, 2015 at 22:54
  • I would like to add that the command \phantomsection can also be used to add Chapters to TOC in the same way. One just does \addcontentsline{toc}{chapter}{....}
    – Rauni
    Commented May 17, 2017 at 7:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .