5

I'm using KOMAScript. I have a \listoffigures and a \listoftables. My document has several \part{}'s. In the two lists the figures and tables are already grouped (small vertical space) by the chapters.

What I'd like to have additionally to the ("anonymous") chapter vspace grouping is the part title over each "part group" of figures and tables in the two lists. Is this somehow possible without having to code a list from scratch?

Foo Bar
  • 13,247
  • Does every part contain tables and figures? If not the code has to be more complex. – egreg Mar 23 '13 at 15:54
  • No. It's possible that a part does only have figures or tables or none of them or both. – Foo Bar Mar 23 '13 at 15:56
  • I just made an edit: the vspace grouping happens on every chapter, of course (not part). But I'd like to have the part title in the lists. – Foo Bar Mar 23 '13 at 15:59

2 Answers2

8

You can setup your own entry by:

\newcommand{\addtocentrylistof}[3]{%
  \ifstr{#2}{}{%
    \addcontentsline{lof}{#1}{#3}%
    \addcontentsline{lot}{#1}{#3}%
  }{%
    \addcontentsline{lof}{#1}{\protect\numberline{#2}#3}%
    \addcontentsline{lot}{#1}{\protect\numberline{#2}#3}%
  }%
}

\renewcommand*{\addparttocentry}[2]{%
  \addtocentrydefault{part}{#1}{#2}%
  \addtocentrylistof{part}{#1}{#2}%
}

Here a small example:

\documentclass[english]{scrreprt}
\usepackage{babel}
\newcommand{\addtocentrylistof}[3]{%
  \ifstr{#2}{}{%
    \addcontentsline{lof}{#1}{#3}%
    \addcontentsline{lot}{#1}{#3}%
  }{%
    \addcontentsline{lof}{#1}{\protect\numberline{#2}#3}%
    \addcontentsline{lot}{#1}{\protect\numberline{#2}#3}%
  }%
}

\renewcommand*{\addparttocentry}[2]{%
  \addtocentrydefault{part}{#1}{#2}%
  \addtocentrylistof{part}{#1}{#2}%
}
\def\examplefig{\begin{figure}[!ht]\caption{foo}\end{figure}\begin{table}[!ht]\caption{foo}\end{table}}
\begin{document}
\tableofcontents
\listoffigures
\listoftables

\part{O I}
\chapter{foo}
\examplefig\examplefig\examplefig
\chapter{foo}
\examplefig\examplefig\examplefig
\chapter{foo}
\examplefig\examplefig\examplefig
\part{O II}
\chapter{bar}
\examplefig\examplefig\examplefig
\chapter{bar}
\examplefig\examplefig\examplefig
\chapter{bar}
\examplefig\examplefig\examplefig

\end{document}
Marco Daniel
  • 95,681
5

This is just an adaptation from this answer to Include chapters in List of Figures with titletoc? . The inclusion is made automatically using the standard commands and only for the parts with figures:

\documentclass{scrreprt}
\usepackage{etoolbox} % or xpatch

\makeatletter
% initial definitions of the chapter info (name and number)
\def\thischaptertitle{}\def\thischapternumber{}
\newtoggle{noFigs}
\newtoggle{noTabs}

\apptocmd{\@part}%
  {\gdef\thisparttitle{#1}\gdef\thispartnumber{\thepart}%
    \global\toggletrue{noFigs}\global\toggletrue{noTabs}}{}{}

% the figure environment does the job: the first time it is used after a \chapter command, 
% it writes the information of the chapter to the LoF
\AtBeginDocument{%
  \AtBeginEnvironment{figure}{%
    \iftoggle{noFigs}{
      \addtocontents{lof}{\protect\contentsline {part}%
        {\protect\numberline {\thispartnumber} {\thisparttitle}}{}{} }
      \global\togglefalse{noFigs}
    }{}
  }%
  \AtBeginEnvironment{table}{%
    \iftoggle{noTabs}{
      \addtocontents{lot}{\protect\contentsline {part}%
        {\protect\numberline {\thispartnumber} {\thisparttitle}}{}{} }
      \global\togglefalse{noTabs}
    }{}
  }%
}

\makeatother


\begin{document}

\tableofcontents
\listoffigures
\listoftables

\part{Test Part With Figures and Tables}
\chapter{Test Chapter with Figures and Tables}
\begin{figure}
\caption{caption text}
\end{figure}
\begin{figure}
\caption{caption text}
\end{figure}
\begin{table}
\caption{caption text for a table}
\end{table}

\part{Test Part with no Figures and no Tables}

\part{Test Part with no Figures and Tables}
\chapter{Test Chapter with Table}
\begin{table}
\caption{caption text for a table}
\end{table}

\part{Test Part with no Tables and Figures}
\chapter{Test Chapter with Figure}
\begin{figure}
\caption{caption text}
\end{figure}

\part{Another Test Part With Figures and Tables}
\chapter{Another Test Chapter}
\begin{figure}
\caption{caption text}
\end{figure}
\begin{table}
\caption{caption text for a table}
\end{table}
\begin{figure}
\caption{caption text}
\end{figure}
\begin{table}
\caption{caption text for a table}
\end{table}
\begin{figure}
\caption{caption text}
\end{figure}
\begin{figure}
\caption{caption text}
\end{figure}
\begin{figure}
\caption{caption text}
\end{figure}

\end{document}

An image of the LoF:

enter image description here

An image of the LoT:

enter image description here

Gonzalo Medina
  • 505,128
  • @Speravir xpatch loads etoolbox – egreg Mar 23 '13 at 16:08
  • 1
    @egreg Aaah, forgot this. Thanks, and sorry to Gonzalo! – Speravir Mar 23 '13 at 16:09
  • Thanks, but I get the error Use of \H@old@part doesn't match its definition. at each occurence of \part. – Foo Bar Mar 23 '13 at 16:16
  • @Speravir No problem ;-) – Gonzalo Medina Mar 23 '13 at 16:16
  • @FooBar Do you get the error with my exact code? If not, please add the code you use as an edit to your question. – Gonzalo Medina Mar 23 '13 at 16:17
  • I use exact your code. I'm using scrreprt, not scrbook and I use \setpartpreamble{} from KOMAScript, if this matters. – Foo Bar Mar 23 '13 at 16:19
  • @FooBar I just did some tests with scrreprt and everythink worked OK (in fact, if you copy/past my updated example code exactly as it is, as a new document, and process it you'll se that the result is the expected). Something else must be going on in your actual document; please design a minimal document illustrating the problem and add it as an edit to your question, so we can investigate what might be causing the problem. – Gonzalo Medina Mar 23 '13 at 17:38