0

Compiling a book (i.e. \documentclass{book}) I have a problem with the bookmarks.

Here there is the structure I would like to obtain (the large spaces denotes a visual dependency in the bookmark from the objects above):

Contents
Preface
1. Introduction
Part 1
    2. Title
Part 2
    3. Title

while this is what I obtain:

Contents
    Preface
    1. Introduction
Part 1
    2. Title
Part 2
    3. Title

This is the result of the following piece of code:

\documentclass[12pt]{book}
%
\usepackage[hypertexnames=false]{hyperref}
%
\newcommand{\mychapter}[2]{
    \setcounter{chapter}{#1}
    \setcounter{section}{0}
    \chapter*{#2}
    \addcontentsline{toc}{chapter}{#2}
}
%
%%%%%%%%%
%%%%%%%%%
\begin{document}

\pagestyle{empty}

\title{Title}
\author{Author}

\maketitle

\pagenumbering{roman}

\setcounter{page}{0}

\tableofcontents

\addcontentsline{toc}{part}{Contents}

\pagestyle{plain}

%%%%%%%%%%%%
\mychapter{0}{Preface}

\mainmatter


%%%%%%%%%%%
\chapter{Introduction}

%%%%%%%%
\part{Title}

\chapter{Title}


%%%%%%%%
\part{Title}

\chapter{Title}

\end{document}

Few points concerning this piece of code:

  1. Of course, if we delete the string \addcontentsline{toc}{part}{Contents} all the problems are fixed, in the sense that both the preface and the chapter "1. introduction" are at the same bookmark level of "Part 1" and "Part 2". Still, in this way I don't get the contents in the bookmarks, and I would like to have them there.

  2. I included the option hypertexnames=false because this was the solution I found here to a problem I had with hyperref.

  3. The command \newcommand{\mychapter}[2]{... etc...} is again a solution I found here for what I wanted to get.

Any feedback concerning this issue is most welcome.
Thank you for your time.

1

You can use the bookmark package to reset levels. And you should consider to use a better class so that you don't have to do this \mychapter-stuff:

\documentclass[12pt]{scrbook}
%
\usepackage[hypertexnames=false]{hyperref}
\usepackage{bookmark}
%


\begin{document}

\title{Title}
\author{Author}

\maketitle

\frontmatter
\hypertarget{toc}{}
\bookmark[dest=toc]{\contentsname}
\tableofcontents

\bookmarksetup{startatroot}
\addchap{Preface}

\mainmatter

\bookmarksetup{startatroot}
\chapter{Introduction}

\part{Title}

\chapter{Title}

\part{Title}

\chapter{Title}

\end{document}

enter image description here

| improve this answer | |
  • Thanks, that works supersmoothly (indeed, you are right, but for now I would like to stick to bookout of pure ignorance and lack of time). Just one last question: in this way I got the line Contents both in the TOC and in the bookmarks. How can I get rid of that line in the TOC? – Kolmin Sep 22 '16 at 8:02
  • 1
    You don't get contents in the toc with my example -- I made quite sure to remove it as I think it is silly to mention the toc in the toc. – Ulrike Fischer Sep 22 '16 at 8:04
  • Indeed, sorry & thanks a lot: It works just great! – Kolmin Sep 22 '16 at 8:06
  • Sorry to bother again, but I got a minor problem: the bookmark associated to Contents doesn't point to the TOC, but to the page just before the TOC. Could this be fixed? – Kolmin Sep 22 '16 at 8:16
  • 1
    Not in my example ... – Ulrike Fischer Sep 22 '16 at 8:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.