I am trying to adapt some code so that I can have chapter headings in my list of figures, tables, equations and reaction schemes using the memoir document class. I asked a question yesterday and received quality answers which I was very happy with and implemented until I checked out a comment by JohnKormylo which fixed the main issue of the question, that being how to get memoir to recognize the chapter titles as well as the chapter numbers (changing \gdef\thischaptertitle{#1} to {#2} is the solution). This now skips the chapter 3 heading if there are no figures/tables etc in that chapter which is incredibly desirable to me.
The issue now is that the code is incompatible with hyperref and is producing the following error message:
(./Listoffigurestest.out) (./Listoffigurestest.out)
Package hyperref Warning: old lof file detected, not used; run LaTeX again.
[1{c:/texlive/2021/texmf-var/fonts/map/pdftex/updmap/pdftex.map}]
! Undefined control sequence.
<argument> ...umberline {\thefigure }{\my@caption
}}{\thepage }{@currentHre...
l.59 \end{figure}
?
The error persists even when I delete the .aux files before running. I would appreciate it if anyone knew how to fix this please as I do not know latex enough at this moment although that will change with time. The minimal working example to produce the error is:
\documentclass[oneside]{memoir}
\usepackage{etoolbox}
\makeatletter
\def\thischaptertitle{}\def\thischapternumber{}
\newtoggle{noFigs}
\apptocmd{@chapter}%
{\gdef\thischaptertitle{#2}\gdef\thischapternumber{\thechapter}%
\global\toggletrue{noFigs}}{}{}
\AtBeginDocument{%
\AtBeginEnvironment{figure}{%
\iftoggle{noFigs}{
\addtocontents{lof}{\protect\contentsline {chapter}%
{\protect\numberline {\thischapternumber.} {\thischaptertitle}}{}{} }
\global\togglefalse{noFigs}
}{}
}%
}
\long\def@caption#1[#2]#3{%
\par
\gdef\my@caption{#2}
\begingroup
@parboxrestore
\if@minipage
@setminipage
\fi
\normalsize
@makecaption{\csname fnum@#1\endcsname}{\ignorespaces #3}\par
\endgroup}
%
\renewenvironment{figure}%
{@float{figure}}%
{\end@float
\addcontentsline{lof}{figure}%
{\protect\numberline{\thefigure}{\my@caption}}%
}%
\renewenvironment{figure*}%
{@dblfloat{figure}}%
{\end@dblfloat
\addcontentsline{lof}{figure}%
{\protect\numberline{\thefigure}{\my@caption}}%
}%
\makeatother
\usepackage{hyperref}
\begin{document}
\listoffigures
\chapter{Testing}
\section{Hallo}
\begin{figure}[t]
\caption{First figure}
Test
\end{figure}
\begin{figure}[b]
\caption{Second figure}
Test
\end{figure}
\chapter{Hallo}
\section{Hallo}
\begin{figure}[t]
\caption{First figure}
Test
\end{figure}
\begin{figure}[b]
\caption{Second figure}
Test
\end{figure}
\chapter{Bye}
\chapter{Bonjour}
\begin{figure}[b]
\caption{Second figure}
Test
\end{figure}
\end{document}
Edit: Solution to chapter headings being beneath the list of... entries
When running the solution provided by Ulrike, the chapter headings on my list of's looked like this:
I am using a segmented file with \include{} and custom .sty packages so struggled to identify what was causing the error. Turns out the solution is to delete the code in your \include{chapter1} etc, save it and redo the code again. Not sure why this worked but so be it.
Hope this helps someone in the future.



\@captionand figure? That looks quite odd. Typically the caption writes the lof-entry, not the figure environment. – Ulrike Fischer Jul 05 '21 at 06:53