有条件的列表 - 仅当列表存在时才显示 lstlistoflistings

有条件的列表 - 仅当列表存在时才显示 lstlistoflistings

如图所示此主题此主题仅当在文档中使用这些项目时,才可以有条件地创建图表列表。

我想将其扩展到列表。只要您lstlisting在文档中至少使用一次环境,一切都会正常工作。通常,我会在代码中包含\lstinputlisting。因此,lstlisting不使用环境。因此,\AtEndEnvironmet永远不会调用,并且命令\conditionalLoL永远不会设置为 true。

有没有办法修改的定义以\conditionalLoL包括\lstinputlisting


目前代码:

\documentclass[listof=totoc]{scrreprt}

\usepackage[latin1]{inputenx}
\usepackage[T1]{fontenc}
\usepackage[ngerman,english]{babel}
\usepackage{etoolbox}
\usepackage{graphicx}
\usepackage{listings}
\usepackage{tabularx}

% https://tex.stackexchange.com/questions/33193/how-to-determine-whether-a-list-of-figures-is-empty-then-not-show-it-at-all
\makeatletter
  % Figure
  \AtEndEnvironment{figure}{\gdef\there@is@a@figure{}}%
  \AtEndDocument{\ifdefined\there@is@a@figure\label{fig:was:used:in:doc}\fi}%
  \newcommand{\conditionalLoF}{\@ifundefined{r@fig:was:used:in:doc}{}{\listoffigures}}%
  % Table
  \AtEndEnvironment{table}{\gdef\there@is@a@table{}}%
  \AtEndDocument{\ifdefined\there@is@a@table\label{tab:was:used:in:doc}\fi}%
  \newcommand{\conditionalLoT}{\@ifundefined{r@tab:was:used:in:doc}{}{\listoftables}}%
  % Listing
  \AtEndEnvironment{lstlisting}{\gdef\there@is@a@listing{}}%
  \AtEndDocument{\ifdefined\there@is@a@listing\label{lst:was:used:in:doc}\fi}%
  \newcommand{\conditionalLoL}{\@ifundefined{r@lst:was:used:in:doc}{}{\lstlistoflistings}}%
  % 
\makeatother

\begin{filecontents*}{command.txt}
Line 1
Line 2
\end{filecontents*}

\begin{document}

\clearpage
\tableofcontents

\conditionalLoF
\let\LaTeXStandardClearpage\clearpage
\let\clearpage\relax
\conditionalLoT
\conditionalLoL
\let\clearpage\LaTeXStandardClearpage % Return to the old definition

\vspace{1cm}

A figure:

\begin{figure}[htbp]
\centering
\includegraphics[width=0.25\linewidth]{example-image-a}
\caption{A picture}
\label{fig:label}
\end{figure}

A table:

\begin{table}[htbp]
\centering
\caption{A table}
\label{tab:label}
\begin{tabularx}{\linewidth}{lX}
test & test test test test test test test test test test test test test test test test test test test test
\end{tabularx}
\end{table}

A listing environment:

% \begin{lstlisting}[label=lst:label1,caption=A listing]
% command
% command
% \end{lstlisting}

A lstinputlisting:

\lstinputlisting[label=lst:label2,caption=Another listing]{command.txt}

\end{document}

lstlisting 环境的结果:

带有列表


仅使用 listinputlisting 的结果:

在此处输入图片描述

更新

使用@Axel Sommerfeldt 的答案,我仍然得到了原始文档的空表列表。我发现这是由我的 documentclass 中的环境引起的。以下是导致空表列表的longtabuMWE :longtabu

\documentclass[listof=totoc]{scrreprt}

\usepackage[latin1]{inputenx}
\usepackage[T1]{fontenc}
\usepackage[ngerman,english]{babel}
\usepackage{etoolbox}
\usepackage{graphicx}
\usepackage{listings}
\usepackage{longtable}
\usepackage{tabu}
\usepackage{tabularx}
\usepackage[figure,table,lstlisting]{totalcount}

\newcommand\conditionalLoF{%
  \iftotalfigures\listoffigures\fi}
\newcommand\conditionalLoT{%
  \iftotaltables\listoftables\fi}
\newcommand\conditionalLoL{%
  \iftotallstlistings\lstlistoflistings\fi}

\begin{filecontents*}{command.txt}
Line 1
Line 2
\end{filecontents*}

\begin{document}

\clearpage
\tableofcontents

\conditionalLoF
\let\LaTeXStandardClearpage\clearpage
\let\clearpage\relax
\conditionalLoT
\conditionalLoL
\let\clearpage\LaTeXStandardClearpage % Return to the old definition

