This works:
Based on the answer https://tex.stackexchange.com/a/14964/36296, I put together a MWE which might look ugly, but works fine. At any place during the document I can change the picture to be inserted for next section with my command \setsectocdeco
.
\documentclass{book}
\usepackage{graphicx}
\usepackage{etoc}
\usepackage{mwe}
\etocsetstyle{chapter}
{}
{}
{%
\sectocdeco%
\etocname%
\par%
}
{}
\DeclareRobustCommand{\sectocdeco}{}
\newcommand*{\setsectocdeco}[1]{%
\addtocontents{toc}{\protect\renewcommand{\sectocdeco}{\protect #1}}%
}
\setsectocdeco{\includegraphics[height=1cm]{example-image-a}}
\begin{document}
\tableofcontents
\chapter{Chapter 1}
\setsectocdeco{\includegraphics[height=1cm]{example-image-b}}
\chapter{Chapter 2}
\end{document}
This does not:
However if I put these commands into my real ToC (which is based on https://tex.stackexchange.com/a/163707/36296), the image will not be updated:
\documentclass{book}
\usepackage{xcolor}
\usepackage{framed}
\usepackage{etoc}
\usepackage{mwe}
\usepackage[linktoc=all, colorlinks=true, linkcolor=black]{hyperref}
\colorlet{toccolour}{blue}
\newlength{\tocmargin}
\newenvironment{tocleftbar}
{\def\FrameCommand {%
\hspace{\dimexpr\tocmargin+40pt\relax}%
{\color{toccolour}\vrule width 2pt depth 6pt}\hspace{6pt}%
\hspace{-\tocmargin}%
}%
\MakeFramed {\leftskip\tocmargin\rightskip\tocmargin\FrameRestore}%
}
{\endMakeFramed}
\newif\ifintocleftbar
\etocsetstyle{chapter}
{\nobreak\etocskipfirstprefix}% don't break immediately after Part
{\pagebreak[2]}% but allow break after section or subsection
{\global\let\ETOCthepage\etocthepage%
\global\let\ETOCthename\etocthename%
\global\let\ETOCthenumber\etocthenumber%
\global\let\ETOCthelink\etocthelink%
\ifintocleftbar\end{tocleftbar}\fi%
\kern\baselineskip%
\begin{tocleftbar}
\intocleftbartrue
\makebox[0pt][r]{%
\null\hfill%
\ETOCthelink{%
\raisebox{-1.5cm}{%
\sectocdeco%
\hspace*{18pt}%
}}}%
\parbox[t]{\dimexpr\hsize-2\tocmargin\relax}{%
\LARGE\par%
\ETOCthelink{\ETOCthename\hspace{0.5em}\dotfill\hspace{0.5em} \nobreak{\etocpage}}%
\rule[-\baselineskip]{0pt}{0pt}%
}%
\par\normalsize\leftskip\dimexpr\leftskip+3em\relax
}
{\ifintocleftbar\end{tocleftbar}\fi
}
\DeclareRobustCommand{\sectocdeco}{}
\newcommand*{\setsectocdeco}[1]{%
\addtocontents{toc}{\protect\renewcommand{\sectocdeco}{\protect#1}}%
}
\setsectocdeco{\includegraphics[height=1cm]{example-image-a}}
\begin{document}
\tableofcontents
\chapter{Chapter 1}
\setsectocdeco{\includegraphics[height=1cm]{example-image-b}}
\chapter{Chapter 2}
\end{document}
So long story short: why is the image in the second example not changed?
Use case:
The solutions do not need to be based on the above code. I am looking for something to convert
\documentclass{book}
\usepackage{graphicx}
\usepackage{titlesec}
\titleformat{\chapter}[display]%
{}%
{\secdeco}%
{1em}%
{}%
[]
\titleformat{name=\chapter,numberless}[display]
{}%
{\secdeco}%
{1em}%
{}%
[]
\newcommand{\secdeco}{\includegraphics[height=1cm]{example-image-a}}
\begin{document}
\tableofcontents
\addtocounter{page}{6}
\chapter{Name of First Chapter}
\section{Another test section}
\section{Another test section}
\subsection{A test subsection}
\section{Another test section}
\subsection{A test subsection}
\subsection{A test subsection with a long title spanning more than one line in the table of contents}
\renewcommand{\secdeco}{\includegraphics[height=1cm, width=0.5cm]{example-image-b}}
\chapter{Name of Second Chapter}
\section{A test section with a long title spanning more than one line in the table of contents}
\subsection{A test subsection}
\subsection{Another test subsection}
\section{A test section}
\section{Another test section}
\subsection{A test subsection}
\section{Another test section}
\subsection{A test subsection}
\renewcommand{\secdeco}{\includegraphics[height=1cm]{example-image-a}}
\chapter{Chapter without Sections}
\chapter*{Unnumbered Chapter}
\addcontentsline{toc}{chapter}{Unnumbered Chapter}
\end{document}
into a ToC which should look like this:
The main goals are:
- For each chapter there should be a rule which is as long as the ToC of this chapter.
- In case the the height of the image is larger than the space of the ToC of an chapter, the rule should be as long as the image (only happens for chapters without sections)
- All the images have the same height, but may be of different width. It would be nice if the would be right aligned.
- the solution should be compatible with
\usepackage{titlesec}
(I apologize, this now sounds pretty much like "do it for me", but I thought this would give more opportunities for possible solutions instead of trying to fix the code above)