1

I'd like to have colored boxes around the titles in my toc. The box should begin at the edge of the page and finish after the end of the chapter name.

I found many solutions for having a box going from the chapter number to the page number. However, I couldn't find one corresponding to my need.

Here is an example of what I am trying to achieve: Exemple of Content section with chapter title in boxes going from edge of page to end of title

Nb : due to font integration issues, I am using XeLatex.

edit

As mentioned before, I need to use XeLatex for unicode support. Hence, Tikz are not a good solution as tikzmark and pgfmark are not well supported (as per Why xelatex renders arrows in tikz figures differently than lualatex? and my many many trials).

I managed to get something working partially:

\documentclass[a4paper,11pt, american]{report}
\usepackage[utf8]{inputenc}
\usepackage{tocloft,tikz}
\usetikzlibrary{calc}

\makeatletter \newcommand{\gettikzxy}[3]{% \tikz@scan@one@point\pgfutil@firstofone#1\relax \edef#2{\the\pgf@x}% \edef#3{\the\pgf@y}% } \makeatother

\newcommand*{\tikzcoordinate}[1]{% \tikz[remember picture, overlay] \coordinate (#1);} \renewcommand{\cftchapfont}{% \tikzcoordinate{A} \bfseries\Large% } \renewcommand{\cftchappagefont}{\bfseries} \renewcommand{\cftchapleader}{% \tikz[remember picture,overlay]{% \coordinate (C);% \coordinate (B) at (current page.west|- A); \gettikzxy{(A)}{\ax}{\ay} \gettikzxy{(B)}{\bx}{\by} \gettikzxy{(C)}{\cx}{\cy} \fill[red,opacity=.2] ([yshift=+1.3333em,xshift=-.3333em]B) rectangle ([xshift=.3333em,yshift=+-1ex]C |- 0,0); \ifx\by\cy \else \coordinate (D) at (\cx+.3333em,\by+1.3333em); \coordinate (E) at (\ax-.8em+\linewidth- \cftchapindent- \cftchapnumwidth,\cy+1em); \gettikzxy{(D)}{\dx}{\dy} \gettikzxy{(E)}{\ex}{\ey} \ifdim\dx<\ex \fill[red,opacity=.2] (D) rectangle (E); \else \fill[white,opacity=1] (D) rectangle (E); \fi \fi }% \bfseries\cftdotfill{\cftdotsep}% }

\begin{document} \tableofcontents \chapter{(title chapter 1)}

\section{(title section 1)} \subsection{(title sub-section 1)} \subsection{(title sub-section 2)} \section{(title section 2)} \subsection{(title sub-section 1)} \subsection{(title sub-section 2)} \chapter{(veryvery very very very very very very very very very very very very very very very very very very very very very long title chapter 2)} \section{(title section 1)} \subsection{(title sub-section 1)} \subsection{(title sub-section 2)} \section{(title section 2)} \subsection{(title sub-section 1)} \subsection{(title sub-section 2)} \chapter{(very very very very very very very very long title chapter 3)} \section{(title section 1)} \subsection{(title sub-section 1)} \subsection{(title sub-section 2)} \section{(title section 2)} \subsection{(title sub-section 1)} \subsection{(title sub-section 2)} \end{document}

Which produces: enter image description here

Now, my problem is that I need the color to be on the background to allow the text to be written in white on top of it. Any clue on how to do that with XeLatex ?

  • Take one of the solutions using tikz and add an extension to (current page.west). A solution using tikzmark will already use [remember picture,overlay]. Otherwise you may need pgfinterruptboundingbox/ – John Kormylo Feb 18 '23 at 14:41

1 Answers1

4

Finally found a solution. Issue in using tikzmark was not XeLatex. However, a counter has to be used to ensure that each mark name is unique. There is probably a simpler solution but this one is working fine.

\documentclass{report}
\usepackage{tocloft,tikz}
\usetikzlibrary{calc}
\usetikzlibrary{tikzmark}

\makeatletter \newcommand{\gettikzxy}[3]{% \tikz@scan@one@point\pgfutil@firstofone#1\relax \edef#2{\the\pgf@x}% \edef#3{\the\pgf@y}% } \makeatother

\newcounter{chapmarkcounter} \setcounter{chapmarkcounter}{1}

\renewcommand{\cftchapfont}{% \def\markname{cs:cmark:\expandafter\thechapmarkcounter}% \tikz[remember picture,overlay]{% \coordinate (A);% \coordinate (B) at (current page.west|- A);% \coordinate (C) at (pic \expandafter\markname);% \gettikzxy{(A)}{\ax}{\ay}% \gettikzxy{(B)}{\bx}{\by}% \gettikzxy{(C)}{\cx}{\cy}% \fill[red] (\bx-.3333em, \by+1.3333em) rectangle (\cx + .3333em, \cy-0.9ex);% \ifx\by\cy% \else% \coordinate (D) at (\cx+.3333em,\by+1.3333em);% \coordinate (E) at (\ax-.8em+\linewidth- \cftchapindent- \cftchapnumwidth,\cy+1em);% \gettikzxy{(D)}{\dx}{\dy}% \gettikzxy{(E)}{\ex}{\ey}% \ifdim\dx<\ex% \fill[red] (D) rectangle (E);% \else% \fill[white] (D) rectangle (E);% \fi% \fi% }% \color{white}\bfseries\Large% }

\renewcommand{\cftchappagefont}{\Large\bfseries} \renewcommand{\cftchapleader}{% \pgfmark{cmark:\expandafter\thechapmarkcounter}% \addtocounter{chapmarkcounter}{1}% \bfseries\cftdotfill{\cftdotsep}% }

\begin{document} \tableofcontents \chapter{(title chapter 1)} \section{(title section 1)} \subsection{(title sub-section 1)} \subsection{(title sub-section 2)} \section{(title section 2)} \subsection{(title sub-section 1)} \subsection{(title sub-section 2)} \chapter{(veryvery very very very very very very very very very very very very very very very very very very very very very long title chapter 2)} \section{(title section 1)} \subsection{(title sub-section 1)} \subsection{(title sub-section 2)} \section{(title section 2)} \subsection{(title sub-section 1)} \subsection{(title sub-section 2)} \chapter{(very very very very very very very very long title chapter 3)} \section{(title section 1)} \subsection{(title sub-section 1)} \subsection{(title sub-section 2)} \section{(title section 2)} \subsection{(title sub-section 1)} \subsection{(title sub-section 2)} \chapter{(title chapter 4)} \section{(title section 1)} \subsection{(title sub-section 1)} \subsection{(title sub-section 2)} \section{(title section 2)} \subsection{(title sub-section 1)} \subsection{(title sub-section 2)} \end{document}

enter image description here