0

My book class-based document normally has structure Chapter -> Section -> Subsection -> Subsubsection. And usually, PDF bookmarks in a PDF viewer (e.g. Adobe Reader) reflect just that. Here is one MWE with expected behaviour:

\documentclass{book}
%\usepackage[estonian]{babel}
%\usepackage{mathtools}
%\usepackage{tocloft}
%\usepackage{titlesec}
\usepackage[hypertexnames=false]{hyperref}
%\usepackage{bookmark}

\setcounter{secnumdepth}{3}
\setcounter{tocdepth}{3}

\begin{document}
    
    \tableofcontents
    
    \chapter{Test C}
    A
    \section{Test C1}
    B
    \subsection{Test C11}
    C
    \subsubsection{Test C111}
    D
    
    \include{Testing bookmarks}
    
\end{document}

where Testing bookmarks.tex is

\appendix
%\addtocontents{toc}{\protect\setcounter{tocdepth}{1}}
\chapter{Test A}
A
\section{Test A1}
B

The bookmarks are as expected:

Expected bookmark structure. Picture 1


However, when \addtocontents{toc}{\protect\setcounter{tocdepth}{1}} is added/uncommented under Testing bookmarks.tex, two of the deeper level bookmarks have disappeared.

Broken/incomplete bookmark structure. Picture 2

How can I fix this?

  • Please make sure to compile the document a suitable number of times when making changes (here, sometimes three).
  • (The MWE is indeed very minimal when compared to the original document.)

1 Answer 1

2

Apparently, adding \addtocontents{toc}{\protect\setcounter{tocdepth}{1}} seems to somehow change the default behaviour of bookmarks with hyperref.

But fixing it was (eventually) straightforward: just adding bookmarksdepth=3 to the setup works like a charm. So

\documentclass{book}
%\usepackage[estonian]{babel}
%\usepackage{mathtools}
%\usepackage{tocloft}
%\usepackage{titlesec}
\usepackage[hypertexnames=false, bookmarksdepth=3]{hyperref} % changed line
%\usepackage{bookmark}

\setcounter{secnumdepth}{3}
\setcounter{tocdepth}{3}

\begin{document}

    \tableofcontents

    \chapter{Test C}
    A
    \section{Test C1}
    B
    \subsection{Test C11}
    C
    \subsubsection{Test C111}
    D

    \include{Testing bookmarks}

\end{document}

This gives the expected bookmark structure:

Bookmark structure, with bookmarksdepth=3

You must log in to answer this question.

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