\vspace{1cm}

\newpage

\chapter{Tests}

\section{Figures}

A figure:

\begin{figure}[htbp]
\centering
\includegraphics[width=0.25\linewidth]{example-image-a}
\caption{A picture}
\label{fig:label}
\end{figure}

A non-floating picture:

\includegraphics[width=0.25\linewidth]{example-image-a}

\section{Tables}

% A table:
% 
% \begin{table}[htbp]
% \centering
% \caption{A table}
% \label{tab:label}
% \begin{tabularx}{\linewidth}{lX}
% test & test test test test test test test test test test test test test test test test test test test test
% \end{tabularx}
% \end{table}
% 
% A non-floating table:
% 
% \begin{tabularx}{\linewidth}{lX}
% test & test test test test test test test test test test test test test test test test test test test test
% \end{tabularx}

A non-floating longtabu

\begin{longtabu} to \textwidth {lX}
test & test test test test test test test test test test test test test test test test test test test test
\end{longtabu}

\section{Listings}

A listing environment:

\begin{lstlisting}[label=lst:label1,caption=A listing environment]
command
command
\end{lstlisting}

A lstinputlisting:

\lstinputlisting[label=lst:label2,caption=A lstinputlisting]{command.txt}

\end{document}

我快速浏览了一下tabu包文档。但我不确定为什么表格的计数器被添加到非浮动longtabutotalcount包裹。

有任何想法吗?

答案1

使用包的解决方案totalcount

\usepackage[figure,table,lstlisting]{totalcount}

\newcommand\conditionalLoF{%
  \iftotalfigures\listoffigures\fi}
\newcommand\conditionalLoT{%
  \iftotaltables\listoftables\fi}
\newcommand\conditionalLoL{%
  \iftotallstlistings\lstlistoflistings\fi}

完整示例文档:

\documentclass[listof=totoc]{scrreprt}

\usepackage[latin1]{inputenx}
\usepackage[T1]{fontenc}
\usepackage[ngerman,english]{babel}
\usepackage{etoolbox}
\usepackage{graphicx}
\usepackage{listings}
\usepackage{tabularx}
\usepackage[figure,table,lstlisting]{totalcount}

\newcommand\conditionalLoF{%
  \iftotalfigures\listoffigures\fi}
\newcommand\conditionalLoT{%
  \iftotaltables\listoftables\fi}
\newcommand\conditionalLoL{%
  \iftotallstlistings\lstlistoflistings\fi}

\begin{filecontents*}{command.txt}
Line 1
Line 2
\end{filecontents*}

\begin{document}

\clearpage
\tableofcontents

\conditionalLoF
\let\LaTeXStandardClearpage\clearpage
\let\clearpage\relax
\conditionalLoT
\conditionalLoL
\let\clearpage\LaTeXStandardClearpage % Return to the old definition

\vspace{1cm}

A figure:

\begin{figure}[htbp]
\centering
\includegraphics[width=0.25\linewidth]{example-image-a}
\caption{A picture}
\label{fig:label}
\end{figure}

A table:

\begin{table}[htbp]
\centering
\caption{A table}
\label{tab:label}
\begin{tabularx}{\linewidth}{lX}
test & test test test test test test test test test test test test test test test test test test test test
\end{tabularx}
\end{table}

A listing environment:

% \begin{lstlisting}[label=lst:label1,caption=A listing]
% command
% command
% \end{lstlisting}

A lstinputlisting:

\lstinputlisting[label=lst:label2,caption=Another listing]{command.txt}

\end{document}

与其他答案给出的解决方案相反,此解决方案实际上基于计数器使用(而不是环境使用)。因此,例如,此解决方案在使用时\captionof也有效,而其他解决方案则失败:

\documentclass[listof=totoc]{scrreprt}

\usepackage{graphicx}

\usepackage{caption}
\usepackage[figure,table]{totalcount}

\newcommand\conditionalLoF{%
  \iftotalfigures\listoffigures\fi}

\begin{document}

\tableofcontents

\conditionalLoF

\vspace{1cm}

A figure:

\begin{minipage}{\textwidth}
\centering
\includegraphics[width=0.25\linewidth]{example-image-a}
\captionof{figure}{A picture}
\label{fig:label}
\end{minipage}

\end{document}

一些补充说明:

您的问题的主要问题是没有完美的解决方案。

如果您向环境添加标志(就像 Werner 的解决方案那样),您将错过图形/表格列表中的条目。如果您向、和添加标志tablelongtablelongtabu将错过threeparttablewraptable和许多其他标志。如果您也向它们添加标志,您将错过\captionof{table}完全独立于所用环境的标志。(并且您还会错过其他条目。)并且table没有\caption也会导致错误的结果。

如果您使用计数器方法(例如我使用 的解决方案totalcount),则代码会遇到问题,因为代码会增加计数器,但不会在表格列表中放置条目,例如没有 的longtable或。您可以在使用这些环境后通过减少计数器来纠正此问题。但是带有 的表格呢?这确实添加了标题,确实增加了表格计数器,但也不会在表格列表中产生条目。longtabu\caption\caption[]{...}

第三种方法是修补\caption。但并非每个增加图形或表格计数器的环境都使用\caption,例如包hvfigure提供的环境hvfloat\caption修补什么?有许多不同的命令,例如,和\caption将使用完全不同的代码。我的包试图修补许多版本(LaTeX 内核版本和 17 个附加包),但不是全部。因此,如果我在我的包中提供解决方案,它将不适用于所有环境,例如包定义的环境,因为包不适用于此包。(我不知道,必须在下个周末仔细研究。我也将在下个周末使用我的包提供替代解决方案。)tablelongtablecaption\captioncaptionalgorithm2ecaptionlongtabucaption

因此,很遗憾,您必须选择最适合您需求的解决方案。没有完美的解决方案,因为任何包/环境/命令都可以自由增加计数器,也可以使用自己的代码自由向图表/表格列表添加内容。

附录 2016-03-13 正如承诺的那样,有一个不同的解决方案\addcontentsline可以修补处理条件列表:

\documentclass{article}

\usepackage{caption}
\usepackage{listings,longtable,tabu}

\begin{filecontents*}{command.txt}
Line 1
Line 2
\end{filecontents*}

\begin{filecontents*}{iflistof.sty}
\NeedsTeXFormat{LaTeX2e}[1994/12/01]
\ProvidesPackage{iflistof}[2016/03/12 v0.1 Offers conditional "List of" (AS)]
\providecommand*\iflistof[1]{%
  \@ifundefined{iflistof@#1}\@secondoftwo\@firstoftwo}
\let\ORI@addcontentsline\addcontentsline
\renewcommand*\addcontentsline[2]{%
  \caption@iflistof{iflistof@#2}%
  \ORI@addcontentsline{#1}{#2}}
\newcommand*\caption@iflistof[1]{%
% \@nameuse{#1}{%
    \global\@namedef{#1}{\@secondoftwo}%
    \if@filesw
      \immediate\write\@mainaux{%
        \string\global\string\@namedef{#1}{\string\@firstoftwo}}%
    \fi}%{}}
\endinput
\end{filecontents*}

\usepackage{iflistof}

\begin{document}
\iflistof{figure}{\listoffigures}{}
\iflistof{table}{\listoftables}{}
\iflistof{lstlisting}{\lstlistoflistings}{}
\clearpage

%\begin{table}
%  \caption{A table}
%\end{table}

\begin{longtable}{ll}
\caption{A longtable}\\
\end{longtable}

\begin{longtabu}{ll}
\caption{A longtable}\\
\end{longtabu}

% \begin{lstlisting}[label=lst:label1,caption=A listing]
% command
% command
% \end{lstlisting}

\lstinputlisting[label=lst:label2,caption=Another listing]{command.txt}

\end{document}

这似乎是最好的方法,但在赞扬它之前,请先尝试一下并报告。如果效果不错,我可以将该iflistof包添加到我的“标题包包”中。

也可以看看:https://sourceforge.net/p/latex-caption/tickets/37/

答案2

尝试一下(嗯,我的;-))包xassoccnt,它提供了一些totcount功能和相关的计数器不是如果figure等计数器设置为零,则重置,即,它们会不断增加(除非直接操作) - 结合Total计数器功能,这些值会存储到.aux文件中并在第二次运行中可用。

稍微重新定义\listof....检查计数器值是否大于零并加载相关的原始\listof...命令。如果计数器为零,则不包含任何内容。

\documentclass{scrartcl}

\usepackage[latin1]{inputenx}
\usepackage[T1]{fontenc}
\usepackage[ngerman,english]{babel}
\usepackage{etoolbox}
\usepackage{xassoccnt}
\usepackage{graphicx}
\usepackage{listings}
\usepackage{tabularx}

\NewTotalDocumentCounter{totalfigures}
\NewTotalDocumentCounter{totaltables}
\NewTotalDocumentCounter{totallistings}

\DeclareAssociatedCounters{lstlisting}{totallistings}
\DeclareAssociatedCounters{figure}{totalfigures}
\DeclareAssociatedCounters{table}{totaltables}

\makeatletter
\renewcommand{\TotalValue}[1]{%
  \value{xassoccnt@total@#1}%
}


\let\@@latex@@listoffigures\listoffigures
\let\@@latex@@listoftables\listoftables
\let\@@latex@@lstlistoflistings\lstlistoflistings

\renewcommand{\listoffigures}{%
  \ifnum\TotalValue{totalfigures} > 0 
  \@@latex@@listoffigures%
  \fi
}


\renewcommand{\listoftables}{%
  \ifnum\TotalValue{totaltables} > 0 
  \@@latex@@listoftables%
  \fi
}


\renewcommand{\lstlistoflistings}{%
  \ifnum\TotalValue{totallistings} > 0 
  \@@latex@@lstlistoflistings%
  \fi
}

\makeatother





\begin{filecontents*}{command.txt}
Line 1
Line 2
\end{filecontents*}

\begin{document}
\tableofcontents

\clearpage

\lstlistoflistings

\listoftables

\listoffigures


A figure:

\begin{figure}[htbp]
\centering
\includegraphics[width=0.25\linewidth]{example-image-a}
%\caption{A picture}
\label{fig:label}
\end{figure}

A table:

\begin{table}[htbp]
\centering
\caption{A table}
\label{tab:label}
\begin{tabularx}{\linewidth}{lX}
test & test test test test test test test test test test test test test test test test test test test test
\end{tabularx}
\end{table}

A listing environment:

\begin{lstlisting}[label=lst:label1,caption=A listing]
  command
  command
\end{lstlisting}

A lstinputlisting:

\lstinputlisting[label=lst:label2,caption=Another listing]{command.txt}

\end{document}

答案3

显然,代码依赖于 的存在figuretable并且lstlisting 环境。但是,\lstinputlisting不使用lstlisting环境(内部),因此它不能按预期运行。

下面的代码\lstinputlisting通过在它前面添加用于检测列表是否存在的条件组件来进行更改:

\makeatletter
\let\oldlstinputlisting\lstinputlisting
\renewcommand{\lstinputlisting}{\gdef\there@is@a@listing{}\oldlstinputlisting}
\makeatother

这是一个完整的最小示例:

在此处输入图片描述

\documentclass{scrartcl}

\usepackage{etoolbox,listings}

% http://tex.stackexchange.com/questions/33193/how-to-determine-whether-a-list-of-figures-is-empty-then-not-show-it-at-all
\makeatletter
  % Figure
  \AtEndEnvironment{figure}{\gdef\there@is@a@figure{}}%
  \AtEndDocument{\ifdefined\there@is@a@figure\label{fig:was:used:in:doc}\fi}%
  \newcommand{\conditionalLoF}{\@ifundefined{r@fig:was:used:in:doc}{}{\listoffigures}}%
  % Table
  \AtEndEnvironment{table}{\gdef\there@is@a@table{}}%
  \AtEndDocument{\ifdefined\there@is@a@table\label{tab:was:used:in:doc}\fi}%
  \newcommand{\conditionalLoT}{\@ifundefined{r@tab:was:used:in:doc}{}{\listoftables}}%
  % Listing
  \AtEndEnvironment{lstlisting}{\gdef\there@is@a@listing{}}%
  \let\oldlstinputlisting\lstinputlisting
  \renewcommand{\lstinputlisting}{\gdef\there@is@a@listing{}\oldlstinputlisting}
  \AtEndDocument{\ifdefined\there@is@a@listing\label{lst:was:used:in:doc}\fi}%
  \newcommand{\conditionalLoL}{\@ifundefined{r@lst:was:used:in:doc}{}{\lstlistoflistings}}%
  % 
\makeatother

\begin{filecontents*}{command.txt}
Line 1
Line 2
\end{filecontents*}

\begin{document}

\clearpage
\tableofcontents

\conditionalLoF
\let\LaTeXStandardClearpage\clearpage
\let\clearpage\relax
\conditionalLoT
\conditionalLoL
\let\clearpage\LaTeXStandardClearpage % Return to the old definition

\vspace{1cm}

A figure:

\begin{figure}[htbp]
  \centering
  \caption{A picture}
\end{figure}

A table:

\begin{table}[htbp]
  \centering
  \caption{A table}
\end{table}

A listing environment:

% \begin{lstlisting}[label=lst:label1,caption=A listing]
% command
% command
% \end{lstlisting}

A lstinputlisting:

\lstinputlisting[label=lst:label2,caption=Another listing]{command.txt}

\end{document}

相关内容