In the book I'm writing I make a lot of use of \chapter*
, \section*
, \subsection*
and so on. I want those sections to appear in the PDF boomarks, though. And here I am, two days later, still trying to figure out how to do that efficiently.
The closer result I was able to produce is this, using package nameref
.
As you'll see I'm using the command \metadef
to produce a starred version of \chapter
that behaves exactly like \chapter*
but with the added bonus of the bookmark.
The code for that comes from this other answer.
\documentclass[a5paper]{book}\usepackage{hyperref}\usepackage{nameref}\usepackage{lipsum}% Creates \metadef, a command used to better manage starred sectioning commands\makeatletter\newcommand{\metadef}[1]{% \DeclareRobustCommand#1{% \@ifstar{\csname s\string#1\endcsname}{\csname n\string#1\endcsname}% }% \edef\meta@def@name{\string#1}% \meta@def}\newcommand\meta@def[3][0]{% \expandafter\newcommand\csname n\meta@def@name\endcsname[#1]{#2}% \expandafter\newcommand\csname s\meta@def@name\endcsname[#1]{#3}%}\makeatother% Creates \chapterplus, a command that, if not starred, acts like \chapter, and, if starred, acts like \chapter* but adds the entry to the bookmarks\metadef{\chapterplus}[1]{\chapter{#1}}{\pdfbookmark{#1}{#1}\chapter*{#1}}\begin{document}\chapterplus*{Chapter one}\lipsum[1-2]\chapterplus{Chapter two}\lipsum[3-4]\chapterplus*{Chapter three}\lipsum[5-6]\end{document}
If you try this you'll see that the generated PDF will have a bookmark set for every chapter:
- the bookmark to chapter 1 works;
- the bookmark to chapter 2 works (it is generated normally by
\chapter
); - the bookmark to chapter 3 works but points to the end of the previous page.
I suppose this means that the anchor is being set in the place in which I'm calling \chapter*
, and THEN the page is broken; the chapter name is thus generated one, if not two pages later.
Since \chapter
is able to produce a working bookmark I suppose that there must be a way around this problem.
I tried to use this other solution, involving the use of the package bookmark
, but the result is exactly the same.
\documentclass[a5paper]{book}\usepackage{hyperref}\usepackage{bookmark}\usepackage{lipsum}% Creates \metadef, a command used to better manage starred sectioning commands\makeatletter\newcommand{\metadef}[1]{% \DeclareRobustCommand#1{% \@ifstar{\csname s\string#1\endcsname}{\csname n\string#1\endcsname}% }% \edef\meta@def@name{\string#1}% \meta@def}\newcommand\meta@def[3][0]{% \expandafter\newcommand\csname n\meta@def@name\endcsname[#1]{#2}% \expandafter\newcommand\csname s\meta@def@name\endcsname[#1]{#3}%}\makeatother% Creates \bkmrk, a command that easily sets a bookmark\newcounter{bkmrk}\newcommand{\bkmrk}[3]{% \addtocounter{bkmrk}{1}% \hypertarget{\thebkmrk}{#2}% \bookmark[dest=\thebkmrk, level=#1]{#3}%}% Creates \chapterplus, a command that, if not starred, acts like \chapter, and, if starred, acts like \chapter* but adds the entry to the bookmarks\metadef{\chapterplus}[1]{\chapter{#1}}{\bkmrk{0}{\chapter*{#1}}{#1}}\begin{document}\chapterplus*{Chapter one}\lipsum[1-2]\chapterplus{Chapter two}\lipsum[3-4]\chapterplus*{Chapter three}\lipsum[5-6]\end{document}
It probably would be easy to just modify \chapter
so that it does everything \chapter
does, including setting a correct bookmark, besides numbering the chapter, and I could do that because I'm only using starred sectioning commands at the moment. The point is that I'm trying to produce a futurible solution since I'm setting a preamble that I plan to use again for future books of the same series and I may want to use numbered chapters in the future, so I prefer to go with \chapter*
instead.