I am trying to use work from this answer and this answer to create something that looks similar to the following:

However, I would like to be able to actually link to the sections in the document from the pictures in the TOC (that is, the pictures underneath the sections in the TOC should have red boundary boxes just like the other sample pictures, indicating an active link). The code I attempted to use was as follows:

\documentclass{article}
\usepackage{graphicx}
\usepackage{lipsum}
\begin{document}

\tableofcontents

\hyperref[sec:C]{\includegraphics[height=5\baselineskip]{example-image-c}}

\section{Section A}\label{sec:A}
\lipsum[2]

\section{Section B}\label{sec:B}
\lipsum[2]

\section{Section C}\label{sec:C}
\lipsum[2]
\end{document}


The attempt for Section C (trying to use the work from this answer) is where the error is thrown:

! Use of \\addstufftotoc doesn't match its definition.

Is there a way to modify the definition of addstufftotoc or some other method to produce my desired result (i.e., have pictures in the TOC that link to sections in the document)?

• \hyperref[sec:C]{\protect\includegraphics{...}} is a possible solution – user31729 Mar 17 '16 at 16:25

The \includegraphics command has to be protected for \hyperref as well, that's all.

Use

\addstufftotoc{\nobreak\smallskip\protect\hyperref[sec:C]{\protect\includegraphics[height=3\baselineskip]{example-image-c}}\par}


to achieve this.

\documentclass{article}
\usepackage{graphicx}
\usepackage{lipsum}
}
\begin{document}

\tableofcontents

\hyperref[sec:C]{\includegraphics[height=5\baselineskip]{example-image-c}}

\section{Section A}\label{sec:A}
\lipsum[2]

\section{Section B}\label{sec:B}
\lipsum[2]

\section{Section C}\label{sec:C}
\lipsum[2]
\end{document}


Some explanations:

\includegraphics is defined as

\def\includegraphics{%
\@ifstar
{\Gin@cliptrue\Gin@i}%
{\Gin@clipfalse\Gin@i}}


i.e. the command itself has no arguments, but leaves it to internal commands as \Gin@i etc. which itself have optional arguments -- this is fragile and \hyperref{\includegraphics...} breaks apart, not seeing the full list of arguments then. In order to prevent this, \protect must be used.

Another way is to use the \robustify command from the etoolbox package and saying \robustiy{\includegraphics} in the preamble of the document -- this will make \includegraphics robust and \protect isn't needed any longer (for \includegraphics)

• Thanks a lot--that's exactly what I needed. Anything I could read to give more explanation behind your answer (i.e., what needs to be protected and all that)? Or is that an overly general question (i.e., read the hyperref package, look at the TeXbook, etc.)? – Daniel W. Farlow Mar 17 '16 at 17:09
• @DanielW.Farlow: I'll add an explanation – user31729 Mar 17 '16 at 17:12
• @DanielW.Farlow: See the update, please – user31729 Mar 17 '16 at 17:16
• That helps a bit. Thanks for the added explanation! – Daniel W. Farlow Mar 17 '16 at 17:20
• @DanielW.Farlow: Only a bit? ;-) There's a general rule: If a command has moving arguments (i.e. the command itself has no arguments, but calls sub commands that treat the argument) this command is fragile then and needs \protect as soon it is nested into the argument of another command. This is true for many commands and one has to know, which commands have moving arguments. For example, \chapter, \section etc. are usual suspects, but \includegraphics as well. – user31729 Mar 17 '16 at 17:24