7

My Question is similar to question which has not been answered. How can i put image in index page to improve look and feel.

How to generate index page for report?

enter image description here

3
  • 3
    I think you don't need this message but once again, please add a minimal working example (MWE) that illustrates your problem. Also what do you have in mind when you mention improve?
    – percusse
    Commented Jan 23, 2013 at 15:17
  • 1
    Are you referring to index or table of contents? They are two very different concepts. If you're asking about the table of contents, as I believe, please state it clearly and at least add some information about what precisely you'd like to do.
    – egreg
    Commented Jan 23, 2013 at 16:41
  • I need something like tex.stackexchange.com/questions/38574/… but i need image there
    – manish
    Commented Jan 23, 2013 at 16:59

1 Answer 1

8

Inserting raw content into the ToC is done using \addtocontents{toc}{<stuff>}. In order to survive writing such content, \protecting is usually required. Here's a small example showing how to do it:

enter image description here

\documentclass{article}
\usepackage{graphicx}% http://ctan.org/pkg/graphicx
\newcommand{\addstufftotoc}[2][toc]{% \addimagetotoc[<toc>]{<stuff>}
  \addtocontents{#1}{#2}}
\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}

In the above example, \addstufftotoc[<toc>]{<stuff>} allows you to add <stuff> to the ToC of your choice (say List of Figures/Tables or Table of Contents which is the default). The example images are from the mwe package. For unnumbered sections, \section* could be used, together with \addtocontents to insert the appropriate formatted title in the ToC (if needed).


Additional request: Having the same size font \section in the ToC than in the main document body

Sectional unit displays in the ToC is governed by the macro \l@section. The following is taken from its definition in article.cls:

\newcommand*\l@section[2]{%
  \ifnum \c@tocdepth >\z@
    \addpenalty\@secpenalty
    \addvspace{1.0em \@plus\p@}%
    \setlength\@tempdima{1.5em}%
    \begingroup
      \parindent \z@ \rightskip \@pnumwidth
      \parfillskip -\@pnumwidth
      \leavevmode \bfseries
      \advance\leftskip\@tempdima
      \hskip -\leftskip
      #1\nobreak\hfil \nobreak\hb@xt@\@pnumwidth{\hss #2}\par
    \endgroup
  \fi}

The arguments passed to \l@section are the sectional unit number and title (as #1) and the page number (as #2). To match the font size from \section, we extract its definition from article.cls:

\newcommand\section{\@startsection {section}{1}{\z@}%
                                   {-3.5ex \@plus -1ex \@minus -.2ex}%
                                   {2.3ex \@plus.2ex}%
                                   {\normalfont\Large\bfseries}}

and redefine \l@section appropriately using a patch (for cleanliness; provided by the etoolbox package):

enter image description here

\documentclass{article}
\usepackage{etoolbox,graphicx}% http://ctan.org/pkg/{etoolbox,graphicx}
\newcommand{\addstufftotoc}[2][toc]{% \addimagetotoc[<toc>]{<stuff>}
  \addtocontents{#1}{#2}}
\makeatletter
\patchcmd{\l@section}% <cmd>
  {\begingroup}% <search>
  {\begingroup\normalfont\Large\bfseries}% <replace>
  {}{}% <success><failure>
\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}
16
  • 1
    Thanks, Is there any way to replace word Contents with Summary
    – manish
    Commented Jan 26, 2013 at 2:32
  • 1
    @manish: Yes. Use \renewcommand{\contentsname}{Summary}. For more details, see How to change the name of document elements like “Figure”, “Contents”, “Bibliography” etc.?
    – Werner
    Commented Jan 26, 2013 at 3:35
  • 1
    @user15662: See the updated answer.
    – Werner
    Commented Jan 29, 2013 at 16:12
  • 1
    @user15662: So you want the font size of \section in the ToC to be the same as the font size of \section in the main body of your document? Yes, they're different, but that can be changed.
    – Werner
    Commented Jan 30, 2013 at 3:09
  • 1
    @user15662: See updated answer.
    – Werner
    Commented Jan 30, 2013 at 3:58

You must log in to answer this question.

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