# Section numbers in PDF bookmarks

I need to write section numbers in PDF bookmarks, but only for section (and not for subsections etc.). Desired result:

Now I use bookmark package, so I can write numbers for every bookmark with numbered option. It is possible, to show numbers only for top-level bookmarks? (Another-leveled items like subsections and deeper must be showed in bookmarks, but without numbers)

\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}

\usepackage[
open,
openlevel=2,
atend,
numbered
]{bookmark}
\makeatletter
\renewcommand{\Hy@numberline}[1]{#1. }
\makeatother

\begin{document}
\section{Bla bla}
...
\subsection{Lorem}
...
\subsection{ipsum}
..
\section{Bla bla bla}
\end{document}
• Sorry, but your question is a little bit unclear – user31729 Mar 10 '15 at 21:21

Option addtohook of package bookmark allows to change the settings for a bookmark in dependence of some parameters such as the bookmark level. Thus the following would solve the problem:

\bookmarksetup{
numbered,
\ifnum\bookmarkget{level}>1 %
\bookmarksetup{numbered=false}%
\fi
},
}

Unhappily option numbered is evaluated before the hook in \bookmark is executed, see the definition of \Hy@writebookmark in package bookmark. Thus the following example changes the effect of option numbered instead:

\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}

\usepackage[atend]{bookmark}

\bookmarksetup{
open,
openlevel=2,
numbered,
\ifnum\bookmarkget{level}>1 %
\DisableBookmarkNumbering
\fi
},
}
\makeatletter
\newcommand*{\DisableBookmarkNumbering}{%
\let\numberline\@gobble
% The followings are not needed for subsections and below
% \let\booknumberline\@gobble
% \let\partnumberline\@gobble
% \let\chapternumberline\@gobble
}
\makeatother

\begin{document}
\section{Bla bla}
...
\subsection{Lorem}
...
\subsection{ipsum}
..
\section{Bla bla bla}
\end{document}

You can use the following switches:

\documentclass{article}

\usepackage[
open,
openlevel=2,
atend,
numbered
]{bookmark}
\makeatletter
\makeatother

\begin{document}

\tableofcontents

\numbersections
\section{Bla bla}
...

\nonumbersections
\subsection{Lorem}
...

\subsection{ipsum}
..

\numbersections
\section{Bla bla bla}
\end{document}

\numbersections turns on numbering in the bookmarks, while \nonumbersections turns it off.

The above process can be automated by adding the following to your preamble:

\let\oldsection\section
\renewcommand{\section}{\numbersections\oldsection}
\let\oldsubsection\subsection
\renewcommand{\subsection}{\nonumbersections\oldsubsection}

It inserts the required switch before using \section or \subsection. Of course it's not needed for \subsubsection, as they would necessarily fall under \subsection (where bookmark numbering has been deactivated via \nonumbersections) and it would seem strange to have a \subsubsection without \subsection.

Some surgery on kernel macros; I redefine \@sect to write \xnumberline{<level>} instead of \numberline, but defining \xnumberline to ignore its first argument and execute \numberline.

I also patch \Hy@writebookmark to redefine \xnumberline so that \xnumberline{<level>} becomes \stas@numberline@<level>; such commands are then defined to do what we prefer: \stas@numberline@section prints the number followed by a period and a space, while \stas@numberline@subsection just ignores the number.

\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}

\usepackage{etoolbox}

\usepackage[
open,
openlevel=2,
atend,
numbered
]{bookmark}

\makeatletter
\patchcmd{\H@old@sect} % \@sect becomes \H@old@sect
{\numberline}
{\xnumberline{#1}}
{}{}
\def\xnumberline#1{\numberline} % for the TOC
\patchcmd{\Hy@writebookmark}
{\let\numberline}
{\let\xnumberline\stas@numberline\let\numberline}
{}{}
\patchcmd{\Hy@writebookmark}
{\else\let\numberline}
{\else\let\xnumberline\@gobbletwo\let\numberline}
{}{}

\def\stas@numberline#1{\csname stas@numberline@#1\endcsname}
\def\stas@numberline@section#1{#1. }
\let\stas@numberline@subsection\@gobble
\let\stas@numberline@subsubsection\@gobble
\makeatother

\begin{document}
\section{Bla bla}
...
\subsection{Lorem}
...
\subsection{ipsum}
..
\section{Bla bla bla}
\end{document}

• IMHO this is an ugly hack. – Martin Schröder Mar 12 '15 at 9:21
• @MartinSchröder I don't agree. It gives much more structure than a simple \numberline in the .toc entries, so one gets more freedom in typesetting the numbers. – egreg Mar 12 '15 at 9:44