Adding a per-chapter image along with group of entries in ToC

Friends, I have an old analytic geometry book with an unusual table of contents: for every group of entries based on the current chapter, there is an image along with it. Well, let me show some actual code:

\documentclass{book}

\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{lipsum}

\begin{document}

\tableofcontents

\chapter{Coordinates of points}

\section{Rectangular coordinates}

\lipsum[1]

\section{Projections of a segment on the axes}

\lipsum[2]

\section{Distance between two points}

\lipsum[3]

\section{The mid-point of a segment}

\lipsum[4]

\section{Division of a segment in any ratio}

\lipsum[5]

\section{Oblique coordinates}

\lipsum[6]

\chapter{The locus of an equation}

\section{First illustrations}

\lipsum[1]

\section{Curve plotting}

\lipsum[2]

\section{Test that a point lie on a curve}

\lipsum[3]

\section{Intercepts}

\lipsum[4]

\section{Points of intersection of two curves}

\lipsum[5]

\section{Oblique coordinates}

\lipsum[6]

\chapter{The straight line}

\section{Equation in terms of point and slope}

\lipsum[1]

\section{Line through two points}

\lipsum[2]

\section{The general equation of first degree}

\lipsum[3]

\section{Parallel and perpendicular lines}

\lipsum[4]

\section{Angle between two lines}

\lipsum[5]

\section{Distance from a point to a line}

\lipsum[6]

\end{document}


I'm curious if there's a semi-automatic way of including a per-chapter image to appear in ToC. I can swear this geometry book was originally typeset in LaTeX! Any ideas or suggestions towards this result? :-)

-

The titletoc package provides the powerful \titlecontents macro that allows one to change ToC entries for sectional headings mid-document. In the MWE below, two new macros are defined using the xparse interface \NewDocumentCommand. Specifically, they are \rtocstuff[<gap>]{<content>}[<width>] and its complement \ltocstuff[<width>]{<content>}[<gap>]. The former typesets <content> on the right of the ToC group defined by the chapter, while the latter typesets <content> on the left. <width> (default 150pt) specifies the width of <content> (using an overlap to allow for varied widths), while <gap> specifies the distance between <content> and the ToC.

For the sake of illustration, I've added

\rtocstuff{\includegraphics[valign=T,width=50pt]{tiger}}%


to each odd chapter, and

\ltocstuff{\includegraphics[valign=T,width=50pt]{tiger}}%


to the even chapter.

The main idea behind the two commands is to adjust the ToC margins when typesetting a chapter ToC. This is done by using boxes of fixed length (for accurate spacing and horizontal alignment). Also, <content> is \smashed so that it can protrude up/downward into the sectional ToC headings.

The current implementation could be considered slightly primitive, but it works with some trickery with adjustbox's valign=T option. Additionally, the alignment within the overlapped box that holds <content> can be optimized, as well as the ToC layout. It also only modifies chapter and section ToC styles and nothing else. So if you need subsections (or others), they need to be included for consistency. But this may all depend on the context. The heavily commented code below will give you a good indication of what should/can be modified to obtain the desired output.

\documentclass{book}

\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{geometry}% http://ctan.org/pkg/geometry
\usepackage{titletoc}% http://ctan.org/pkg/titletoc
\usepackage{xparse}% http://ctan.org/pkg/xparse

