# How to clear \listoffigures between different parts of the document?

Since I'm required to write a document in multiple languages, is it possible to reset the list of figures/tables at different parts of the document?

The first two languages are not required to have a list of figures, therefore I just need to discard the list once, before the last one.

I can reset the counter with:

\setcounter{figure}{0}
\setcounter{table}{0}
\setcounter{listing}{0}


However, in the final \listoffigures all figures will show up.

I can add arbitrary content to the .lof file with \addcontentsline, however I haven't found a corresponding \clearcontents function.

Alternatively, I've thought of rendering the three theses separately, and then merging the PDFs, but that feels hackish.

• can you please add a minimale example of your code? you can do this see titletoc package. – touhami May 27 '16 at 18:46
• This is not so easy with standard methods, since there's only one .lof file usually per run. – user31729 May 27 '16 at 18:47
• I agree with @touhami... could we have some more context? Currently things aren't very clear. – Werner May 27 '16 at 19:33
• @Werner Sorry if I was unclear, Christian's solution is what I was looking for. – RoliSoft May 27 '16 at 20:07
• One might be able to create multiple float types, one for each language. The name of the float/counter does not have to appear in the caption or listof... – John Kormylo May 27 '16 at 21:08

Edit: See the better versions at the of the code, the basic principles explained at the top did not change, however!

A really quick hack, that hooks into \listoffigures and \listoftables, advancing a counter for each \listof... and writing the entries to 1lof, 2lof, etc. (the same for 1lot, etc.)

This is done by redefining the \ext@lof etc. command each time a \caption is used.

\documentclass{book}

\usepackage{xpatch}

\newcounter{lofcntr}
\newcounter{lotcntr}

\newcommand{\clearcontents}{%
\stepcounter{lofcntr}%
\stepcounter{lotcntr}%
\setcounter{figure}{0}
\setcounter{table}{0}
}

\AtBeginDocument{%
\stepcounter{lofcntr}%
\stepcounter{lotcntr}%
}

\makeatletter

\xpretocmd{\caption}{%
\def\ext@figure{\number\value{lofcntr}lof}
\def\ext@table{\number\value{lotcntr}lot}
}{\typeout{Worked!}}{}

\xpatchcmd{\listoffigures}{%
\@starttoc{lof}%
}{%
\@starttoc{\number\value{lofcntr}lof}%
}{\typeout{Patch success}}{\typeout{Patch failure}}

\xpatchcmd{\listoftables}{%
\@starttoc{lot}%
}{%
\@starttoc{\number\value{lotcntr}lot}%
}{\typeout{Patch success}}{\typeout{Patch failure}}

\makeatother

\usepackage{caption}
\usepackage{blindtext}

\begin{document}
\tableofcontents
\listoffigures
\listoftables

\captionof{figure}{Some figure caption}

\captionof{figure}{Some figure caption}
\captionof{figure}{Some figure caption}
\captionof{figure}{Some figure caption}

\captionof{table}{Some table caption for 1st lot}
\captionof{table}{Some table caption for 1st lot}
\captionof{table}{Some table caption for 1st lot}
\captionof{table}{Some table caption for 1st lot}

\clearcontents

\listoffigures
\listoftables

\captionof{figure}{Some figure caption for 2nd lof}
\captionof{figure}{Some figure caption for 2nd lof}
\captionof{figure}{Some figure caption for 2nd lof}
\captionof{figure}{Some figure caption for 2nd lof}

\captionof{table}{Some table caption for 2nd lot}
\captionof{table}{Some table caption for 2nd lot}
\captionof{table}{Some table caption for 2nd lot}
\captionof{table}{Some table caption for 2nd lot}

\end{document}


Update Some better version:

\documentclass{book}

\usepackage{xparse}
\usepackage{xpatch}
\usepackage{caption}
\usepackage{blindtext}

% Do not a driver counter, i.e. a resetting counter for those two counter fellows here:
\newcounter{lofcntr}
\newcounter{lotcntr}

\NewDocumentCommand{\clearcontents}{}{%
\stepcounter{lofcntr}% We don't need labels here, I suppose?
\stepcounter{lotcntr}%
\setcounter{figure}{0}
\setcounter{table}{0}
}

\AtBeginDocument{%
\stepcounter{lofcntr}%
\stepcounter{lotcntr}%
}

\makeatletter
% Store the definition of \ext@figure etc. first
\let\latex@ext@figure\ext@figure
\let\latex@ext@table\ext@table

\AtBeginDocument{%
\xpretocmd{\caption}{%
% Prepend the extension with the number of the current list of ...
\def\ext@figure{\number\value{lofcntr}\latex@ext@figure}
\def\ext@table{\number\value{lotcntr}\latex@ext@table}
}{\typeout{Worked!}}{\typeout{Failed miserably!}}
}

