2

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…

user73438
  • 153
  • 6
  • 1
    What happens when you have a part/chapter that doesn't have any figures (or tables) in it? Do you still want the title in the LoF (LoT)? If not, it may seem weird. But then again, if you do, it may seem equally weird. – Werner Mar 08 '15 at 18:26
  • Hi, thanks for your really fast reply. I test for each figure if I need to add the chapter and the part. So a part or chapter is added only if there is a figure. The figure environment do the job : \AtBeginEnvironment{figure}{if we are in a part -> add this part ; if we are in a chapter -> add this chapter} – user73438 Mar 08 '15 at 19:57
  • Something like this: http://tex.stackexchange.com/questions/200360/list-of-tables-or-figures-by-section-latex ? –  Mar 10 '15 at 00:33
  • Hi, like this, yes. I did not read this thread before but that one: http://tex.stackexchange.com/questions/52746/include-chapters-in-list-of-figures-with-titletoc. I used it to make the code above. It works perfectly for chapters but not for parts. I suspect parts are handled differently. My knowledge is limited. Do you know how I can add parts to the LOF? Why the apptocmd doesn't work with parts? Or preferably, how to use it for parts? – user73438 Mar 10 '15 at 17:31
  • What does "LOF" stand for -- "List of Figures"? – Mico Mar 10 '15 at 22:15
  • Yes, LoF: List of figures, LoT: List of tables, ToC: table of contents. – user73438 Mar 11 '15 at 20:36
  • But I never saw LoL for list of listings :) – user73438 Mar 12 '15 at 19:30

1 Answers1

2

My own answer finally…

I will add parts and chapters to the List of figures (LOF). The list of figures will contain figures of course, but also wrapfigures and background images.

\documentclass[french]{book}

\usepackage{xparse}
\usepackage{etoolbox}
\usepackage{babel}
\usepackage{fontspec} % I use XeLaTeX
\usepackage{lipsum}

\usepackage{graphicx}
\usepackage{wrapfig} % Wrap images
\usepackage{background} % For background images, and full page images, like on the cover page
\backgroundsetup{pages=some} % To control the display of the background on each page with \BgThispage
\backgroundsetup{contents=} % To remove the default: « draft » — Why ? I already said « some », it should only display on demand ?!
\usepackage[space]{grffile} % To deal with spaces in path of files

1. Unique environment for figures, wrapfigures and background images: illustration

I will use \AtBeginEnvironment{figure}{…} later. But wrapfigures are not figures… So there are 2 solutions:

— Duplicate the \AtBeginEnvironment{figure}{…} for wrapfigures: \AtBeginEnvironment{wrapfigure}{…}. But if I modify the code of one, I must not forget to modify the other.

— Or create a unique environment for figures and wrapfigures.

I choose the second one. So I need the xparse package.

If you don't need the illustration environment, jump to the next point and replace « illustration » by « figure » (or else).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Unique environment for figures, wrapfigures and background images

