1

I have read about this: Pretty table of contents

Can someone tell me how to do that circle for my \part? I tried to copy some code but obviously did not work.

\documentclass[twoside]{book}
\usepackage[utf8]{inputenc}
\usepackage[italian]{babel}
\usepackage{geometry}
\usepackage{fancyhdr,calc,blindtext}
\usepackage{xwatermark,wallpaper,grffile}
\usepackage{bm,graphicx,empheq,etoolbox}
\usepackage{units,cancel,mathtools,lipsum,lmodern}
\usepackage{varwidth,verbatim,setspace,lmodern}
\usepackage{anyfontsize,rotating,framed,titletoc}
\usepackage{amsmath,tocloft, amssymb,fancyhdr,color,contour}
\usepackage{booktabs,caption,multirow,marginnote}
\usepackage{xcolor}
\usepackage[explicit]{titlesec}
\usepackage[listings]{tcolorbox}
\tcbuselibrary{listings,theorems,skins,breakable
,theorems,many,raster}
\usepackage{hyperref}
\hypersetup{colorlinks=true, linkcolor=black, anchorcolor=black, citecolor=black, urlcolor=black,pdfstartpage=5}
\usepackage{listings, float, wasysym}
\floatstyle{boxed}

\setlength\parindent{0pt}
\DeclareMathAlphabet{\mathpzc}{OT1}{pzc}{m}{it}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{fadings,decorations.text}



\setcounter{tocdepth}{3}
\addtocontents{toc}{\cftpagenumbersoff{chapter}}
\setlength{\cftsubsecindent}{\cftsecnumwidth}
\patchcmd{\tableofcontents}{\contentsname}{\sffamily\contentsname}{}{}
\setlength{\cftbeforechapskip}{6ex}
\setlength{\cftbeforesecskip}{0ex}

%---this doesn't work!--------------
\newcommand\mycircle[1]{
\begin{tikzpicture}%
\node[draw=yellow,circle, text width=18pt,line width=1pt,align=center] {#1};
\end{tikzpicture}}
\makeatletter
\patchcmd{\@part}
  {\addcontentsline{toc}{part}{\thepart\hspace{1em}#1}}
  {\addtocontents{toc}{\protect\addvspace{20pt}}
    \addcontentsline{toc}{part}{\huge{\protect\color{yellow}%
      \setlength\fboxrule{2pt}\protect\mycircle{%
        \hfil\thepart\hfil%
      }%
    }\\[2ex]\color{red}\sffamily#1}}{}{}
%----------------------------



\begin{document}
\tableofcontents
\part{First Part}

\chapter{My first}
    \section{section 1}
    \subsection{stuff1}
        \subsubsection{substuff1}
    \subsection{stuff2}
    \subsection{stuff3}

\chapter{My second}
\section{section 2}

-----------

\part{Second Part}
\chapter{My third}
\section{section 3}


\end{document}
Francesco
  • 361
  • 1
    This preamble is a real mess. Did you notice that you load some packages more than once? Most of the packages aren't needed for the ToC related issue at all! –  Mar 23 '16 at 14:32

1 Answers1

5

hyperref redefines \@part etc. considerably, so \patchcmd{...} after using hyperref is too late. The patches has to be done before hyperref is loaded.

Writing something fragile like tikzpicture command to the .aux and .toc file is difficult, it's better to robustify the \mycircle command then!

\documentclass[twoside]{book}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[italian]{babel}
\usepackage{geometry}
\usepackage{fancyhdr}
\usepackage{calc}
\usepackage{blindtext}
\usepackage{xwatermark}
\usepackage{wallpaper}
\usepackage{grffile}
\usepackage[explicit]{titlesec}

\usepackage{bm,graphicx,empheq}
\usepackage{units,cancel,mathtools,lipsum,lmodern}
\usepackage{varwidth,verbatim,setspace,lmodern}
\usepackage{anyfontsize,rotating,framed,titletoc}

\usepackage{amsmath,amssymb,contour}
\usepackage{booktabs,caption,multirow,marginnote}
\usepackage{tocloft}
\usepackage{xcolor}
\usepackage[most]{tcolorbox}
\usepackage{etoolbox}
\usepackage{xpatch}
\usepackage{listings}
\usepackage{wasysym}
\usepackage{float}



\makeatletter
\newcommand\mycircle[1]{%
  \begin{tikzpicture}%
    \node[draw=blue,circle, text width=18pt,line width=1pt,align=center] {#1};
  \end{tikzpicture}
}

\robustify{\mycircle}
\xpatchcmd{\@part}{%
    \addcontentsline{toc}{part}{\thepart\hspace{1em}#1}%
  }{%
    \addcontentsline{toc}{part}{\huge\texorpdfstring{\hfil\protect\color{blue}%
        \mycircle{%
        \thepart}\hfil%%
      \\[2ex]\color{red}\sffamily#1}{#1}}%
  }{\typeout{Success}}{\typeout{Failed}}


%----------------------------

\makeatother


\usepackage{hyperref}
\hypersetup{colorlinks=true, linkcolor=black, anchorcolor=black, citecolor=black, urlcolor=black,pdfstartpage=5}
%\floatstyle{boxed}

\setlength\parindent{0em}
%\DeclareMathAlphabet{\mathpzc}{OT1}{pzc}{m}{it}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{fadings,decorations.text}



\setcounter{tocdepth}{3}
\addtocontents{toc}{\cftpagenumbersoff{chapter}}
\setlength{\cftsubsecindent}{\cftsecnumwidth}
\patchcmd{\tableofcontents}{\contentsname}{\sffamily\contentsname}{}{}
\setlength{\cftbeforechapskip}{6ex}
\setlength{\cftbeforesecskip}{0ex}

%---this doesn't work!--------------


\begin{document}
\tableofcontents
\part{First Part}

\chapter{My first}
    \section{section 1}
    \subsection{stuff1}
        \subsubsection{substuff1}
    \subsection{stuff2}
    \subsection{stuff3}

\chapter{My second}
\section{section 2}

-----------

\part{Second Part}
\chapter{My third}
\section{section 3}


\end{document}

enter image description here