\xpatchcmd{\listoffigures}{%
\@starttoc{lof}%
}{%
\@starttoc{\number\value{lofcntr}lof}%
}{\typeout{Patch success}}{\typeout{Patch failure}}

\xpatchcmd{\listoftables}{%
\@starttoc{lot}%
}{%
\@starttoc{\number\value{lotcntr}lot}%
}{\typeout{Patch success}}{\typeout{Patch failure}}

\makeatother

\begin{document}
\tableofcontents
\listoffigures
\listoftables

\captionof{figure}{Some figure caption for 1st lof}
\captionof{figure}{Some figure caption for 1st lof}
\captionof{figure}{Some figure caption for 1st lof}
\captionof{figure}{Some figure caption for 1st lof}

\captionof{table}{Some table caption for 1st lot}
\captionof{table}{Some table caption for 1st lot}
\captionof{table}{Some table caption for 1st lot}
\captionof{table}{Some table caption for 1st lot}

\clearcontents
\listoffigures
\listoftables

\captionof{figure}{Some figure caption for 2nd lof}
\captionof{figure}{Some figure caption for 2nd lof}
\captionof{figure}{Some figure caption for 2nd lof}
\captionof{figure}{Some figure caption for 2nd lof}

\captionof{table}{Some table caption for 2nd lot}
\captionof{table}{Some table caption for 2nd lot}
\captionof{table}{Some table caption for 2nd lot}
\captionof{table}{Some table caption for 2nd lot}

\end{document}


Update for minted and listings environment:

\documentclass{book}

\usepackage{minted}
\usepackage{xparse}
\usepackage{xpatch}
\usepackage{caption}
\usepackage{blindtext}

% Do not use a driver counter, i.e. a resetting counter for those two counter fellows here:
\newcounter{lofcntr}
\newcounter{lotcntr}
\newcounter{lolcntr}

\NewDocumentCommand{\clearcontents}{}{%
\stepcounter{lofcntr}% We don't need labels here, I suppose?
\stepcounter{lotcntr}%
\stepcounter{lolcntr}%
\setcounter{figure}{0}
\setcounter{table}{0}
\setcounter{listing}{0}
}

\makeatletter

\def\ext@figure{\number\value{lofcntr}\latex@ext@figure}
\def\ext@table{\number\value{lotcntr}\latex@ext@table}%
\@namedef{ext@listing}{\number\value{lolcntr}\latex@ext@listing}%
}

\AtBeginDocument{%
% Store the definition of \ext@figure etc. first
\let\latex@ext@figure\ext@figure
\let\latex@ext@table\ext@table
\let\latex@ext@listing\ext@listing
\stepcounter{lofcntr}%
\stepcounter{lotcntr}%
\stepcounter{lolcntr}%
}

\AtBeginDocument{%
\xpretocmd{\caption}{%
% Prepend the extension with the number of the current list of ...
}{\typeout{Patching caption worked!}}{\typeout{Patching caption failed miserably!}}
}

\xpatchcmd{\listoffigures}{%
\@starttoc{lof}%
}{%
\@starttoc{\number\value{lofcntr}lof}%
}{\typeout{Patch success}}{\typeout{Patch failure}}

\xpatchcmd{\listoftables}{%
\@starttoc{lot}%
}{%
\@starttoc{\number\value{lotcntr}lot}%
}{\typeout{Patch success}}{\typeout{Patch failure}}

\makeatother

\begin{document}
\tableofcontents
\listoffigures
\listoftables
\listoflistings

\begin{listing}
\mint{c}
/* code */
\caption{A foo listing for 1st lol}
\end{listing}

\begin{listing}
\mint{c}
/* code */
\caption{A foo listing for 1st lol}
\end{listing}

\begin{listing}
\mint{c}
/* code */
\caption{A foo listing for 1st lol}
\end{listing}

\begin{listing}
\mint{c}
/* code */
\caption{A foo listing for 1st lol}
\end{listing}

\captionof{figure}{Some figure caption for 1st lof}
\captionof{figure}{Some figure caption for 1st lof}
\captionof{figure}{Some figure caption for 1st lof}
\captionof{figure}{Some figure caption for 1st lof}

\captionof{table}{Some table caption for 1st lot}
\captionof{table}{Some table caption for 1st lot}
\captionof{table}{Some table caption for 1st lot}
\captionof{table}{Some table caption for 1st lot}

\clearcontents
\listoffigures
\listoftables
\listoflistings

\captionof{figure}{Some figure caption for 2nd lof}
\captionof{figure}{Some figure caption for 2nd lof}
\captionof{figure}{Some figure caption for 2nd lof}
\captionof{figure}{Some figure caption for 2nd lof}

\captionof{table}{Some table caption for 2nd lot}
\captionof{table}{Some table caption for 2nd lot}
\captionof{table}{Some table caption for 2nd lot}
\captionof{table}{Some table caption for 2nd lot}

