part bookmarks and toc style

Within the book.cls class, I redefined the \part section to customize the respective toc entry as follows:

\def\@part[#1]#2{%
\ifnum \c@secnumdepth >-2\relax
\refstepcounter{part}%
\else%
...


The problem is that the \fcolorbox somehow does not go well with the created part bookmark in the pdf file. For instance, instead of parttitle, I am getting the following bookmark in the built pdf file:

push0 g 0 Gpop[-2]parttitleocreblack!5push0 g 0 Gpoptowidthheightdepthottheight-0.5 -widthheightdepthd- widthheightdepthheightted


I have been trying some \protect commands, without success so far. Would you be aware of a systematic way to correct this behavior. If not, I can still redefine the part titles directly in the pdf.

Something like this:

\documentclass{book}
\usepackage{xcolor}
\usepackage{hyperref}
\usepackage{calc}

\makeatletter

\def\@myparttocformat #1{\noindent
\fcolorbox{cyan}{black!5}
{\makebox[\linewidth-2\fboxsep-\@pnumwidth]{\Large\sffamily\strut #1}}}

\def\@part[#1]#2{%
\ifnum \c@secnumdepth >-2\relax
\refstepcounter{part}%
{\texorpdfstring{\protect\@myparttocformat{#1}}{#1}}%
\else
\addcontentsline {toc}{part}{#1}\fi \markboth {}{}{\centering \interlinepenalty \@M \normalfont \ifnum \c@secnumdepth >-2\relax \huge \bfseries \partname \nobreakspace \thepart \par \vskip 20\p@ \fi \Huge \bfseries #2\par }\@endpart }

\makeatother

\begin{document}
\tableofcontents

\part{Hello}

\chapter{A chapter}

world

\end{document}


I did this to keep close to your skeleton of a mwe, however, I would much rather redefine \l@part. (do not know in which device name xcolor option ocre is defined)

The hyperref link would admittedly then be only on the actual name of the Part, and not like here the entire shaded and framed box. But the whole thing would be cleaner.

with the bookmarks shown (now in Acrobat):

• Nice. Do you know why the part and chapter numbers are not included in the bookmarks? Feb 24 '13 at 22:12
• I think they never are for chapters and sections. They are for parts because standard LaTeX puts different things in the toc file then (no \numberline but a \hspace{1em}) and (I did not investigate more), presumably hyperref just filters out the \hspace{1em} which leaves the part number. Yes, I just tried with the memoir class and then even the parts do not get numbers in the bookmarks (memoir uses \partnumberline, \chapternumberline, ...)
– user4686
Feb 24 '13 at 22:18
• I just found a possible answer: tex.stackexchange.com/questions/11867/customize-pdf-bookmarks/… Feb 24 '13 at 22:23
• yes, that gives a fix. But does one really want numbers in bookmarks? bookmarks are supposed to be clicked upon... and the TOC already has the numbers.
– user4686
Feb 24 '13 at 22:29
• correct but when the document is long, numbered bookmarks do help a bit. Also, it looks like the \texorpdfstring command removes the automatic bookmark numbering. Feb 24 '13 at 22:30