\NewDocumentCommand{\rtocstuff}{O{20pt} m O{150pt}}{% \rtocstuff[<gap>]{<content>}[<width>]
\titlecontents{chapter}
[0pt]% left margin indent
{\bigskip\bfseries}% chapter ToC formatting
{\makebox[1.5em][l]{\thecontentslabel}}% chapter label (numbered)
{\hspace*{1.5em}}% chapter label (unnumbered)
{\titlerule*[1pc]{.}% dotted contents line
\thecontentspage% ToC page number
\hspace*{#1}% gap between page number & <content>
\smash{% remove vertical height of image
\raisebox{1.5ex}{% align with top of character
\hspace*{#3}% space for <content>
\llap{% left overlap
#2% actual <content>
}}}}%
\titlecontents{section}
[0pt]% left margin indent
{\normalfont}% section ToC formatting
{\hspace*{1.5em}\makebox[2.3em][l]{\thecontentslabel}}% section label (numbered)
{\hspace*{3.8em}}% section label (unnumbered)
{\titlerule*[1pc]{.}% dotted contents line
\thecontentspage% ToC page number
\hspace*{#1}% gap between page number & <content>
\hspace*{#3}% gap for <content>
}
}
\NewDocumentCommand{\ltocstuff}{O{150pt} m O{20pt}}{% \ltocstuff[<width>]{<content>}[<gap>]
\titlecontents{chapter}
[0pt]% left margin indent
{\bigskip\bfseries}% chapter ToC formatting
{\smash{% remove vertical height of image
\raisebox{1.5ex}{% align with top of character
\rlap{% right overlap
#2% actual content
}\hspace*{#1}% space for <content>
}}%
\hspace*{#3}% gap between <content> and ToC entries
\makebox[1.5em][l]{\thecontentslabel}}% chapter label (numbered)
{\smash{% remove vertical height of image
\raisebox{1.5ex}{% align with top of character
\rlap{% right overlap
#2% actual <content>
}\hspace*{#1}% space for <content>
}}%
\hspace*{1.5em}}% chapter label (unnumbered)
{\titlerule*[1pc]{.}% dotted contents line
\thecontentspage% ToC page number
}
\titlecontents{section}
[0pt]% left margin indent
{\normalfont}% section ToC formatting
{\hspace*{#1}% space for <content>
\hspace*{#3}% gap between <content> and ToC entries
\hspace*{1.5em}\makebox[2.3em][l]{\thecontentslabel}}% section label (numbered)
{\hspace*{3.8em}}% section label (unnumbered)
{\titlerule*[1pc]{.}% dotted contents line
\thecontentspage% ToC page number
}
}

\usepackage{lipsum}% http://ctan.org/pkg/lipsum

\begin{document}

\contentsmargin{0pt}% Remove right margin in ToC
\tableofcontents

\rtocstuff{\includegraphics[valign=T,width=75pt]{tiger}}
\chapter{Coordinates of points}

\section{Rectangular coordinates} \lipsum[1]
\section{Projections of a segment on the axes} \lipsum[2]
\section{Distance between two points} \lipsum[3]
\section{The mid-point of a segment} \lipsum[4]
\section{Division of a segment in any ratio} \lipsum[5]
\section{Oblique coordinates} \lipsum[6]

\ltocstuff{\includegraphics[valign=T,width=75pt]{tiger}}
\chapter{The locus of an equation}

\section{First illustrations} \lipsum[1]
\section{Curve plotting} \lipsum[2]
\section{Test that a point lie on a curve} \lipsum[3]
\section{Intercepts} \lipsum[4]
\section{Points of intersection of two curves} \lipsum[5]
\section{Oblique coordinates} \lipsum[6]

\rtocstuff{\includegraphics[valign=T,width=75pt]{tiger}}
\chapter{The straight line}

\section{Equation in terms of point and slope} \lipsum[1]
\section{Line through two points} \lipsum[2]
\section{The general equation of first degree} \lipsum[3]
\section{Parallel and perpendicular lines} \lipsum[4]
\section{Angle between two lines} \lipsum[5]
\section{Distance from a point to a line} \lipsum[6]

\end{document}


Finally, since this example has a duplicate tiger at each chapter, you could also automate this using some help from the etoolbox package, by patching \chapter:

\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
...
\preto{\chapter}{%
\ifodd\thechapter
\ltocstuff{\includegraphics[valign=T,width=75pt]{tiger}}
\else
\rtocstuff{\includegraphics[valign=T,width=75pt]{tiger}}
\fi
}


The above addition produces the same output as before. However, you might not be interested in such an implementation, since the images would ideally like to change from one chapter to the next, to showcase some conceptual idea conveyed in that particular chapter.

You'll also notice that I've completely redefined the chapter and section formatting using the notation provided by titlesec. For example, the 1pc dotted contents line. This, as always, can be modified to match the regular book document class more closely.

-
Absolutely fantastic! I was wondering how to modify the ToC in order to include the images, and you made a very impressive, didatic and detailed solution. Thanks a lot, Werner! Hats off to you! :-) – Paulo Cereda Oct 14 '11 at 11:36
A couple of bugs I found while trying to apply this to this question: 1) Long titles do not wrap well. 2) If there are not enough sections to fill a full image, then the image will overlap with the following text. The only solution I have found is to the latter is to stuff everything into a single box. It would be nice if there was less brutal solution though. – mforbes May 8 '12 at 20:26
Is there any way to implement this tex.stackexchange.com/questions/95020/… – manish Jan 24 '13 at 3:02
I found this example to be very useful but i need to use it for a5 article. Can i use it? – manish Jan 25 '13 at 1:15
@user15662: There should be no restriction in terms of its usage on other page sizes. Haven't tested it though. – Werner Jan 25 '13 at 2:20