仅当内容

仅当内容

如何才能将图表、表格或列表分别包含内容的情况下将其添加到前言中?

答案1

这是针对类的;通过复制您正在使用的类中的定义,以类似的方式article调整重新定义。\listoftables

\documentclass{article}

\makeatletter
% We don't want to kill the table of contents
\renewcommand\tableofcontents{%
    \section*{\contentsname
        \@mkboth{%
           \MakeUppercase\contentsname}{\MakeUppercase\contentsname}}%
    \global\@printlisttrue
    \@starttoc{toc}%
    }

% redefine \listoftables to check for contents
\renewcommand\listoftables{%
  \check@list{lot}
  \if@printlist
    \section*{\listtablename}%
      \@mkboth{%
          \MakeUppercase\listtablename}%
         {\MakeUppercase\listtablename}%
  \fi
  \@starttoc{lot}%
}
\def\@starttoc#1{%
  \begingroup
    \makeatletter
    \if@printlist
      \@input{\jobname.#1}%
    \fi
    \if@filesw
      \expandafter\newwrite\csname tf@#1\endcsname
      \immediate\openout \csname tf@#1\endcsname \jobname.#1\relax
    \fi
    \@nobreakfalse
  \endgroup}
\newif\if@printlist

% We typeset the file in a box; if the box is empty, we don't input the file for real
\def\check@list#1{%
  \global\@printlistfalse
  \setbox\z@=\vbox{\makeatletter\@input{\jobname.#1}}%
  \ifdim\ht\z@>\z@\global\@printlisttrue\fi}
\makeatother

\begin{document}
\listoftables

%%% uncomment the table for seeing the effect
%\begin{table}[htp]x\caption{y}\end{table}

abc

\end{document}

答案2

您可以使用/我的totalcount包来实现此目的:

\documentclass{article}
\usepackage{listings}
\usepackage[figure,table,lstlisting]{totalcount}

\begin{document}
\iftotalfigures\listoffigures\fi
\iftotaltables\listoftables\fi
\iftotallstlistings\lstlistoflistings\fi

\begin{figure}
\caption{A figure}
\end{figure}

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

\begin{lstlisting}[caption={Useless code},label=useless,captionpos=t]
  for i:=maxint to 0 do
  begin
      { do nothing }
  end;
\end{lstlisting}

\end{document}

请注意,旧版本的totalcount软件包不支持计数器lstlisting,至少需要 v1.0-92 版本。

版本 v1.0-92totalcount可在此处找到:https://sourceforge.net/p/latex-caption/code/HEAD/tree/trunk/tex/(将于今天上传至 CTAN)

答案3

\caption应该使用简单解决方案的草图,并附加额外的限制:listof...位于文档的末尾。

\documentclass{article}


\def\ADPlistoftables{\ifnum\value{table}>0 \listoftables \else  \fi}
\def\ADPlistoffigures{\ifnum\value{figure}>0 \listoffigures \else  \fi}

\begin{document}

%\ADPlistoftables
%\ADPlistoffigures


\begin{figure}[!ht]
     \centering
 \caption{ }   
\end{figure}

\begin{table}[!ht]
 \caption{ }   
\end{table}

\section{First}

\begin{figure}[!ht]
     \centering
 \caption{ }   
\end{figure}

\begin{table}[!ht]
 \caption{ }   
\end{table}

\ADPlistoftables
\ADPlistoffigures


\end{document}

在此处输入图片描述

相关内容