This is a follow up to "Include chapters in List of Figures with titletoc?". There appears to be a problem with the original solution when a float is placed at the top of the page following a new part/chapter. In the LoF, the figure caption is then above the inserted part/chapter title (see screenshot). If we change the float position to b
or anything else, it works fine.
The code below is adapted to report class, but I've checked and exists in book/report/article and its KOMA equivalents.
\documentclass{scrreprt}
\usepackage{lipsum}
\usepackage{etoolbox}
\makeatletter
\def\thisparttitle{}\def\thispartnumber{}
\newtoggle{noFigs}
\apptocmd{\@part}%
{\gdef\thisparttitle{#1}\gdef\thispartnumber{\thepart}%
\global\toggletrue{noFigs}}{}{}
\AtBeginDocument{%
\AtBeginEnvironment{figure}{%
\iftoggle{noFigs}{
\addtocontents{lof}{\protect\contentsline {part}%
{\protect\numberline {\thispartnumber} {\thisparttitle}}{}{} }
\global\togglefalse{noFigs}
}{}
}%
}
\makeatother
\begin{document}
\listoffigures
\part{Part 1}
\chapter{Hallo}
\lipsum
\section{Hallo}
\begin{figure}[t] % change placement to cbt and it works
\caption{Part 1 Fig}
Test
\end{figure}
\end{document}
Edit
The suggested solution from Heiko below has a problem when several figures/tables are in different parts. For example:
\begin{document}
\listoftables\listoffigures
\part{Part 1}
\begin{table}\caption{Part 1 Tab}\end{table}
\part{Part 2}
\begin{table}\caption{Part 1 Tab}\end{table}
\begin{figure}\caption{Part 1 Fig}\end{figure}
\end{document}