\NewDocumentEnvironment{illustration}{soD(){O}oD(){0pt}}{
  % The first argument: noStar=figure; star=wrapfigure
  % The second argument must be [htbp] (or the like) for a figure, and [narrowLines] for a wrapfigure
  % The following arguments are the usual ones for wrapfigure: {placement}[overhang]{width}
  % — Here I wrote the default values letter « Ô » then « zero point » for the 2 mandatory arguments of wrapfigure
  \IfBooleanTF{#1}{
    \IfNoValueTF{#2}{                   % Starred
      \IfNoValueTF{#4}{
        \begin{wrapfigure}{#3}{#5}
      }{\begin{wrapfigure}{#3}[#4]{#5}
      }
    }{\IfNoValueTF{#4}{
        \begin{wrapfigure}[#2]{#3}{#5}
      }{\begin{wrapfigure}[#2]{#3}[#4]{#5}
      }
    }
  }{\IfNoValueTF{#2}{                   % Nonstarred
      \begin{figure} }{
      \begin{figure}[#2]}
  }
}{\IfBooleanTF{#1}{
    \end{wrapfigure}                    % Starred
  }{\end{figure}}                       % Nonstarred
}
% Usage:
% For a figure:     \begin{illustration}[htbp] … \end{illustration} or \illustration[htbp] … \endillustration
% For a wrapfigure: \illustration* … \endillustration
%   As said here (http://stackoverflow.com/questions/3233031/latex-defining-a-custom-wrapfig-environment), we can't use \begin{illustration}* … \end{illustration}*
%   Wrapfigure does not accept empty arguments, ie. []{O}[]{0pt} (! Missing number, treated as zero. <to be read again> \relax l.xxx \end{wrapfigure})
%   Wrapfigure needs a box or a graphic inside the environment, the caption is not enough
% For a background image, the trick is to place the caption in a figure environment:
%   \backgroundsetup{some options, contents={\includegraphics[width=\paperwidth,height=\paperheight]{Some graphic}}}\BgThispage
%   \begin{illustration} Caption here \end{illustration}

2. Modify parts and chapters definitions

This is what I struggled with. I used this: Include chapters in List of Figures with titletoc?, and \expandafter thanks to that: error when patching with etoolbox \apptocmd: "the patching command seems to be nested in the argument to some other command".

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Tweak the PARTS and CHAPTERS definitions %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage[newparttoc]{titlesec} % newparttoc because I will use \titlecontent{part} -- I'm not sure of this

\titleformat{\part}[display]
            {\centering\huge} % Before
            {\thispagestyle{empty}\partname\ \thepart}{20pt}{\Huge}
            [] % After

\makeatletter
\expandafter\pretocmd{\@part}\expandafter{\gdef\parttitle{\ttl@savetitle}}{}{} % #1 does not work here so I use \ttl@savetitle
% \expandafter is mandatory here: https://tex.stackexchange.com/questions/51929/error-when-patching-with-etoolbox-apptocmd-the-patching-command-seems-to-be-n --- This was my problem
\makeatother

\makeatletter
\newtoggle{isThisChapterTitleAlreadyAddedInTheLOF}
\newtoggle{isThisPartTitleAlreadyAddedInTheLOF}
\newtoggle{areWeInAPart}
\newtoggle{areWeInAChapter}
\global\togglefalse{areWeInAPart}
\global\togglefalse{areWeInAChapter}

\apptocmd{\@chapter}{ % before hyperref
  \gdef\thischaptertitle{#1}
  \gdef\thischapternumber{\thechapter}
  \global\toggletrue{areWeInAChapter}
  \global\togglefalse{isThisChapterTitleAlreadyAddedInTheLOF}
}{}{\typeout{Problème}}

\expandafter\apptocmd{\@part}\expandafter{ % before hyperref
  % Again, \expandafter is mandatory here: https://tex.stackexchange.com/questions/51929/error-when-patching-with-etoolbox-apptocmd-the-patching-command-seems-to-be-n --- This was my problem
  \gdef\thisparttitle{\parttitle}
  \gdef\thispartnumber{\thepart}
  \global\toggletrue{areWeInAPart}
  \global\togglefalse{isThisPartTitleAlreadyAddedInTheLOF}
  \global\togglefalse{areWeInAChapter} % There is yet no chapter in this part
  \global\togglefalse{isThisChapterTitleAlreadyAddedInTheLOF}
}{}{\typeout{Problème}}
\makeatother

3. Write parts and chapters into the LOF

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Finally %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% https://tex.stackexchange.com/questions/52746/include-chapters-in-list-of-figures-with-titletoc

\makeatletter
\AtBeginDocument{ % Is this line useful ?
  \AtBeginEnvironment{illustration}{
    \iftoggle{areWeInAPart}{ % 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{areWeInAChapter}{ % 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

4. Also tested

% I also tested with the following — xcolor should be placed at the beginning (or almost).
%\usepackage{xcolor} % [dvipsnames,svgnames,x11names,hyperref,table,fixpdftex] % Only LyX, xcolor and dvipsnames work for me
%\definecolor{Bleu}{RGB}{0,0,127} % Pour les liens
%\definecolor{Violet}{RGB}{127,0,127} % Pour les liens
%\definecolor{Vert}{RGB}{0,127,0} % Pour les liens
%\definecolor{Rouge}{RGB}{127,0,0} % Pour les liens
%\usepackage{hyperref} % Loaded after titletoc and usually at the end
%\hypersetup{
%  unicode, % Caractères unicode dans les bookmarks et le texte
%  %pdfencoding=unicode, % Pour les liens en unicode
%  psdextra, % additional math macros are supported in bookmarks
%  %pagebackref=section, % Idem avec la bibliographie (s'il y a un blanc entre chaque livre — Les liens pointent vers les sections) % Doesn't work for me
%  %backref, % liens retour de la bibliographie % Doesn't work for me
%  %implicit=false, % ne redéfinit pas de commandes LaTeX % Il est déjà défini à true au chargement du package !
%  breaklinks,
%  linktoc=all,
%  colorlinks=true,
%  citecolor=Vert, % citations de la bibliographie
%  linkcolor=Bleu, % internes
%  filecolor=Violet, % fichiers
%  urlcolor=Rouge % url
%}
%\usepackage{caption} % loaded after hyperref

5. Test and result

\begin{document}

\frontmatter
%\listoffigures

\mainmatter
\part{Hello}

\chapter{1 Introduction with no Figure}

\chapter{2 Test chapter with Figures} % Not added because there is no illustration
\begin{figure}
  \caption{caption text}
\end{figure}
\begin{figure}
  \caption{caption text}
\end{figure}

\chapter{3 Test chapter with no Figure}

\chapter{4 Another test chapter with WrapFigures} % Not added because there is no illustration
\begin{wrapfigure}{O}{0pt}
  \fbox{$\mathcal{FIGURE}$}
  \caption{caption text}
\end{wrapfigure}
\lipsum[1-4]
\begin{wrapfigure}{O}{0pt}
  \fbox{$\mathcal{FIGURE}$}
  \caption{caption text}
\end{wrapfigure}
\lipsum[1-4]

\part{Part with no Figure}

\chapter{5 Test chapter with no Figure}

\part{Part with illustrations but without chapter at the beginning\\Here the LOF looks good !}

Text. % Necessary to have the part added to the LOF ?!
\parttitle

% What ?! We are still in the previous test chapter with no figure, chapter 5 ?!

% Figure % Ouch ! This figure has moved before the part title ?!
\begin{illustration}
  \caption{caption text}
\end{illustration}

\chapter{6 Test chapter with no illustration}

\chapter{7 Chapter with only WrapFigure as illustration that is added to the LOF}
% Not wrapped !!!
\begin{illustration}*
  \fbox{$\mathcal{FIGURE}$}
  \caption{caption text}
\end{illustration}
\lipsum[1-3]
% Wrapped !!! I don't know why but we have to use these commands !
\illustration*
  \fbox{$\mathcal{FIGURE}$}
  \caption{caption text}
\endillustration
\lipsum[1-3]

\chapter{8 Another test chapter with no illustration}

\chapter{9 Conclusion with illustrations}
% Figure
\begin{illustration}[htbp]
  \caption{caption text}
\end{illustration}
% WrapFigure
\illustration*(O)(0pt)
  \fbox{$\mathcal{FIGURE}$}
  \caption{caption text}
\endillustration
\lipsum[1-2]
% Background image
\clearpage
\backgroundsetup{contents=Draft} % To set « draft » again !!!
\BgThispage
\begin{illustration}
  \caption{draft}
\end{illustration}

\backgroundsetup{contents=}

\backmatter
\listoffigures

\end{document}

enter image description here


6. Bugs

Aaaaargh! The chapter 5 title moved under the following part title. And the figures just at the beginning of the part moved before the part title. I read something on stackexchange on that… To be continued!

user73438
  • 153
  • 6