1

This refers to my previous question.

How to put image in table of contents?

How can i add mdframed after toc on the same page?

enter image description here

Regards

0

1 Answer 1

2

Here's one possible solution, patching the \tableofcontents command (I used the code provided by Werner in the question linked); of course, use the desired options for mdframed in the \mdframedintoc command:

\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]
Some contents for the \texttt{mdframed} environment.
\end{mdframed}\par\bigskip
\begin{mdframed}% mdframed for the image
\centering
\includegraphics[height=2\baselineskip]{example-image-a}
\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}
\section{Last section}
\addstufftotoc{\nobreak\smallskip\protect\includegraphics[height=2\baselineskip]{example-image-c}\par}

\end{document}

enter image description here

7
  • Thanks, can i add one more mdframed with image inside after this mdframed on the same page.
    – manish
    Commented Mar 29, 2013 at 2:37
  • @user15662 please see my updated answer. Commented Mar 29, 2013 at 2:44
  • Thanks for quick reply. I have one last query, How can i move word "Contents" up so that we can see more spacing between Contents and TOC. I found top margin is more on this page. How can i control it.
    – manish
    Commented Mar 29, 2013 at 3:00
  • 1
    @user15662 You can add to the preamble something like \patchcmd{\tableofcontents}{\section*}{\vspace*{-80pt}\section*}{}{} change -80pt according to your needs. Commented Mar 29, 2013 at 3:05
  • Thanks for your reply. I m using \addtocontents{toc}{\protect\contentsline{section}{Image is not present}{}} for writing some text if image is missing but i see space is more between this text and Fourth section. How can i reduce spacing?
    – manish
    Commented Mar 29, 2013 at 4:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .