1

I am trying to add table of content to the bookmarks while using the tcolorbox package, I am using memoir class. Following is my MWE.

enter image description here

\documentclass[oneside]{memoir}
\usepackage[most]{tcolorbox}
\usepackage{lipsum}
\usepackage[bookmarks=true]{hyperref}

\begin{document}
\frontmatter
\chapter{Sample front matter chapter}
\lipsum[1-2]

\newpage

\begin{tcolorbox}[
    breakable,
    enhanced jigsaw,title={\contentsname},fonttitle=\bfseries\Large,
    %   colback=white,% colback=yellow!10!white,
    opacityback=0,
    colframe=red!50!black,before=\par\bigskip\noindent,
    %   interior style={fill overzoom image=goldshade.png,fill image opacity=0.25},
    colbacktitle=red!50!yellow!75!black,
    enlargepage flexible=\baselineskip,pad at break*=3mm,
    %   watermark color=yellow!75!red!25!white,
    %   watermark text={\bfseries\Large \contentsname},
    attach boxed title to top center={yshift=-0.25mm-\tcboxedtitleheight/2,yshifttext=2mm-\tcboxedtitleheight/2},
    boxed title style={enhanced,boxrule=0.5mm,
        frame code={ \path[tcb fill frame] ([xshift=-4mm]frame.west) -- (frame.north west)
            -- (frame.north east) -- ([xshift=4mm]frame.east)
            -- (frame.south east) -- (frame.south west) -- cycle; },
        interior code={ \path[tcb fill interior] ([xshift=-2mm]interior.west)
            -- (interior.north west) -- (interior.north east)
            -- ([xshift=2mm]interior.east) -- (interior.south east) -- (interior.south west)
            -- cycle;}  },
    %   drop fuzzy shadow
    ]
    \makeatletter
    \small
    \@starttoc{toc}
    \makeatother
%       \addcontentsline{toc}{chapter}{test}
\end{tcolorbox}

\mainmatter
\part{Test Part One}

\chapter{Test Chapter One}
\lipsum[1]
\section{Test Section One}
\lipsum[1]
\end{document}
3

There you go!

Digging in the \tableofcontents macro I found the lines responsible for creating the Table of Contents entry in the table of contents:

 \phantomsection% <- Add a placeholder section
 \addcontentsline{toc}{chapter}{\contentsname}% <- Add the ToC entry to the .toc file

Then I just added both right before the \@starttoc{toc}.

Also, I took the liberty to add \thispagestyle{empty} to the Contents page to remove the title of the previous chapter from the header.

The actual table of contents: enter image description here

And naturally, the pdf bookmarks: enter image description here

\documentclass[oneside]{memoir}
\usepackage[most]{tcolorbox}
\usepackage{lipsum}
\usepackage[bookmarks=true,colorlinks]{hyperref}

\begin{document}
\frontmatter

\chapter{Sample front matter chapter}

\lipsum[1-2]

\newpage

\begin{tcolorbox}[
    breakable,
    enhanced jigsaw,title={\contentsname},fonttitle=\bfseries\Large,
    %   colback=white,% colback=yellow!10!white,
    opacityback=0,
    colframe=red!50!black,before=\par\bigskip\noindent,
    %   interior style={fill overzoom image=goldshade.png,fill image opacity=0.25},
    colbacktitle=red!50!yellow!75!black,
    enlargepage flexible=\baselineskip,pad at break*=3mm,
    %   watermark color=yellow!75!red!25!white,
    %   watermark text={\bfseries\Large \contentsname},
    attach boxed title to top center={yshift=-0.25mm-\tcboxedtitleheight/2,yshifttext=2mm-\tcboxedtitleheight/2},
    boxed title style={enhanced,boxrule=0.5mm,
        frame code={ \path[tcb fill frame] ([xshift=-4mm]frame.west) -- (frame.north west)
            -- (frame.north east) -- ([xshift=4mm]frame.east)
            -- (frame.south east) -- (frame.south west) -- cycle; },
        interior code={ \path[tcb fill interior] ([xshift=-2mm]interior.west)
            -- (interior.north west) -- (interior.north east)
            -- ([xshift=2mm]interior.east) -- (interior.south east) -- (interior.south west)
            -- cycle;}  },
    %   drop fuzzy shadow
    ]
    \thispagestyle{empty}% <- SUGGESTION
    \makeatletter
    \small
    % HERE ↓↓↓↓↓↓↓↓
    \phantomsection
    \addcontentsline{toc}{chapter}{\contentsname}
    % HERE ↑↑↑↑↑↑↑↑
    \@starttoc{toc}
    \makeatother
