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

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

October 14Hits:1
Advertisement

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} 


The generated table of contents is as expected:

Now, this book has a table of contents similar to this one:

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? :-)

## Answers

Here is a first stab at producing this layout in the Table of Contents.

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[export]{adjustbox}% http://ctan.org/pkg/adjustbox
\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.

## Related Articles

• ### Adding a per-chapter image along with group of entries in ToCOctober 14

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]{in

• ### Added chapter with addcontentsline does not adapt style of ToCAugust 20

I added my BibTeX references following: \cleardoublepage \phantomsection \nocite{*} \bibliographystyle{plainnat} %\setlength{\bibsep}{3mm} \addcontentsline{toc}{chapter}{References} \bibliography{bibliography} but within my ToC the style is not adapt

• ### adding the apache user to every new groupJune 4

Hi I am fairly new to ispconfig. We are looking at using it on our new web server. My colleagues had a look and we had certain ideas on how to improve security. The solution we came up with (it's not ours!) is based on this http://lists.evolt.org/arc

• ### Grouping Together Sections in TOCJanuary 10

I am reviewing a series of documents and have created a report that looks like this: \chapter{Interesting Things} \input{thing1.tex} \input{thing2.tex} \input{thing3.tex} where the thing*.tex each look like: \section{The Name of The Thing} \subsectio

• ### pgfplots: "group" legend entries?July 21

Actually, I'm working on a study where I've to plot a lot of temperature data etc. Therefore, I use "pgfplots". Everything works pretty well, so that I get the result as shown in the attached picture . by using the following code: \documentclass

• ### Adding Network Service Account to a domain groupJuly 8

How would I add the Network Service account of a machine to a domain (controller) group? --------------Solutions------------- Network Service authenticates with the computer's domain account, so you might be able to achieve this by adding the compute

• ### Adding a friend back into a Facebook group who has previously leftMarch 31

A friend left a Facebook group and now he wants to rejoin the group. When I try to add him, Facebook tells me that "Mr.X has already left the group". How do I add him back in again? --------------Solutions------------- You'll have to ask the fri

• ### DIY Fusion Drive: Adding Recovery HD to a CoreStorage Volume GroupNovember 3

After creating a "DIY Fusion Drive" using the methods below in 10.8, I'm now wondering how to add a RecoveryHD partition to the internal drive. Here's what I did to create a Logical Volume Group across two physical volumes (128G SSD/750GHD) (mak

• ### Adding a User to an existing SharePoint GroupSeptember 18

I am trying to create a script to add a user to an existing SharePoint group. I'm new to PowerShell Scripting & SharePoint, so I'm a little lost. I keep getting the error that the specified group does not exist, even though I'm looking at the created

• ### Adding the domain account to a security group on the SQL Server computer that has sufficient privileges to log on as a serviceOctober 20

After reading this article, http://www.red-gate.com/supportcenter/content/knowledgebase/SQL_Backup/KB200710000173 I have some problems configuring point 2) and 3): 2) Create a SQL Server login that has the ability to backup (and restore) databases (y

• ### Adding a secondary route non-stackable switch groupJanuary 30

I believe I am missing something obvious. I am using GNS3 to lab out having two paths to a CORE network. See picture. The configuration works as I imagined, I can ping from each Switch every other Switch within VLAN212. I can also ping the loopbacks

• ### Adding an existing user to an existing group isn't working with usermod -a -G June 2

This question already has an answer here: Adding user to a group - Why had to reboot? 2 answers I'm attempting to add myself to a group vboxshare such that my user can access a virtual box shared directory under /media. Seems easy enough. A quick goo

• ### adding media content to a Drupal Commons Group page header?January 4

I've been playing with Drupal for a bit, and have gotten used to the idea of Content and fields. I am now trying to use Drupal Commons 7 to up my game some and improve how my site looks. Im a bit stumped however on how I can edit the look of my Group

• ### Adding a Pseudo Chapter with (!) Page NumberSeptember 15

I want to manually add a chapter to the ToC and add a specific number to the page counter. This is meant for adding to my document a number of papers, which are printed out elsewhere and which I cannot add into the LaTeX file. I tried this, \newcomma

• ### Adding a user to the local Administrator group using powershellMarch 8

I would like to use PowerShell to add a specific user to the local administrator group on a machine. I would be running the PowerShell script in the context of a user that has Administration rights on the local machine. --------------Solutions-------

• ### Adding the apache user to my default group doesn't give apache group accessMarch 1

The whole Linux user:group topic just gives me headaches again. Situation I have my document roots (httpdocs) and everything inside chown-ed to a certain user and a certain group, let's say myuser:mygroup. chown -R myuser:mygroup httpdocs Now apache

• ### Adding a string "Chapter" to selected chapter numbers in TOCSeptember 21

Any idea how to add a string "Chapter" to TOC to a selected positions in TOC? For now, TOC looks like this: Chapter without any number A Chapter without any number B 1. This is chapter 1. 2. This is chapter 2. 3. This is chapter 3. 4. This is ch

• ### Adding a partner portal user to Chatter groupJanuary 16

I don't think that this question has been asked previously on SFSE. Assuming I am migrate to partner Communities from Partner Portal, is it possible to add a partner portal user to my private group in Chatter? Scenario: I have a range of partner port

• ### Adding a system user to an LDAP group with SSSDFebruary 27

Our LDAP server is running RFC 2307 groups (memberuid contains a username, not a DN). With our old nscd/nss_ldap/pam_ldap setup, you could list a non-LDAP user (a system user from /etc/passwd) in an LDAP group's memberuid attribute, and that system u

• ### Help required in adding Domain Users to Local Powers Users groupApril 6

Hi all, Ubuntu 8.04 Server 64-bit edition Samba 3.0.28a configured as PDC I have NO WINDOWS NT MACHINE ON THE NETWORK. WinXP clients login to the samba domain and access their home directories as configured. I am following the instructions in http://

Copyright (C) 2017 ceus-now.com, All Rights Reserved. webmaster#ceus-now.com 14 q. 0.928 s.