I would like to add parts and chapters to the LOF. I can add chapters but the \apptocmd{\@part} doesn't work unless I comment \titleformat{\part}. Why ?
I have found a workaround with \addtoLOF\currentTitle but I would like to understand.
\documentclass[french]{book}
\usepackage{etoolbox}
\usepackage{babel} % I don't know what are active characters and they may interfere with your solution…
\usepackage{fontspec} % I use XeLaTeX
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% PARTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{titlesec}
%\makeatletter\newcommand*\currentTitle\ttl@savetitle\makeatother
\titleformat{\part}[display]
{\centering\huge} % Before
{\thispagestyle{empty}\partname\ \thepart}{20pt}{\Huge}
[] % After
%[\addtoLOF\currentTitle] % After
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% LOF %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\makeatletter % https://tex.stackexchange.com/questions/52746/include-chapters-in-list-of-figures-with-titletoc
\newtoggle{isThisChapterTitleAlreadyAddedInTheLOF}
\newtoggle{isThisPartTitleAlreadyAddedInTheLOF}
\newtoggle{isThereAPart}
\newtoggle{isThereAChapter}
\global\togglefalse{isThereAPart}
\global\togglefalse{isThereAChapter}
\apptocmd{\@chapter}{ % before hyperref
\gdef\thischaptertitle{#1}
\gdef\thischapternumber{\thechapter}
\global\toggletrue{isThereAChapter}
\global\togglefalse{isThisChapterTitleAlreadyAddedInTheLOF}
}{}{\typeout{Problème}}
%%%%%%%%%%%%%%%% Doesn't work ! %%%%%%%%%%%%%%%%%%%%%%%
\apptocmd{\@part}{ % before hyperref
\gdef\thisparttitle{#1}
\gdef\thispartnumber{\thepart}
\global\toggletrue{isThereAPart}
\global\togglefalse{isThisPartTitleAlreadyAddedInTheLOF}
\global\togglefalse{isThereAChapter} % There is yet no chapter in this part
}{}{\typeout{Problème}}
\makeatother
%%%%%%%%%%%%%%%% Works ! %%%%%%%%%%%%%%%%%%%%%%%
\newcommand*{\addtoLOF}[1]{
\gdef\thisparttitle{#1}
\gdef\thispartnumber{\thepart}
\global\toggletrue{isThereAPart}
\global\togglefalse{isThisPartTitleAlreadyAddedInTheLOF}
\global\togglefalse{isThereAChapter} % There is yet no chapter in this part
}
\makeatletter
\AtBeginDocument{ % Is this line useful ?
\AtBeginEnvironment{figure}{
\iftoggle{isThereAPart}{ % True
\iftoggle{isThisPartTitleAlreadyAddedInTheLOF}{}{ % True : nothing to do %%% False : add the part title
\addtocontents{lof}{\protect\contentsline {part} {\protect\numberline {\thispartnumber} {\thisparttitle}}{}{} }
\global\toggletrue{isThisPartTitleAlreadyAddedInTheLOF}
}
}{} % False
\iftoggle{isThereAChapter}{ % True
\iftoggle{isThisChapterTitleAlreadyAddedInTheLOF}{}{ % True : nothing to do %%% False : add the chapter title
\addtocontents{lof}{\protect\contentsline {chapter} {\protect\numberline {\thischapternumber} {\thischaptertitle}}{}{} }
\global\toggletrue{isThisChapterTitleAlreadyAddedInTheLOF}
}
}{} % False
}
}
\makeatother
\begin{document}
\frontmatter
\listoffigures
\mainmatter
\part{Hello}
\chapter{Introduction with no Figure}
\chapter{Test Chapter with Figures}
\begin{figure}
\caption{caption text}
\end{figure}
\begin{figure}
\caption{caption text}
\end{figure}
\chapter{Test Chapter with no Figure}
\chapter{Another Test Chapter with Figures}
\begin{figure}
\caption{caption text}
\end{figure}
\begin{figure}
\caption{caption text}
\end{figure}
\part{Part with no figure}
\chapter{Test Chapter with no Figure}
\part{Part with figures but without chapter}
Text. % Necessary to have the part added to the LOF ?!
\begin{figure}
\caption{caption text}
\end{figure}
\begin{figure}
\caption{caption text}
\end{figure}
\end{document}
2nd day
Another symptom of the same problem:
\documentclass[french]{book}
\usepackage{titlesec}
\makeatletter\pretocmd{\@part}{\gdef\parttitle{#1}}{}{}\makeatother
\titleformat{\part}[display]
{\centering\huge} % Before
{\thispagestyle{empty}\partname\ \thepart}{20pt}{\Huge}
%[] % After
\begin{document}
\part{A part…}
\parttitle
\end{document}
produces the following error: Undefined control sequence. \parttitle. So, the patched \part command seems not to be used anymore when I \titleformat{\part}. That's why \parttitle is not defined. So how can I patch \part when \titleformat{\part} is used?
3rd day
I found something. As suggested here: error when patching with etoolbox \apptocmd: "the patching command seems to be nested in the argument to some other command", the following works.
\documentclass[french]{book}
\usepackage{xparse}
\usepackage{etoolbox}
\usepackage{babel}
\usepackage{fontspec}
\usepackage{titlesec}
\titleformat{\part}[display]
{\centering\huge} % Before
{\thispagestyle{empty}\partname\ \thepart}{20pt}{\Huge}
[] % After
\makeatletter
\expandafter\pretocmd{\@part}\expandafter{\gdef\parttitle{\ttl@savetitle}}{}{}
\makeatother
\begin{document}
\part{A part…}
\parttitle
\end{document}
I don't understand very well…
