7

enter image description here

This image provides a rough approximation of something I'd like to do in my book's ToC. I would like the sections within only one of my chapters to utilize a unique icon. For the sake of time, I've only provided an example for the first section. However, I'd like a unique icon to be used for each of the sections within one chapter. It is fine if the section numbers are retained; I just need the image to appear before the text of the section title.

I'm lost about how to go about this. Any help is appreciated.

(I am using the book class.)

  • Are you a bundle of pictures (one for each section)? Is it only a icon for all sections? is it necessary to make it using some graphic package as tikz or ...? – skpblack Oct 26 '14 at 14:46
  • So the icon is not supposed to occur in the section heading itself (i.e. on page 3, in the example)? – Hagen von Eitzen Oct 26 '14 at 16:54
12

Here's one way to do it using the titletoc package.

Use \iconsectioninToC just before the \chapter(s) in which you want to add the icon in the ToC; return to the standard formatting using \stdsectioninToC.

In the definition of \iconsectioninToC change the settings for the \includegraphics command and adjust the lengths to suit your needs.

\documentclass{book}
\usepackage{titletoc}
\usepackage{graphicx}

\makeatletter
\newcommand\stdsectioninToC{
\titlecontents{section}
  [3.8em]
  {}
  {\contentslabel{2.3em}}
  {\hspace*{-2.3em}}
  {\titlerule*[1em]{.}\contentspage}
}
\newcommand\iconsectioninToC{
\titlecontents{section}
  [3.8em]
  {}
  {\contentslabel{2.3em}%
    \smash{\includegraphics[height=10pt]{image-5}}\hspace{0.5em}% change here 
  }
  {\hspace*{-2.3em}}
  {\titlerule*[1em]{.}\contentspage}
}
\AtBeginDocument{\stdsectioninToC}
\makeatother

\begin{document}

\tableofcontents

\chapter{A test chapter}
\section{First test section}
\section{Second test section}
\section{Third test section}
\section{Fourth test section}

\iconsectioninToC
\chapter{A test chapter}
\section{First test section}
\section{Second test section}
\section{Third test section}
\section{Fourth test section}

\stdsectioninToC
\chapter{A test chapter}
\section{First test section}
\section{Second test section}
\section{Third test section}
\section{Fourth test section}

\end{document}

The resulting ToC:

enter image description here If each section entry has to receive its own image, you can use this simply variation, which assumes your image files are named image-1, image-2, image-3, etc.:

\documentclass{book}
\usepackage{titletoc}
\usepackage{graphicx}

\newcounter{mysecimage}

\makeatletter
\newcommand\stdsectioninToC{
\titlecontents{section}
  [3.8em]
  {}
  {\contentslabel{2.3em}}
  {\hspace*{-2.3em}}
  {\titlerule*[1em]{.}\contentspage}
}
\newcommand\iconsectioninToC{
\titlecontents{section}
  [3.8em]
  {}
  {\contentslabel{2.3em}%
    \stepcounter{mysecimage}%
    \smash{\includegraphics[height=10pt]{image-\the\value{mysecimage}}}\hspace{0.5em}% change here 
  }
  {\hspace*{-2.3em}}
  {\titlerule*[1em]{.}\contentspage}
}
\AtBeginDocument{\stdsectioninToC}
\makeatother

\begin{document}

\tableofcontents

\chapter{A test chapter}
\section{First test section}
\section{Second test section}
\section{Third test section}
\section{Fourth test section}

\iconsectioninToC
\chapter{A test chapter}
\section{First test section}
\section{Second test section}
\section{Third test section}
\section{Fourth test section}

\stdsectioninToC
\chapter{A test chapter}
\section{First test section}
\section{Second test section}
\section{Third test section}
\section{Fourth test section}

\end{document}

enter image description here

  • 1
    Wow. Gonzalo, that is EXACTLY what I am after. However, I want to use a unique image for each of the sections. Is such a thing possible? – Linter Oct 26 '14 at 16:42
  • 1
    @LarryTate sure, it's possible. Please see my updated answer. – Gonzalo Medina Oct 26 '14 at 16:59
  • 1
    Wow. Thanks. I would have never figured that out. Thanks for your help, Gonzalo. – Linter Oct 26 '14 at 17:33
  • 1
    @LarryTate You're welcome! Glad I could help. – Gonzalo Medina Oct 26 '14 at 17:35
  • I'm not good enough to understand all that is going on in your code here. What is the purpose of the \stdsectioninToC command? Hopefully, I can get this to work in my book. – Linter Oct 26 '14 at 19:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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