# Don't print headings below a specified level but keep them in PDF document outline

I have a document in which I have a fairly deep hierarchy of /section, /subsection, /subsubsection, /paragraph, etc that I have been using to help organize the writing. For the final output, I only want headings for sections and subsections to appear in the printed output and table of contents, but I want to retain all headings in the PDF document outline.

I know how to suppress sections from the table of contents, using \setcounter{tocdepth}{2}, however this also removes the headings from the PDF document outline, and doesn't suppress any headings.

For example:

\documentclass{article}
\usepackage[bookmarks]{hyperref} %pdf index
\usepackage{blindtext}
\setcounter{tocdepth}{2}
\begin{document}
\tableofcontents
\section{section}
\subsection{subsection}
\subsubsection{subsubsection}
\paragraph{paragraph}
\subparagraph{subparagraph}
\blindtext[1]
\end{document}


This outputs a document like this:

# Contents

1. section
1. subsection

# 1. section

## 1.1 subsection

### 1.1.1 subsubsection

paragraph
subparagraph Lorem ipsum dolor ...

and the PDF document outline only contains section and subsection.

What I want is this:

# Contents

1. section
1. subsection

# 1. section

## 1.1 subsection

Lorem ipsum dolor ...

but with a PDF document outline containing section, subsection, subsubsection, paragraph, and subparagraph.

• You mean the bookmarks, not the pdf index – user31729 Feb 14 '17 at 18:36
• @ChristianHupfer You are correct in that 'index' is not the correct word. According to the PDF reference manual the correct term for the feature is 'Document Outline'. – AJMansfield Feb 14 '17 at 21:19
• Yes, that's even better. – user31729 Feb 14 '17 at 21:20

Use the bookmark package and say \bookmarksetup{depth=5} to keep all the bookmarks down to \subparagraph level -- this works independently of the tocdepth setting.

\documentclass{article}
\usepackage[bookmarks,bookmarksopen=true,bookmarksopenlevel=5]{hyperref} %pdf index
\usepackage{bookmark}

\usepackage{blindtext}
\setcounter{tocdepth}{2}
\begin{document}
\bookmarksetup{depth=5}
\tableofcontents
\section{section}
\subsection{subsection}
\subsubsection{subsubsection}
\paragraph{paragraph}
\subparagraph{subparagraph}
\blindtext[1]
\end{document}