如何清除文档不同部分之间的 \listoffigures?

如何清除文档不同部分之间的 \listoffigures?

由于我需要用多种语言编写文档,是否可以重置文档不同部分的图表列表?

前两种语言不需要有图形列表,因此我只需要在最后一个图形之前丢弃一次列表。

我可以使用以下命令重置计数器:

\setcounter{figure}{0}
\setcounter{table}{0}
\setcounter{listing}{0}

然而,最终\listoffigures所有数字都会显示出来。

.lof我可以使用 向文件添加任意内容\addcontentsline,但我还没有找到相应的\clearcontents函数。

或者,我想过分别渲染这三篇论文,然后合并 PDF,但这感觉有点儿太老套。

答案1

编辑:查看代码中的更好的版本,但是,顶部解释的基本原理并没有改变!

一个非常快速的破解方法,它连接到\listoffigures\listoftables,为每个推进一个计数器\listof...,并将条目写入1lof、等(对于等也是2lof一样)。1lot

这是通过\ext@lof每次\caption使用时重新定义 etc. 命令来实现的。

\documentclass{book}


\usepackage{xpatch}

\newcounter{lofcntr}
\newcounter{lotcntr}


\newcommand{\clearcontents}{%
  \stepcounter{lofcntr}%
  \stepcounter{lotcntr}%
  \setcounter{figure}{0}
  \setcounter{table}{0}
}

\AtBeginDocument{%
  \stepcounter{lofcntr}%
  \stepcounter{lotcntr}%
}


\makeatletter

\xpretocmd{\caption}{%
  \def\ext@figure{\number\value{lofcntr}lof}
  \def\ext@table{\number\value{lotcntr}lot}
}{\typeout{Worked!}}{}



\xpatchcmd{\listoffigures}{%
  \@starttoc{lof}%
}{%
  \@starttoc{\number\value{lofcntr}lof}%
}{\typeout{Patch success}}{\typeout{Patch failure}}

\xpatchcmd{\listoftables}{%
  \@starttoc{lot}%
}{%
  \@starttoc{\number\value{lotcntr}lot}%
}{\typeout{Patch success}}{\typeout{Patch failure}}

\makeatother




\usepackage{caption}
\usepackage{blindtext}

\begin{document}
\tableofcontents
\listoffigures
\listoftables

\captionof{figure}{Some figure caption}

\captionof{figure}{Some figure caption}
\captionof{figure}{Some figure caption}
\captionof{figure}{Some figure caption}


\captionof{table}{Some table caption for 1st lot}
\captionof{table}{Some table caption for 1st lot}
\captionof{table}{Some table caption for 1st lot}
\captionof{table}{Some table caption for 1st lot}


\clearcontents

\listoffigures
\listoftables


\captionof{figure}{Some figure caption for 2nd lof}
\captionof{figure}{Some figure caption for 2nd lof}
\captionof{figure}{Some figure caption for 2nd lof}
\captionof{figure}{Some figure caption for 2nd lof}


\captionof{table}{Some table caption for 2nd lot}
\captionof{table}{Some table caption for 2nd lot}
\captionof{table}{Some table caption for 2nd lot}
\captionof{table}{Some table caption for 2nd lot}


\end{document}

在此处输入图片描述

更新一些更好的版本:

\documentclass{book}

\usepackage{xparse}
\usepackage{xpatch}
\usepackage{caption}
\usepackage{blindtext}


% Do not a driver counter, i.e. a resetting counter for those two counter fellows here:
\newcounter{lofcntr}
\newcounter{lotcntr}


\NewDocumentCommand{\clearcontents}{}{%
  \stepcounter{lofcntr}% We don't need labels here, I suppose?
  \stepcounter{lotcntr}%
  \setcounter{figure}{0}
  \setcounter{table}{0}
}

\AtBeginDocument{%
  \stepcounter{lofcntr}% 
  \stepcounter{lotcntr}%
}


\makeatletter
% Store the definition of \ext@figure etc. first
\let\latex@ext@figure\ext@figure
\let\latex@ext@table\ext@table

\AtBeginDocument{%
\xpretocmd{\caption}{%
  % Prepend the extension with the number of the current list of ...
  \def\ext@figure{\number\value{lofcntr}\latex@ext@figure}
  \def\ext@table{\number\value{lotcntr}\latex@ext@table}
}{\typeout{Worked!}}{\typeout{Failed miserably!}}
}


\xpatchcmd{\listoffigures}{%
  \@starttoc{lof}%
}{%
  \@starttoc{\number\value{lofcntr}lof}%
}{\typeout{Patch success}}{\typeout{Patch failure}}

\xpatchcmd{\listoftables}{%
  \@starttoc{lot}%
}{%
  \@starttoc{\number\value{lotcntr}lot}%
}{\typeout{Patch success}}{\typeout{Patch failure}}

\makeatother



\begin{document}
\tableofcontents
\listoffigures
\listoftables


\captionof{figure}{Some figure caption for 1st lof}
\captionof{figure}{Some figure caption for 1st lof}
\captionof{figure}{Some figure caption for 1st lof}
\captionof{figure}{Some figure caption for 1st lof}

\captionof{table}{Some table caption for 1st lot}
\captionof{table}{Some table caption for 1st lot}
\captionof{table}{Some table caption for 1st lot}
\captionof{table}{Some table caption for 1st lot}

\clearcontents
\listoffigures
\listoftables


\captionof{figure}{Some figure caption for 2nd lof}
\captionof{figure}{Some figure caption for 2nd lof}
\captionof{figure}{Some figure caption for 2nd lof}
\captionof{figure}{Some figure caption for 2nd lof}


\captionof{table}{Some table caption for 2nd lot}
\captionof{table}{Some table caption for 2nd lot}
\captionof{table}{Some table caption for 2nd lot}
\captionof{table}{Some table caption for 2nd lot}



\end{document}

更新针对mintedlistings环境:

\documentclass{book}

\usepackage{minted}
\usepackage{xparse}
\usepackage{xpatch}
\usepackage{caption}
\usepackage{blindtext}


% Do not use a driver counter, i.e. a resetting counter for those two counter fellows here:
\newcounter{lofcntr}
\newcounter{lotcntr}
\newcounter{lolcntr}

\NewDocumentCommand{\clearcontents}{}{%
  \stepcounter{lofcntr}% We don't need labels here, I suppose?
  \stepcounter{lotcntr}%
  \stepcounter{lolcntr}%
  \setcounter{figure}{0}
  \setcounter{table}{0}
  \setcounter{listing}{0}
}


\makeatletter

\NewDocumentCommand{\advancecontents}{}{%
  \def\ext@figure{\number\value{lofcntr}\latex@ext@figure}
  \def\ext@table{\number\value{lotcntr}\latex@ext@table}%
  \@namedef{ext@listing}{\number\value{lolcntr}\latex@ext@listing}%
}

\AtBeginDocument{%
% Store the definition of \ext@figure etc. first
  \let\latex@ext@figure\ext@figure
  \let\latex@ext@table\ext@table
  \let\latex@ext@listing\ext@listing
  \stepcounter{lofcntr}% 
  \stepcounter{lotcntr}%
  \stepcounter{lolcntr}%
  \advancecontents%
}



\AtBeginDocument{%
\xpretocmd{\caption}{%
  % Prepend the extension with the number of the current list of ...
  \advancecontents%
}{\typeout{Patching caption worked!}}{\typeout{Patching caption failed miserably!}}
}



\xpatchcmd{\listoffigures}{%
  \@starttoc{lof}%
}{%
  \@starttoc{\number\value{lofcntr}lof}%
}{\typeout{Patch success}}{\typeout{Patch failure}}

\xpatchcmd{\listoftables}{%
  \@starttoc{lot}%
}{%
  \@starttoc{\number\value{lotcntr}lot}%
}{\typeout{Patch success}}{\typeout{Patch failure}}


\makeatother



\begin{document}
\tableofcontents
\listoffigures
\listoftables
\listoflistings


\begin{listing}
  \mint{c}
  /* code */
  \caption{A foo listing for 1st lol}
\end{listing}


\begin{listing}
  \mint{c}
  /* code */
  \caption{A foo listing for 1st lol}
\end{listing}

\begin{listing}
  \mint{c}
  /* code */
  \caption{A foo listing for 1st lol}
\end{listing}

\begin{listing}
  \mint{c}
  /* code */
  \caption{A foo listing for 1st lol}
\end{listing}



\captionof{figure}{Some figure caption for 1st lof}
\captionof{figure}{Some figure caption for 1st lof}
\captionof{figure}{Some figure caption for 1st lof}
\captionof{figure}{Some figure caption for 1st lof}

\captionof{table}{Some table caption for 1st lot}
\captionof{table}{Some table caption for 1st lot}
\captionof{table}{Some table caption for 1st lot}
\captionof{table}{Some table caption for 1st lot}

\clearcontents
\listoffigures
\listoftables
\listoflistings

\captionof{figure}{Some figure caption for 2nd lof}
\captionof{figure}{Some figure caption for 2nd lof}
\captionof{figure}{Some figure caption for 2nd lof}
\captionof{figure}{Some figure caption for 2nd lof}


\captionof{table}{Some table caption for 2nd lot}
\captionof{table}{Some table caption for 2nd lot}
\captionof{table}{Some table caption for 2nd lot}
\captionof{table}{Some table caption for 2nd lot}


\begin{listing}
  \mint{c}
  /* code */
  \caption{A foo listing for 2nd lol}
\end{listing}

\begin{listing}
  \mint{c}
  /* code */
  \caption{A foo listing for 2nd lol}
\end{listing}

\begin{listing}
  \mint{c}
  /* code */
  \caption{A foo listing for 2nd lol}
\end{listing}


\end{document}

答案2

以下示例(起点是 Christian Hupfer 的示例)为每种语言定义了一个开关\ifLangA\ifLangB。当语言的部分X开始,然后写入辅助文件,并在语言部分的末尾添加结束符。设置图表列表时,只有当前语言部分通过开关启用,其他条目将被忽略。\ifLangX.lof.lot\fi

当 LaTeX 设置 和 时,它会在输出列表后清除辅助文件\listoffigures和。这可以通过在本地设置 (一个内部开关)来防止,该开关告诉 LaTeX 不要触碰和写入辅助文件(由 所用)。只有最后的图表和表格列表才会正常设置。\listoftables.lof.lot\@fileswfalse\nofiles

完整示例:

\documentclass{book}

\newif\ifLangA
\newif\ifLangB

\newcommand*{\StartLang}[1]{%
  \addtocontents{lof}{%
    \expandafter\protect\csname ifLang#1\endcsname
  }%
  \addtocontents{lot}{%
    \expandafter\protect\csname ifLang#1\endcsname
  }%
  \csname Lang#1true\endcsname
  \expandafter\let\expandafter\LangFalse\csname Lang#1false\endcsname
}
\newcommand*{\StopLang}{%
  \addtocontents{lof}{\protect\fi}%
  \addtocontents{lot}{\protect\fi}%
  \LangFalse
}

\makeatletter
\newcommand*{\ListOfFigures}{%
  \begingroup
    \@fileswfalse
    \listoffigures
  \endgroup
}
\newcommand*{\ListOfTables}{%
  \begingroup
    \@fileswfalse
    \listoftables
  \endgroup
}
\let\LastListOfFigures\listoffigures
\let\LastListOfTables\listoftables
\makeatother

\usepackage{caption}
\usepackage{blindtext}

\begin{document}
\tableofcontents

\StartLang{A}

  \chapter{Lang A}

  \ListOfFigures
  \ListOfTables

  \captionof{figure}{Some figure caption}

  \captionof{figure}{Some figure caption}
  \captionof{figure}{Some figure caption}
  \captionof{figure}{Some figure caption}

  \captionof{table}{Some table caption for 1st lot}
  \captionof{table}{Some table caption for 1st lot}
  \captionof{table}{Some table caption for 1st lot}
  \captionof{table}{Some table caption for 1st lot}

\StopLang

\StartLang{B}

  \chapter{Lang B}

  \LastListOfFigures
  \LastListOfTables

  \captionof{figure}{Some figure caption for 2nd lof}
  \captionof{figure}{Some figure caption for 2nd lof}
  \captionof{figure}{Some figure caption for 2nd lof}
  \captionof{figure}{Some figure caption for 2nd lof}

  \captionof{table}{Some table caption for 2nd lot}
  \captionof{table}{Some table caption for 2nd lot}
  \captionof{table}{Some table caption for 2nd lot}
  \captionof{table}{Some table caption for 2nd lot}

\StopLang

\end{document}

答案3

您可以使用tocbasicKOMA-script 类中的包来创建您自己的事物列表。请参阅 KOMA-script 手册中的第 15 章。

这是一个 MWE,它假设您正在使用 KOMAscrreprt类,并且想要每个章节的图表列表。在序言中,我使用 的函数定义了一种用于第 1 章图表的新浮点数类型,figure1以及一种figure2用于第 2 章图表的类似浮点数类型。这些浮点数的行为类似于常规图表 ( )。(也许可以以某种方式自动执行此操作,以便为每个章节自动编号……)相应的列表列表名为和。实际的列表使用、等插入到文档中。使用类选项将两个列表条目添加到目录中。 tocbasic\DeclareNewTOCfloattype=1lof1lof2\listoftoc{lof1}\listoftoc{lof2}listof=totoc平均能量损失

\documentclass[listof=totoc]{scrreprt}

\usepackage[demo]{graphicx}

\DeclareNewTOC[%
   type=figure1,%
   float,% define a floating environment
   floattype=1,%
   counterwithin=chapter,%
   name=Figure,%
   listname={List of Figures for Chapter 1}%
]{lof1}

\DeclareNewTOC[%
   type=figure2,%
   float,% define a floating environment
   floattype=1,%
   counterwithin=chapter,%
   name=Figure,%
   listname={List of Figures for Chapter 2}%
]{lof2}

\begin{document}
\tableofcontents

\chapter{A chapter}
\label{cha:achapter}
\begin{figure1}
  \centering
  \includegraphics{}
  \caption{A figure in the first chapter}
  \label{fig:achapter}
\end{figure1}

\listoftoc{lof1}

\chapter{Another chapter}
\label{cha:another-chapter}
\begin{figure2}
  \centering
  \includegraphics{}
  \caption{A figure in the second chapter}
  \label{fig:anotherchapter}
\end{figure2}

\listoftoc{lof2}

\end{document}

如果您不使用 KOMA 类之一,请将其添加\usepackage{scrhack,tocbasic}到您的序言中。

答案4

我能说什么呢?这是一个有趣的问题。

\documentclass{article}
\usepackage{float}
\usepackage{caption}

\newfloat{german}{tbp}{lofg}
\floatname{german}{Figur}

\begin{document}

\captionof{figure}{English}
\captionof{german}{Deutsch}

\listoffigures

\listof{german}{Liste Figuren}

\end{document}

相关内容