\end{tcolorbox}

\mainmatter
\part{Test Part One}

\chapter{Test Chapter One}
\lipsum[1]
\section{Test Section One}
\lipsum[1]
\end{document}

By the way, nice table of contents :)

  • 1
    The ToC format is either stolen from the manual of tcolorbox or perhaps from here https://tex.stackexchange.com/questions/406158/reproducing-toc-format ;-) – user31729 Mar 11 '18 at 12:04
  • 1
    @ChristianHupfer Ooh... Nice table of contents for you then :) I didn't see your answer before... This is me, always finding the longest path to the solution :P – Phelype Oleinik Mar 11 '18 at 13:36
  • I did not invent it. The code in my answer there is taken from the tcolorbox manual. – user31729 Mar 11 '18 at 13:38
3

Using tcolorbox tools, i.e. defining a special tocbox and saying list inside and list type=chapter does this automatically then.

\documentclass[oneside]{memoir}
\usepackage[most]{tcolorbox}
\usepackage{lipsum}
\usepackage[bookmarks=true]{hyperref}


\newtcolorbox[list inside=toc,list type=chapter]{tocbox}{%
    breakable,
    enhanced jigsaw,title={\contentsname},fonttitle=\bfseries\Large,
    %   colback=white,% colback=yellow!10!white,
    opacityback=0,
    colframe=red!50!black,before=\par\bigskip\noindent,
    %   interior style={fill overzoom image=goldshade.png,fill image opacity=0.25},
    colbacktitle=red!50!yellow!75!black,
    enlargepage flexible=\baselineskip,pad at break*=3mm,
    %   watermark color=yellow!75!red!25!white,
    %   watermark text={\bfseries\Large \contentsname},
    attach boxed title to top center={yshift=-0.25mm-\tcboxedtitleheight/2,yshifttext=2mm-\tcboxedtitleheight/2},
    boxed title style={enhanced,boxrule=0.5mm,
        frame code={ \path[tcb fill frame] ([xshift=-4mm]frame.west) -- (frame.north west)
            -- (frame.north east) -- ([xshift=4mm]frame.east)
            -- (frame.south east) -- (frame.south west) -- cycle; },
        interior code={ \path[tcb fill interior] ([xshift=-2mm]interior.west)
            -- (interior.north west) -- (interior.north east)
            -- ([xshift=2mm]interior.east) -- (interior.south east) -- (interior.south west)
            -- cycle;}  },
%        list entry=\contentsname
        % drop fuzzy shadow
}

\begin{document}
\frontmatter
\chapter{Sample front matter chapter}
\lipsum[1-2]

\newpage

\begin{tocbox}
    \makeatletter
    \small
    \@starttoc{toc}
    \makeatother
\end{tocbox}

\mainmatter
\part{Test Part One}

\chapter{Test Chapter One}
\lipsum[1]
\section{Test Section One}
\lipsum[1]
\end{document}
1

Unlike other classes the memoir class \tableofcontents sets up the ToC and adds its title to the ToC while \tableofcontents* acts like tablofcontents in other classes and does not add its title to the ToC. (In retrospect this was probably an unfortunate design decision).

I think that a simple change to your MWE will give you what you want:

Replace the lines:

\makeatletter
\small
\@starttoc{toc}
\makeatother
\addcontentsline{toc}{chapter}{test}
\end{tcolorbox}

with

\small
\tableofcontents
\addcontentsline{toc}{chapter}{test} % do you really need this in your final doc?
\end{tcolorbox}

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.