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

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}

enter image description here

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

\NewDocumentCommand{\advancecontents}{}{%
  \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}%
  \advancecontents%
}



\AtBeginDocument{%
\xpretocmd{\caption}{%
  % Prepend the extension with the number of the current list of ...
  \advancecontents%
}{\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}

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\addvspace[1]{}
\renewcommand\contentsline[3]{}}  % replace 3 with 4 if hyperref used
\newcommand{\resetlist}{\endgroup}

\begin{document}
\addtocontents{lof}{\protect\clearlist}
\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}
\addtocontents{lof}{\protect\resetlist}
\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}{}{%
\renewcommand\addvspace[1]{}}

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.


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]{%
  \addtocontents{lof}{%
    \expandafter\protect\csname ifLang#1\endcsname
  }%
  \addtocontents{lot}{%
    \expandafter\protect\csname ifLang#1\endcsname
  }%
  \csname Lang#1true\endcsname
  \expandafter\let\expandafter\LangFalse\csname Lang#1false\endcsname
}
\newcommand*{\StopLang}{%
  \addtocontents{lof}{\protect\fi}%
  \addtocontents{lot}{\protect\fi}%
  \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}