[Tex/LaTex] Problems with \addtocontent and \addcontentsline

table of contentstocloft

I am trying to add a heading in the list of figures and list of tables. I found a solution here:

Include chapters in List of Figures with titletoc?

However, this looks pretty complicated and I after some more googling I found this webpage:

http://www.personal.ceu.hu/tex/toc.htm

which show how to add a single line to toc, lof and lot using just 1 command. This seemed to be my solution however when I add the following line:

\addtocontents{lof}{Heading}

I get an error message: ! LaTeX Error: Something's wrong–perhaps a missing \item.

I googled and found this webpage which shows a solution for this problem:

https://groups.google.com/forum/?fromgroups=#!topic/comp.text.tex/2IE68-IJJ6U

I tried different approaches such as:

\addtocontents{lof}{\protect\contentsline{chapter}{Heading 1}{}}
\addtocontents{lof}{\protect\contentsline{figure}{Heading 2}{}}

\addcontentsline{lof}{chapter}{\protect\numberline{}{Heading 3}}
\addcontentsline{lof}{figure}{\protect\numberline{}{Heading 4}}

The one which looks closest to what I want is the first command …{Heading 1}
However, using this command latex adds an extra line or extra vertical space so that
there is a new line after Heading 1 in the lof.

This would be fixed by using:

\addtocontents{lof}{\textbf{Heading}}

However, as already written this results in an error. I don't understand why this simple command results in an error even though I am quite sure I got the syntax right.

So just to clarify how I would like to have my lof here is a short example:

\textbf{1 Chapter}
          1.1 Figure 1
          1.2 Figure 2

\textbf{2 Chapter}
          2.1 Figure 3
          2.2 Figure 4

Can anyone help?

Here is my working example:

\documentclass[10pt,DIV=12,a4paper,numbers=noenddot]{scrreprt}
\begin{document}

\tableofcontents
\listoffigures

% 
\addtocontents{lof}{\protect\contentsline{figure}{Heading 1}{}}
\addcontentsline{lof}{chapter}{\protect\numberline{}{Heading 2}}
\addcontentsline{lof}{figure}{\protect\numberline{}{Heading 3}}

% Closest to what I would prefer
\addtocontents{lof}{\textbf{Heading 4}\protect\par}
\addtocontents{lof}{\protect\contentsline{chapter}{Heading 5}{}}

\chapter{Introduction}
\section{s1}

\begin{figure}
\caption{figure 1}
\end{figure}

\begin{figure}
\caption{figure 2}
\end{figure}

\section{s2}

\chapter{Content}
\section{c1}
\section{c2}

\end{document}

I basically want the lof to look like the toc. There is no newline after "1 Introduction" and "2 Content". I also just realized that the font for "Heading 4" and "Heading 5" looks different. So I think I would prefer the font of "Heading 5" which I think is the one used in the toc. So "Heading 5" would be perfect if there would not be a newline afterwards. Is it possible to somehow suppress this newline?

Best Answer

The answer to the question linked provides and automated solution; if you, for some reason, decide to do this manually (which might be a cause for problems: for example, the automated solution won't add heading if there's no figures in a chapter and manually you can overlook this), you can proceed as in the xample below.

Using a variation of Martin Scharrer's answer to Table of Contents with additional description of entries, I defined a \headinginfo command which typesets its mandatory argument in the LoF, using the same formatting as chapter entries in the ToC (since a KOMA class is used, this is easily achieved using \usekomafont{disposition}); the \headinginfo command must be used before the first figure environment of a chapter (I would suggest immediately after the corresponding \chapter):

\documentclass{scrreprt}

\makeatletter
% A modification of the kernel's \@dottedtocline,
% suppressing the leaders (dots) and page number
% A variation of Martin Scharrer's answer:
% https://tex.stackexchange.com/a/10189
\def\@nodottedtocline#1#2#3#4#5{%
    \vskip \z@ \@plus.2\p@
    {\leftskip #2\relax \rightskip \@tocrmarg \parfillskip -\rightskip
     \parindent #2\relax\@afterindenttrue
     \interlinepenalty\@M
     \leavevmode
     \@tempdima #3\relax
     \advance\leftskip \@tempdima \null\nobreak\hskip -\leftskip
     {#4}\hfill\null\par}}
\newcommand*\l@heading{\@nodottedtocline{0}{0em}{0em}}
\makeatother
\newcommand\headinginfo[1]{%
  \addcontentsline{lof}{heading}{{\usekomafont{disposition}#1}}%
  \addtocontents{lof}{\vspace{5pt}}}

\begin{document}
\listoffigures

\chapter{Test Chapter One}
\headinginfo{Some Text for Chapter One}
\begin{figure}
\centering
A
\caption{A test figure}
\end{figure}
\begin{figure}
\centering
B
\caption{Another test figure}
\end{figure}

\chapter{Test Chapter Two}
\headinginfo{Some Text for Chapter Two}
\begin{figure}
\centering
C
\caption{A test figure}
\end{figure}
\begin{figure}
\centering
D
\caption{Another test figure}
\end{figure}

\end{document}

enter image description here

Related Question