\begin{listing}
\mint{c}
/* code */
\caption{A foo listing for 2nd lol}
\end{listing}

\begin{listing}
\mint{c}
/* code */
\caption{A foo listing for 2nd lol}
\end{listing}

\begin{listing}
\mint{c}
/* code */
\caption{A foo listing for 2nd lol}
\end{listing}

\end{document}

• the redefinition of \ext@figure deserves some rework, of course, but I had no time ;-) – user31729 May 27 '16 at 19:30
• @RoliSoft: You're welcome. I will update with a better version later on – user31729 May 27 '16 at 20:04
• I'm trying to adapt it for use with listing/minted, however I'm getting "Patch failure" on \xpatchcmd{\listoflistings}. Any ideas on that? – RoliSoft May 27 '16 at 20:44
• @RoliSoft: \listoflistings is defined differently. You can't apply the same scheme for anything. I'll take a look – user31729 May 27 '16 at 20:45
• In minted, \listoflistings is defined as \providecommand{\listoflistings}{\listof{listing}{\listoflistingscaption}}, if that helps. – RoliSoft May 27 '16 at 20:49

The following example (starting point was Christian Hupfer's example) defines a switch for each language \ifLangA, \ifLangB. When the part for a language X starts, then \ifLangX is written to the auxiliary files .lof and .lot and the closing \fi is added there at the end of the language part. When the list of figures and tables are set, only the current language part is enabled by the switch and the other entries are ignored.

When LaTeX sets the \listoffigures and \listoftables it clears the auxiliary files .lof and .lot after the list is output. This is prevented by locally setting \@fileswfalse, an internal switch, which tells LaTeX not to touch and write the auxiliary files (used by \nofiles). Only the last lists of figures and tables are set normally.

Full example:

\documentclass{book}

\newif\ifLangA
\newif\ifLangB

\newcommand*{\StartLang}[1]{%
\expandafter\protect\csname ifLang#1\endcsname
}%
\expandafter\protect\csname ifLang#1\endcsname
}%
\csname Lang#1true\endcsname
\expandafter\let\expandafter\LangFalse\csname Lang#1false\endcsname
}
\newcommand*{\StopLang}{%
\LangFalse
}

\makeatletter
\newcommand*{\ListOfFigures}{%
\begingroup
\@fileswfalse
\listoffigures
\endgroup
}
\newcommand*{\ListOfTables}{%
\begingroup
\@fileswfalse
\listoftables
\endgroup
}
\let\LastListOfFigures\listoffigures
\let\LastListOfTables\listoftables
\makeatother

\usepackage{caption}
\usepackage{blindtext}

\begin{document}
\tableofcontents

\StartLang{A}

\chapter{Lang A}

\ListOfFigures
\ListOfTables

\captionof{figure}{Some figure caption}

\captionof{figure}{Some figure caption}
\captionof{figure}{Some figure caption}
\captionof{figure}{Some figure caption}

\captionof{table}{Some table caption for 1st lot}
\captionof{table}{Some table caption for 1st lot}
\captionof{table}{Some table caption for 1st lot}
\captionof{table}{Some table caption for 1st lot}

\StopLang

\StartLang{B}

\chapter{Lang B}

\LastListOfFigures
\LastListOfTables

\captionof{figure}{Some figure caption for 2nd lof}
\captionof{figure}{Some figure caption for 2nd lof}
\captionof{figure}{Some figure caption for 2nd lof}
\captionof{figure}{Some figure caption for 2nd lof}

\captionof{table}{Some table caption for 2nd lot}
\captionof{table}{Some table caption for 2nd lot}
\captionof{table}{Some table caption for 2nd lot}
\captionof{table}{Some table caption for 2nd lot}

\StopLang

\end{document}


You can use the tocbasic package that is part of the KOMA-script classes to create your own list-of-things. See Chapter 15 in the KOMA-script manual.

Here is a MWE that assumes you are using the KOMA scrreprt class and want a list of figures for each chapter. In the preamble I define a new type of float called figure1 for figures in chapter 1 and a similar one called figure2 for figures in chapter 2 using tocbasic's \DeclareNewTOC function. These floats behave like regular figures (floattype=1). (Maybe this can be automated somehow to get automatic numbering for each chapter...) The corresponding listof lists are named lof1 and lof2. The actual listof is inserted in the document using \listoftoc{lof1}, \listoftoc{lof2}, etc. Using the class option listof=totoc adds the two listof entries to the table of contents.

\documentclass[listof=totoc]{scrreprt}

\usepackage[demo]{graphicx}

\DeclareNewTOC[%
type=figure1,%
float,% define a floating environment
floattype=1,%
counterwithin=chapter,%
name=Figure,%
listname={List of Figures for Chapter 1}%
]{lof1}

