Assuming that you're using hyperref
already*, the documentation states:
Usually hyperref
automatically adds bookmarks for \section
and similar macros. But they can also set manually.
The manual setting is performed using
\pdfbookmark[<level>]{<text>}{<name>}
where \part
is level -1
, \chapter
is level 0
, \section
is level 1
, ... It places <text>
in the bookmarks and can be referenced internally (via a hyperlink) as <name>
. Or, if you want to place it at a level relative to the current one, use
\currentpdfbookmark{<text>}{<name>}
\subpdfbookmark{<text>}{<name>}
\belowpdfbookmark{<text>}{<name>}
where <text>
and <name>
have similar meanings as before.
In the following MWE, hyperref
creates sectional bookmarks for every unstarred heading up to secnumdepth
and only the numbered, unstarred versions show up in the ToC. The starred versions, removed from the ToC by default, are included using some variation of \...pdfbookmark
:
\documentclass{book}
\usepackage{hyperref}% http://ctan.org/pkg/hyperref
\usepackage{bookmark}% http://ctan.org/pkg/bookmark
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\begin{document}
\tableofcontents
\part{FIRST PART}
\chapter{First chapter} \lipsum[1]
\section{First section} \lipsum[2]
\section*{Second section} \currentpdfbookmark{Second section}{ch:1:sec:2} \lipsum[3]
\section{Third section} \lipsum[4]
\subsection{First subsection} \lipsum[5]
\subpdfbookmark{First subsubsection}{ch:1:sec:3:ssec:1:sssec:1}\subsubsection{First subsubsection} \lipsum[6]
\currentpdfbookmark{Second subsubsection}{ch:1:sec:3:ssec:1:sssec:2} \subsubsection*{Second subsubsection} \lipsum[7]
\subsection{Second subsection} \lipsum[8]
\currentpdfbookmark{Third subsection}{ch:1:sec:3:ssec:3} \subsection*{Third subsection} \lipsum[9]
\pdfbookmark[2]{Fourth subsection}{ch:1:sec:3:ssec:4}\subsection*{Fourth subsection} \lipsum[10]
\subsection{Fifth subsection} \lipsum[11]
\chapter{Second chapter} \lipsum[12]
\end{document}
For the inclusion of sectional numbers in the PDF bookmark panel, use the bookmarksnumbered
option of hyperref
:
\usepackage[bookmarksnumbered]{hyperref}% http://ctan.org/pkg/hyperref
The above MWE will then produce:
* If not, the bookmark
package provides similar, stand-alone functionality for inclusion of PDF bookmarks in your document. In fact, hyperref
suggests using it as improved bookmark organization.
After a little tinkering, plus a little inspiration from here and there, I've com up with a solution.
The bookmarksetupnext{level=part}
simply sets the next bookmark to have the part
level. There is no bookmark named "Appendices", and because the next bookmark is Appendix A, this appendix chapter will be changed to have the level of a part. Appendix B is still unchanged, so it remains at the chapter level and hence the bookmark will be a child of Appendix A.
Adding replacing the bookmarksetupnext{level=part}
with \pdfbookmark[-1]{Appendices}{bookmark:appendices}
solves this problem.
To add a table of contents (TOC) entry named "Appendices", a command like \cftaddtitleline{toc}{chapter}{Appendices}{}
can be used. This reuquired the tocloft
package. This command will reference the previous bookmark, so a bookmark must be added at the appropriate place. Adding the the following lines just before \begin{appendices}
will place create a TOC entry which points to the "Appendices" page. For a two-sided document, the \cleardoublepage
command should be used instead. The provided code omits the page number in the TOC entry. I chose to do this because the "Appendices" page is empty. The page number can be manually added in the last, empty brace.
\clearpage
\phantomsection
\pdfbookmark[-1]{Appendices}{bookmark:appendices}
\cftaddtitleline{toc}{chapter}{Appendices}{}
To lower the appendix chapters, sections and subsections by one level, the following code can be added before the first appendix chapter. With the default TOC depth of 2, the appendix subsections will be hidden from the TOC. To display these, increase the TOC depth as per usual by the command \setcounter{tocdepth}{3}
.
\makeatletter
\addtocontents{toc}{
\begingroup
\let\protect\l@chapter\protect\l@section
\let\protect\l@section\protect\l@subsection
\let\protect\l@subsection\protect\l@subsubsection
}
\makeatother
Below is a complete example. I've also added another part-level bookmark titled "Body", just to make the bookmarks in the PDF viewer line up properly. The "Body" bookmark points to the TOC.
\documentclass[11pt]{scrreprt}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[hidelinks]{hyperref}
\usepackage[numbered,open]{bookmark}
\usepackage[page]{appendix}
\usepackage{tocloft}
\usepackage{lipsum}
\renewcommand{\appendixpagename}{Appendices\thispagestyle{empty}}
\setcounter{tocdepth}{3}
\begin{document}
\pdfbookmark[-1]{Body}{bookmark:body}
\tableofcontents
\chapter{First chapter}
\lipsum[1-2]
\section{First section}
\lipsum[3-6]
\subsection{First subsection}
\lipsum[7-10]
\subsection{Second subsection}
\lipsum[11-14]
\section{Second section}
\lipsum[15-18]
\chapter{Second chapter}
\lipsum[19-22]
\clearpage
\phantomsection
\pdfbookmark[-1]{Appendices}{bookmark:appendices}
\cftaddtitleline{toc}{chapter}{Appendices}{}
\begin{appendices}
\makeatletter
\addtocontents{toc}{
\begingroup
\let\protect\l@chapter\protect\l@section
\let\protect\l@section\protect\l@subsection
\let\protect\l@subsection\protect\l@subsubsection
}
\makeatother
\chapter{First appendix}
\lipsum[23-24]
\section{First appendix section}
\lipsum[25-28]
\subsection{First appendix subsection}
\lipsum[29-32]
\subsection{Second appendix subsection}
\lipsum[33-36]
\section{Second appendix section}
\lipsum[37-40]
\chapter{Second appendix}
\lipsum[41-44]
\end{appendices}
\end{document}
The last question, on how to redefine bookmark styles, would be better to have as a separate question.
Best Answer
Option
addtohook
of packagebookmark
helps: