ファイル内にテーブルがあることを検出するにはどうすればいいですか?

ファイル内にテーブルがあることを検出するにはどうすればいいですか?

私は大学の論文用の 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 ファイルを強迫的に削除する習慣がある場合、フロートの有無の自動検出がうまくいかない可能性があります。(この強迫観念がどれほど一般的であるかに驚かれるでしょう...) したがって、別のアプローチを取ることをお勧めします。つまり、ユーザー自身が指定する 、、nofiguresという3 つのドキュメントクラス レベルのオプションを提供します。これらのオプションが指定されると、それぞれ図のリスト、表のリスト、アルゴリズムのリストは生成されません。notablesnoalgorithms

次のコードで、この提案をより詳しく説明します。このコードでは、新しいオプションを設定してクラスを呼び出す というドキュメント クラス ファイル (もちろん、もっと想像力豊かな名前を選択することもできます) と、ドキュメント クラスを使用するサンプル ユーザー ファイルの両方myclass.clsを設定します。report.texmyclass

追記: @Werner の提案により、3つのユーザーレベルマクロのコードを追加しました: \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}
}

nofigures2 番目は、サンプルのユーザー ドキュメントです。新しいドキュメント クラス オプション ( 、、notablesおよび)の 3 つすべてnoalgorithmsを指定すると、図、表、およびアルゴリズムのリストは作成されないことに注意してください。

\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}

関連情報