# Use Tikz-made icons in TOC

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, rather than a section number.

Presently, I'm using the suggestions I received in this question. However, the .png images are hard to work with and always turn out fuzzy. I'd like to try and use TikZ to make these images so they will stay sharp at any size.

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. These icons would be very basic: just a circle with a letter inside. I want to use TikZ for this to avoid having to make these icons as images.

• Er... you haven't 'provided an example' at all, but it would be much easier to help if you did. This is unanswerable as it stands: any answer is going to have to make assumptions about your document which will likely turn out to be false. Hence, answering is currently pretty much a waste of time: it might be lucky - and, of course, it might help somebody else - but it is unlikely to help you. So, from your perspective, you are likely to end up with few actually helpful responses. Also, why on earth do you want to use TikZ for this? Complete overkill...
– cfr
Feb 15, 2016 at 2:40
• And... what have you tried? There are lots of questions about customising ToCs. tocloft is popular. etoc can do pretty much anything. Of course, it depends on your class... config ... ... ...
– cfr
Feb 15, 2016 at 2:41
• The solution here worked perfectly well: [tex.stackexchange.com/questions/209149/…, however dealing with the icons and editing them/resizing them was a real pain. I think it would be much easier to use simple Tikz drawings for this purpose. Feb 15, 2016 at 11:20
• But you thought you'd ask essentially the same question here without even bothering to refer people to that answer in case it might be helpful? Not only do you not bother to provide an MWE, you don't even bother to link to one which was written for you. You decided anybody who wanted to help you could just start from scratch again? Because, hey, I'm sure people love duplicating other people's efforts for no reason. You can easily substitute TikZ in place of \includegraphics. Just use the TikZ code or save the TikZ picture in a box and then use the box.
– cfr
Feb 15, 2016 at 23:25
• So, that isn't very helpful. And I'm sorry if I didn't provide the link, but as I was editing the post originally, stackexchange went into read only mode and I was unable to update it. In any case, I'd don't know how the easy substitution of tikz can be made here. It doesn't seem straightforward to me. But then I am not a tex expert. Feb 16, 2016 at 10:28

In general, using the package tikzscale alters the \includegraphics command to be able to read and typeset tikz-graphics. Therefor you could do something like

• create your images with tikz
• save each image in an individual file
• use the code of the linked answer and load the tikzscale package
• use \includegraphics[height=10pt]{mytikzimage} at the appropriate places.

This approach has the additional bonus that tikzscale automatically scales the tikz-images to an absolute size, which I believe is crucial for this idea? You might want to look into tikz's externalize library though, because tikzscale typesets each image a couple of times to achieve this scaling. otherwise, using several icons in your ToC might slow down the compilation of your document significantly.

See the following code I borrowed from here, which you already linked:

\documentclass{book}
\usepackage{titletoc}
\usepackage{graphicx}
\usepackage{tikz}      % to draw the symbols
\usepackage{tikzscale} % to include tikz graphics as image files and scale them

\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}}\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}


Here, I used the additional file image.tikz with the following content:

% image.tikz
\begin{tikzpicture}
\draw [fill=red!30] (-1,0) -- (0,1) -- (1,0) -- (0,-1) -- cycle;
\end{tikzpicture}


As the screenshot below shows, this works like a charm:

For sake of completeness: If you want individual pictures for individual sections, you have to change the code inside \iconsectioninToC (this is not my brain working, copied from the linked answer). Change the commented part to:

\stepcounter{mysecimage}
\smash{\includegraphics[height=10pt]{image-\the\value{mysecimage}}}\hspace{0.5em}


and name your images image-1.tikz, image-2.tikz and so on.

• If I want a unique icon for each section, however, I will have to figure out how to reference them individually in the image.tikz file. Is there some way to do that? Thanks, @Dux, this is so helpful. Mar 8, 2016 at 19:15
• I'm imagining code like this: \begin{tikzpicture} \draw (0,0) circle [radius=0.4] node {$A$}; \end{tikzpicture} Then I would use some unique text in each icon to replace "A". Mar 8, 2016 at 19:43
• This, too, is explained in the answer you already linked. I thought your problem was adding tikz to the picture, which I did with this answer...
– Dux
Mar 9, 2016 at 9:05
• I edited my answer accordingly.
– Dux
Mar 9, 2016 at 9:15