3

I need to center the title of the parts in the toc. I'm trying these two approaches:

\documentclass{article}
\usepackage{hyperref}
\usepackage{tocloft}
\cftpagenumbersoff{part}
\begin{document}
    \tableofcontents
    \addcontentsline{toc}{part}{\centerline{Part A}}
    \addcontentsline{toc}{section}{Section A}
    \addcontentsline{toc}{part}{\hfill{Part B}\hfill}
    \addcontentsline{toc}{section}{Section B}
\end{document}

When the PDF is generated, the bookmarks have some of problems:

  1. Part A appears in the bookmarks as "toPart A".
  2. Part B appears hierarchically under Section A.
  3. Part B appears correctly in the bookmarks, but it's not correctly centered in the page.

What can I do to solve this problem? Thank you!

  • 2
    The content of \hfill etc. is not allowed in the bookmarks. Why are you generating such entries manually? – user31729 Jun 7 '17 at 19:59
  • Christian, I tried to simplify the document to isolate the problems. The real document is much more complex than this. The entries are inserted manually because they can't appear in the document body, only in the toc. – Zé Cláudio Jun 7 '17 at 20:02
2

\hfill or \centerline is no valid content for the bookmarks. Use \texorpdfstring{ToC content}{bookmark content}.

Also use the correct load order: tocloft, hyperref, bookmark to get the correct hierarchy.

\documentclass{article}
\usepackage{tocloft}
\usepackage{hyperref}
\usepackage{bookmark}
\cftpagenumbersoff{part}
\begin{document}
\tableofcontents
\addcontentsline{toc}{part}{\texorpdfstring{\centerline {Part A}}{Part A}}
\addcontentsline{toc}{section}{Section A}
\addcontentsline{toc}{part}{\texorpdfstring{\centerline {Part B}}{Part B}}
\addcontentsline{toc}{section}{Section B}
\end{document}
  • Thanks, Christian, but the centering has the same problem described in "Part B" of my example: the text is not correctly centered. The centering should consider the area where page numbers appear. – Zé Cláudio Jun 7 '17 at 20:06
  • 1
    @ZéCláudio: It's still better than before and I am working on it. – user31729 Jun 7 '17 at 20:08
  • Yes, it is, Christian. Thank you very much. Based on you help I tried this "\addcontentsline{toc}{part}{\texorpdfstring{\centerline{Part A}}{Part A}}" and it is correctly centered now. The only problem is the bookmarks hierarchy. – Zé Cláudio Jun 7 '17 at 20:16
  • Whoa! That's what I needed. Thank you very much, @ChristianHupfer! – Zé Cláudio Jun 7 '17 at 20:21
  • 1
    @ZéCláudio: The bookmark package is the wizard here! – user31729 Jun 7 '17 at 20:21
2

If you want a good bookmark hierarchy, you should add anchors. And imho the look of the part entries should be set through the tocloft commands:

\documentclass{article}

\usepackage{tocloft}
\cftpagenumbersoff{part}

\renewcommand \cftpartfont     {\hfill}
\renewcommand \cftpartafterpnum{\hfill\mbox{}} 
\usepackage{hyperref}
\begin{document}
\tracingmacros=1
    \tableofcontents
    \phantomsection\addcontentsline{toc}{part}{Part A}
    \phantomsection\addcontentsline{toc}{section}{Section A}
    \phantomsection\addcontentsline{toc}{part}{Part B}
    \phantomsection\addcontentsline{toc}{section}{Section B}
\end{document}
2

\addcontentsline uses the latest anchor setting for the bookmark. The example of the question does not contain an explicit anchor setting for the \addcontentsline commands, therefore they all share the same latest anchor, in this case the anchor from the begin of the document.

The algorithm for the bookmarks does not work correctly, if there are several bookmarks that share the same anchor. This is fixed by adding package bookmark.

An anchor can be set by \phantomsection and it should be ensured that the location is correct, because this is the target of the bookmark. Often the following pattern can be seen:

\cleardoublepage
\phantomsection
\addcontentsline{toc}{part}{...}
Part stuff.

Macros like \centerline that do not make sense and do not work in bookmarks can be globally disabled by \pdfstringdefDisableCommands, e.g.:

\pdfstringdefDisableCommands{\let\centerline\@firstofone}

However, \centerline does not look too good inside \addcontentsline. It usually better to change the layout of the table of contents instead ( (e.g. redefining \l@part or using a package that deals with the layout of the table of contents).

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.