3

This refers to my previous question How to put image in table of contents?

 \documentclass{article}
 \usepackage{etoolbox,graphicx}% http://ctan.org/pkg/{etoolbox,graphicx}
 \usepackage{xcolor}
 \usepackage{mdframed}

\definecolor{myblue}{RGB}{146,243,224}

\newcommand{\addstufftotoc}[2][toc]{% \addimagetotoc[<toc>]{<stuff>}
 \addtocontents{#1}{#2}}

\makeatletter
\patchcmd{\l@section}% <cmd>
{\begingroup}% <search>
{\begingroup\normalfont\Large\bfseries}% <replace>
 {}{}% <success><failure>

\newcommand\mdframedintoc{\par\bigskip%
\begin{mdframed}[hidealllines=true,backgroundcolor=myblue]
\includegraphics[width=0.5\textwidth]{mypicture.png} 
 \end{mdframed}%
}
\patchcmd{\tableofcontents}{\@starttoc{toc}}{\@starttoc{toc}\mdframedintoc}{}{}
\makeatother


 \begin{document}

 \tableofcontents
 \section{First section}
 \addstufftotoc{\nobreak\smallskip\protect\includegraphics[height=2    \baselineskip]{example-image-a}\par}
 \section{Second section}
 \addstufftotoc{\nobreak\smallskip\protect\includegraphics[height=2     \baselineskip]{example-image-b}\par}
 \section{Third section}
 \section*{Fourth section}
 \addcontentsline{toc}{section}{\protect\numberline{}Fourth section}
 \addtocontents{toc}{\protect\contentsline{section}{Section Name}{}}
 \section{Last section}
 \addstufftotoc{\nobreak\smallskip\protect\includegraphics[height=2  \baselineskip]{example-image-c}\par}

 \end{document}

enter image description here

How can I control the spacing between Contents and First Section (Increase) and between fourth section and Section Name (Reduce)?

manish
  • 9,111

1 Answers1

2

At the appropriate location within your document, use the \addstufftotoc macro to insert vertical space in the ToC.

To add/reduce space, use

\addstufftotoc{\vspace*{<len>}}

where <len> is positive [negative] to add [remove] space.

Werner
  • 603,163