Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Using this answer, I have highlighting for chapters in my Table of Contents.

For long chapter names, however, the box is wonky, because P1.north west and P1.north east do not span the entire line:

north west       north east                                      line end
-------------------------------------------------------------------------
This is a longer chapter heading than can fit in an entire line this is a
longer chapter heading than.

This is a problem both with and without the use of page numbers. When \cftpagenumbersoff{chapter} is used:

No page numbers

and with page numbers:

enter image description here

My current projects ToC does not use page-numbers. Future projects will, and a solution that works for both would be ideal.

How can I correctly highlight a long ToC entry using this approach?

MWE:

\documentclass{report}
\usepackage{tocloft}
\usepackage{tikz}
\usepackage{xcolor}
% command to make a hidden node
\newcommand*{\hnode}[1]{%
    \tikz[remember picture] \node[minimum size=0pt,inner sep=0pt,outer sep=5pt] (#1) {};}
% create a node at the beginning of the chapter entry
\renewcommand{\cftchapfont}{\hnode{P1}\bfseries\Large}
\renewcommand{\cftchappagefont}{\bfseries}
% create a node at the end of the chap page number and draw the gray box
\renewcommand{\cftchapafterpnum}{%
  \hnode{P2}\tikz[remember picture,overlay]
  \draw (P1.north west)  [line width={25pt}, gray,opacity=.2] -- (\textwidth,0 |- P2);}
  %\draw (P1.north west)  [line width={25pt}, gray,opacity=.2] -- (P2.north east);}


\begin{document}
\cftpagenumbersoff{chapter}
\tableofcontents
\addcontentsline{toc}{chapter}{This is a longer chapter title than can fit in a single line}
\end{document}

My attempt, which hasn't thus far worked: Conceptually, if I were to implement a solution, I would first check whether a chapter heading will span multiple lines (count characters, perhaps?). I'd then use a pre-determined cm value, with a pre-determined node height, as the bottom-right TikZ node coordinate.

I thought \linelength would work (without page numbers the highlighting is ragged, and in my head my attempt was going to have a neat side-effect of fixing that):

\draw (P1.north west)  [line width={25pt}, gray,opacity=.2] -- (\textwidth,0 |- P2);}

But that resulted in a highlighting extending far past the last word on the line, and the box is not horizontal. The intersection coordinate isn't strictly needed, but I don't know why - I thought I'd have to specify the y coordinate.

enter image description here

share|improve this question

1 Answer 1

up vote 3 down vote accepted

I’d use

  • coordinates instead of nodes (see the re-named \tikzcoordinate macro), especially if you don’t overlay them, and
  • a rectangle path instead of a line.

Furthermore, the point origin of the TikZ picture is after the word line (the same where P2 lies which you actually don’t need because of this fact), the point (\textwidth,0pt) then protrudes into the margin. See drawing a line the width of the text using tikz for similar problems.

Thus, the end of the line has to be re-established, this is done via the coordinate @aux that is placed with

\coordinate (@aux) at ([xshift=+\linewidth] P1);

It can then be used to find the line-end on the last line with (@aux |- 0,0).


You can avoid many problems if you use the tikzmark library where you can refer to a coordinate (disguised as an empty PGF picture) before it is actually defined in the document. This makes it possible to drop the opacity key and actually draw the rectangle behind the text.

Code

\documentclass{report}
\usepackage{tocloft,tikz}
\usepackage[showframe,pass]{geometry} % just for this example
\newcommand*{\tikzcoordinate}[1]{%
    \tikz[remember picture] \coordinate (#1);}
\renewcommand{\cftchapfont}{\tikzcoordinate{P1}\bfseries\Large}
\renewcommand{\cftchappagefont}{\bfseries}
\renewcommand{\cftchapafterpnum}{%
  \tikz[remember picture,overlay]{%
    \coordinate (@aux) at ([xshift=+\linewidth] P1);
    \fill[gray,opacity=.2] ([yshift=+1.3333em,xshift=-.3333em]P1)  rectangle
                                    ([xshift=.3333em,yshift=+-1ex]@aux |- 0,0);
  }%
}
\begin{document}
\cftpagenumbersoff{chapter}
\tableofcontents
\addcontentsline{toc}{chapter}{This is a longer chapter title than can fit in a single line}
\end{document}

Output

enter image description here

enter image description here

Code (tikzmark)

\documentclass{report}
\usepackage{tocloft,tikz}
\usepackage[showframe,pass]{geometry} % just for this example
\usetikzlibrary{tikzmark}
\renewcommand{\cftchapfont}{%
  \tikz[remember picture,overlay]%
    \fill[gray!20] (+-.3333em,+1.3333em) rectangle ([xshift=+.333em,yshift=+-1ex] +\linewidth,+0pt |- {pic cs:toc-chap-end});%
  \bfseries\Large}
\renewcommand{\cftchappagefont}{\bfseries}
\renewcommand{\cftchapafterpnum}{\pgfmark{toc-chap-end}}
\begin{document}
\cftpagenumbersoff{chapter}
\tableofcontents
\addcontentsline{toc}{chapter}{This is a longer chapter title than can fit in a single line}
\end{document}

Output (tikzmark)

enter image description here

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.