\DeclareNewTOC[%
type=figure2,%
float,% define a floating environment
floattype=1,%
counterwithin=chapter,%
name=Figure,%
listname={List of Figures for Chapter 2}%
]{lof2}

\begin{document}
\tableofcontents

\chapter{A chapter}
\label{cha:achapter}
\begin{figure1}
\centering
\includegraphics{}
\caption{A figure in the first chapter}
\label{fig:achapter}
\end{figure1}

\listoftoc{lof1}

\chapter{Another chapter}
\label{cha:another-chapter}
\begin{figure2}
\centering
\includegraphics{}
\caption{A figure in the second chapter}
\label{fig:anotherchapter}
\end{figure2}

\listoftoc{lof2}

\end{document}


If you don't use one of the KOMA classes, add \usepackage{scrhack,tocbasic} to your preamble.

Here is 3 methods

1 without any package just \addtocontents{lof}{\protect\clearlist} in the OP way

\documentclass{book}
\usepackage{mwe}

\newcommand{\clearlist}{%
\begingroup
\renewcommand\contentsline[3]{}}  % replace 3 with 4 if hyperref used
\newcommand{\resetlist}{\endgroup}

\begin{document}
\chapter{foo}
\begin{figure}
\centering
\includegraphics[scale=.4]{example-image}
\caption{Foo}
\end{figure}
\begin{figure}
\centering
\includegraphics[scale=.4]{example-image}
\caption{Foo}
\end{figure}

\chapter{bar}
\begin{figure}
\centering
\includegraphics[scale=.4]{example-image}
\caption{Foo}
\end{figure}
\begin{figure}
\centering
\includegraphics[scale=.4]{example-image}
\caption{Foo}
\end{figure}

\chapter{baz}
\listoffigures
\begin{figure}
\centering
\includegraphics[scale=.4]{example-image}
\caption{Foo}
\end{figure}
\begin{figure}
\centering
\includegraphics[scale=.4]{example-image}
\caption{Foo}
\end{figure}
\end{document}


2 using titletoc package with commands

\startlist{lof}
\printlist{lof}{}{%


and \stoplist{lof}

\documentclass{book}
\usepackage{mwe}

\usepackage{titletoc}

\begin{document}
\chapter{foo}
\begin{figure}
\centering
\includegraphics[scale=.4]{example-image}
\caption{Foo1}
\end{figure}
\begin{figure}
\centering
\includegraphics[scale=.4]{example-image}
\caption{Foo2}
\end{figure}

\chapter{bar}
\begin{figure}
\centering
\includegraphics[scale=.4]{example-image}
\caption{Foo3}
\end{figure}
\begin{figure}
\centering
\includegraphics[scale=.4]{example-image}
\caption{Foo4}
\end{figure}

\chapter{baz}
\startlist{lof}
\printlist{lof}{}{%
\renewcommand\addvspace[1]{}% % more stuff can be add here to customize the list
}
\begin{figure}
\centering
\includegraphics[scale=.4]{example-image}
\caption{Foo5}
\end{figure}
\begin{figure}
\centering
\includegraphics[scale=.4]{example-image}
\caption{Foo6}
\end{figure}
\stoplist{lof}
\end{document}


3 using minitoc package

\documentclass{book}
\usepackage{mwe}

\usepackage{minitoc}
\dominilof
\AtEndDocument{\listoffigures}%   % comment this line in last run
\begin{document}
\chapter{foo}
\begin{figure}
\centering
\includegraphics[scale=.4]{example-image}
\caption{Foo1}
\end{figure}
\begin{figure}
\centering
\includegraphics[scale=.4]{example-image}
\caption{Foo2}
\end{figure}

\chapter{bar}
\begin{figure}
\centering
\includegraphics[scale=.4]{example-image}
\caption{Foo3}
\end{figure}
\begin{figure}
\centering
\includegraphics[scale=.4]{example-image}
\caption{Foo4}
\end{figure}

\chapter{baz}
\minilof
\begin{figure}
\centering
\includegraphics[scale=.4]{example-image}
\caption{Foo5}
\end{figure}
\begin{figure}
\centering
\includegraphics[scale=.4]{example-image}
\caption{Foo6}
\end{figure}
\end{document}


Note one need to use \listoffigures some where (at end of document) and comment it in last run or just save it in a savebox.

• I ended up using your first method due to its simplicity and universal applicability to any toc file. Thanks! – RoliSoft May 27 '16 at 22:26

What can I say? It's an interesting problem.

\documentclass{article}
\usepackage{float}
\usepackage{caption}

\newfloat{german}{tbp}{lofg}
\floatname{german}{Figur}

\begin{document}

\captionof{figure}{English}
\captionof{german}{Deutsch}

\listoffigures

\listof{german}{Liste Figuren}

\end{document}