如何檢測文件中是否有表格?

如何檢測文件中是否有表格?

我正在為我們的大學論文編寫一個 LaTeX 模板。我面臨的一個問題如下:

一位學生問我,他的論文不包含任何表格,他查看了一個空的表格清單。我已經把文件放進\listoftables去了.cls,並讓他發表評論。但是,我想知道如何偵測表格的存在並自動停用/啟用任何列表,例如圖形列表、表格列表、演算法列表和類似的東西。

我搜尋了一段時間,但除了向文檔類添加選項之外找不到任何解決方案,如下所示:

\newif\if@alg\@algfalse
\DeclareOption{alg}{
  \@algtrue
}
\ProcessOptions\relax
\if@alg
    \RequirePackage{algorithmicx}
    \RequirePackage{algorithm}
    \RequirePackage{algpseudocode}
    \renewcommand{\thealgorithm}{\arabic{chapter}.\arabic{algorithm}} 
\fi

\if@alg
    \listofalgorithms
    \cleardoublepage
    \newpage
\fi

答案1

如果使用者習慣強制刪除 aux 文件,自動偵測浮動是否存在可能會變得混亂。 (您會驚訝於這種痴迷如此普遍......)因此,我想建議您採取不同的方法:提供三個文檔類級別選項:nofiguresnotablesnoalgorithms,由用戶自己指定。如果指定了這些選項,則不會分別產生圖形清單、表格清單和演算法清單。

下面的程式碼應該更詳細地說明這個建議。它設定了一個名為myclass.cls(顯然您可以自由選擇一個更具想像力的名稱!)的文檔類文件,該文件設置新選項然後調用該類report,以及一個.tex使用該myclass文檔類的示例用戶文件。

附錄:根據@Werner的建議,我新增了三個使用者層級巨集的程式碼:\nofigures\notables\noalgorithms。它們可以由使用者插入到序言中,作為在該\documentclass階段提供相應選項的替代方法

首先,類別文件的程式碼(儲存為myclass.cls):

\NeedsTeXFormat{LaTeX2e}[2015/01/01]
\ProvidesClass{myclass}[2018/02/21]

% define three new documentclass-level options
\newif\ifnofigures\nofiguresfalse
\newif\ifnotables\notablesfalse
\newif\ifnoalgorithms\noalgorithmsfalse
\DeclareOption{nofigures}{\nofigurestrue}
\DeclareOption{notables}{\notablestrue}
\DeclareOption{noalgorithms}{\noalgorithmstrue}

\ProcessOptions\relax
\LoadClass[]{report}

% provide three user commands: \nofigures, \notables, \noalgorithms
% (to be used as an alternative to setting documentclass-level options)
\newcommand\nofigures{\let\ifnofigures\iftrue}
\newcommand\notables{\let\ifnotables\iftrue}
\newcommand\noalgorithms{\let\ifnoalgorithms\iftrue}

\usepackage{algorithmicx,algorithm,algpseudocode}
% load any and all other default packages

\AtBeginDocument{%
   \pagenumbering{roman}
   \maketitle % or, likely, something far more elaborate
   \ifnofigures\else\listoffigures\fi
   \ifnotables\else\listoftables\fi
   \ifnoalgorithms\else\listofalgorithms\fi
   \clearpage
   \pagenumbering{arabic}
}

其次,範例使用者文件。請注意,如果指定了所有三個新文件類別選項 -- nofiguresnotablesnoalgorithms--,則不會建立圖形、表格和演算法的清單。

\documentclass[nofigures,notables,noalgorithms]{myclass}

\title{Thoughts}
\author{A. Person}
\date{\today}

\begin{document}
xxx
\end{document}

答案2

您可以使用 Totalcount 套件。但請注意,它需要編譯以獲得表格或圖形的清單。

\documentclass{book}
\RequirePackage[figure,table]{totalcount}
\begin{document}
\iftotaltables
   \listoftables %no list of tables
\fi
\iftotalfigures
   \listoffigures
\fi

\chapter{A}
\begin{figure}
\caption{a figure}
\end{figure} 
\chapter{B}
blblb 
\end{document}

相關內容