[Tex/LaTex] Different hierarchical levels in TOC and hyperref-generated PDF bookmarks

bookmarkshyperrefpdftable of contents

I would like to make hyperref generate bookmarks for all available hierarchical levels of my documents, but only in the produced PDF. For the TOC itself (i.e. the one printed at the beginning of the document), however, I would like to display only the first 2 or 3 levels, to make it more compact.

But from what I understand, the only way to change the depth in the PDF bookmarks is to change that of the TOC, using the command \setcounter{tocdepth}{x}.

Is there an alternate way of controlling this depth, which would allow using a different value for the TOC and for the PDF bookmarks?


Edit: Here's a minimal example. By default, the 3 hierarchical levels are displayed in both the TOC and PDF bookmarks. By uncommenting the 4th line, one can limit the depth to the 2nd level, but this is applied to both the TOC and PDF bookmarks. I would like it only to apply to the TOC, and keep the 3rd level in the PDF bookmarks.

\documentclass{article}
\usepackage{hyperref}

\begin{document}

%\setcounter{tocdepth}{2}
\tableofcontents

\section{This should be in both the TOC and bookmarks}
\subsection{This too}
\subsubsection{But this should appear only in the bookmarks}
\subsubsection{and this also}

\end{document}

Best Answer

You can control the bookmark depth just like you can the ToC depth using the bookmarksdepth key-value:

\usepackage{hyperref}% http://ctan.org/pkg/hyperref
\hypersetup{bookmarksdepth=3}

enter image description here

bookmark also adapts this so you can use

\usepackage{bookmark}% http://ctan.org/pkg/bookmark
\bookmarksetup{depth=3}