# Problem to insert different pictures in ToC

I am using titletoc package to format my ToC. I would like my table of contents to be like that shown in the next figure

I don't know if it's possible to achieve this ToC style in LaTeX, but inserting a figure in ToC is undoubtedly the most difficult part of this work. So I have tried to do the trick suggested by titletoc documentation defining

\newcommand{\figureintoc}[1]{
\begin{figure}
\includegraphics{#1}%
\end{figure}}


But I am getting this error message when I compile:

Runaway argument? { ! Paragraph ended before \@writefile was complete. \par

l.11 ...le{\figureintoc {pitagoras.png}}{\par }

I don't know what to do now. Here is my MWE:

\documentclass[openright]{book}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{geometry}
\usepackage{xparse}
\usepackage{graphicx}
\usepackage[explicit]{titlesec}
\usepackage[many]{tcolorbox}
\usepackage{emptypage}
\usepackage{lipsum}
\usepackage{lmodern}
\usepackage{titletoc,titleps}

\titlecontents{part}%
[0pt]
{}
{\MakeUppercase}
{\hfill\thecontentspage}

\titlecontents{chapter}
[0pt]
{Chapter~\thecontentslabel.\hspace{1em}\MakeUppercase}
{}
{\hfill\thecontentspage}

\titlecontents{section}
[2em]
{}
{\textbf{\thecontentslabel}\hspace{1em}}
{\hspace*{4em}\bfseries\itshape}
{\hfill\thecontentspage}

\titlecontents*{subsection}
[1.0cm]
{}
{%
{\textbf{\thecontentslabel}\hspace{1em}}%
}%
{\hspace*{4em}\bfseries\itshape}
{\hfill\thecontentspage}

\setcounter{secnumdepth}{3}

\newcommand{\figureintoc}[1]{
\begin{figure}
\includegraphics{#1}%
\end{figure}}

\begin{document}
\tableofcontents

\chapter{Foo chapter}

\lipsum[1]

\section{Foo section}

\lipsum[2]

\section{Foofoo section}

\lipsum[3]
\end{document}


I hope you can help me! I will really appreciate any help you can provide.

Here is a very different approach because the code in https://tex.stackexchange.com/a/330885/36296 had very different constraints. Or I just became wiser and less obsessed with framed.sty.

This is second version: the first one used a multiline \parshape but it is hard then to take into account long titles over multiple lines; it is simpler to do each time a \par and use \prevgraf to check if we have reached bottom of picture. Thus the new version uses \parshape with n=1 line specification.

edit: well finally I have dropped entirely the \parshape thing and use only adjustments of \leftskip. I also take this opportunity to simply the code for chapter titles, because it came from a context where \parskip was positive and not to be modified, hence looked unnecessarily weird. And, I add use of package needspace to make sure there is enough room at bottom of page for graphics. The present code will easily left the chapter title at bottom of page, if one wants it to stick to the graphics below it, then it is possible but one needs to move some code from the section style to the chapter style, taking into account that in the example the \setsectocdeco commands are already used before \chapter. But with the present code, they may as well be used after the \chapter as we don't worry about glueing the chapter title to the graphics.

Also I have added the better handling of section numbers and page numbers so that long titles do not go underneath them on subsequent lines (this is matter of taste). And the last chapter has no associated graphics to show it does work in that case too.

note: in the image below the jpeg decorating the second chapter has some whitespace all around the square, which explains why it looks shifted to the right and to the bottom. This comes from the jpeg not from the LaTeX code.

% Image credits:

% Par Mallowtek — Travail personnel, CC BY-SA 3.0,
% https://commons.wikimedia.org/w/index.php?curid=20438883

% Par Jean-Luc W — Travail personnel, CC BY-SA 3.0,
% https://commons.wikimedia.org/w/index.php?curid=10002415

\documentclass[a4paper]{book}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{geometry}
\usepackage{graphicx}
%\usepackage{xcolor}
\usepackage{etoc}
\usepackage{mwe}
\usepackage{needspace}

\newbox\TOCbox

\makeatletter

\etocsetstyle{chapter}
{\sffamily\parskip\z@skip\parindent\z@}
{\allowbreak
\rule{\linewidth}{1pt}\par
\nobreak Chapter \etocnumber. \MakeUppercase{\etocname}\par
\nobreak
\rule[\dimexpr.4\baselineskip-1pt\relax]{\linewidth}{1pt}\par
}
{}
{}

% loop facilities not needed after all
% \usepackage{xinttools}

\def\TOCleftsep{1.25cm}
%\def\TOCrightsep{1cm}
\def\TOCnumwidth{0.75cm}
\def\TOCpnumwidth{0.5cm}

\etocsetstyle {section}
{\leftskip\z@skip\parfillskip\z@skip
\rightskip\TOCpnumwidth\relax
{\Gin@drafttrue\setbox\TOCbox\hbox{\sectocdeco}%
\@tempcnta\ht\TOCbox
\@tempcntb\baselineskip
\divide\@tempcnta\@tempcntb
\needspace{\@tempcnta\baselineskip}%
\xdef\TOC@shiftedlines{\the\@tempcnta}%
\dimen@\wd\TOCbox
\ifdim\dimexpr\ht\TOCbox+\dp\TOCbox=\z@
\gdef\TOC@shiftedlines{0}%
\else
\xdef\TOC@leftskip{\the\dimen@}%
\fi }%
}
{\ifnum\TOC@shiftedlines>\z@
\leftskip\TOC@leftskip\relax
\etociffirst
{\leavevmode\llap{{\raisebox{\dimexpr-\height+\ht\strutbox\relax}
[\ht\strutbox][\z@]{\sectocdeco}}\hspace{\TOCleftsep}}}{}%
\else
\leftskip\TOCnumwidth\relax
\fi
\leavevmode
\llap{\makebox[\TOCnumwidth][l]{\etocnumber}}%
\etocname\nobreak\hfill\rlap{\makebox[\rightskip][r]{\etocpage}}%
\par}
{\edef\TOC@shiftedlines{\the\numexpr\TOC@shiftedlines-\prevgraf}}
{\leftskip\z@skip\rightskip\z@skip\parfillskip\@flushglue
\count@\TOC@shiftedlines\relax

\makeatother

\newcommand*{\setsectocdeco}[1]%

%\renewcommand{\etocaftertochook}{\thispagestyle{empty}}

\begin{document}

\tableofcontents

\setsectocdeco{\includegraphics[height=2cm]{Pythagore_1}}

\chapter{Foo}

\section{Foo}
\section{This is a section with a long long long long long long long long long long long long long long long long name}

\setsectocdeco{\includegraphics[height=3cm]{Pythagore_2}}

\chapter{Bar}

\section{Foo}
\section{Foofoo}
\section{Foo}
\section{Foofoo}
\section{This is a section with a long long long long long long long long long long long long long long long long name}
\section{Foo}
\section{Foofoo}
\section{Foo}
\section{Foofoo}

% test of needspace
%\setsectocdeco{\includegraphics[height=5cm]{example-image-c}}

% test with empty decor
\setsectocdeco{}

\chapter{FooBar}
\section{Foo}
\section{Foofoo}
\section{Foo}
\section{Foofoo}
\section{Foo}
\section{Foofoo}

\end{document}


• the setsectocdeco commands can be used after the \chapter, they just need to be located before the first \section following a \chapter. – user4686 Oct 21 '16 at 9:04