我想在 LOF 中添加部分和章节。我可以添加章节,但\apptocmd{\@part}
除非我发表评论,否则无法添加\titleformat{\part}
。为什么?
我已经找到了解决方法,\addtoLOF\currentTitle
但我希望了解。
\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}
第二天
同一问题的另一个症状:
\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}
产生以下错误: Undefined control sequence. \parttitle
。因此,\part
当我 时,修补的命令似乎不再使用\titleformat{\part}
。这就是\parttitle
未定义的原因。那么\part
当\titleformat{\part}
使用 时我该如何修补?
第三天
我发现了一些东西。正如这里所建议的:使用 etoolbox \apptocmd 修补时出现错误:“修补命令似乎嵌套在其他命令的参数中”,则如下工作。
\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}
我不太明白……
答案1
我终于有了自己的答案……
我将向图表列表 (LOF) 添加部分和章节。图表列表当然会包含图表,但也包含包装图和背景图像。
\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. 独特的图形、包装图形和背景图像环境:插图
我稍后会用到\AtBeginEnvironment{figure}{…}
。但是包装图不是图形……所以有两个解决方案:
— 重复\AtBeginEnvironment{figure}{…}
for wrapfigures: \AtBeginEnvironment{wrapfigure}{…}
。但是如果我修改了一个的代码,就不能忘记修改另一个。
— 或者为图形和包装图形创建一个独特的环境。
我选择第二个。所以我需要这个xparse
包。
如果您不需要插图环境,请跳至下一点并将“插图”替换为“图形”(否则)。
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 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.修改部分章节定义
这是我遇到的问题。我用的是这个:是否在带有 titletoc 的图表列表中包含章节?,并\expandafter
因此:使用 etoolbox \apptocmd 修补时出现错误:“修补命令似乎嵌套在其他命令的参数中”。
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 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. 将部分和章节写入 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. 还测试了
% 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. 测试及结果
\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}
6. 错误
啊啊啊啊!第 5 章的标题移到了下一部分标题下。而且该部分开头的图移到了部分标题之前。我在 stackexchange 上读到了一些相关内容……未完待续!