Tocloft カスタムリストが空かどうかをチェックする

Tocloft カスタムリストが空かどうかをチェックする

パッケージを使用して、「不足しているもののリスト」というカスタム リストを定義しましたtocloft。リストにはいくつかの項目が含まれており、listofコマンドはそれぞれのリストを表示します。基本的に、このリストは項目が含まれている場合にのみ表示されます。それ以外の場合は、何も印刷しないようにします。これを使用して、論文にまだ追加する必要があるものをマークし、不足しているものがすべて何らかの方法で解決されたら、コードから他のものを削除しないようにします。

私が望んでいるのは、互換性のために、追加パッケージをできるだけ少なくすることです。私はすでにこれを.sty形にしており、それが役に立つとわかったら、いつかアップロードすることを検討しています。

だから私は考え私がする必要があったのは、 コマンドによって作成されたカウンターを確認することでしたlistof。 を使用しようとしました\ifnum\value{missingthings}=0が、 の直後に コマンドを使用しているため\listoftables、カウンターの値missingthingsは 0.0 です。ファイルにtest.lomtコンテンツの行が含まれているかどうかを確認する必要があると考えました。

これをどうやって実行すればいいのでしょうか? ここでパッケージを使用したりetoc、.toc ファイルをチェックしたりする同様のトピックを読んだことがありますが、私の場合はあまり役に立ちませんね。

また、私の の何が問題なのでしょうか?が文書の先頭に配置されていても印刷されるifnumため、 は正常に動作していないようです。\listofmissingthings

MWE:

\documentclass{scrartcl}

\usepackage{tocloft}

\newlistof[section]{missingthings}{lomt}{List of Missing Things}

\newcommand{\missingthing}[2]{%
\refstepcounter{missingthings}
\par\noindent\textbf{Missing Thing \themissingthings: #1.} \newline #2
\addcontentsline{lomt}{missingthings}{\protect\numberline{\themissingthings}#1}\par}

\let\oldlistofmissingthings\listofmissingthings%
\renewcommand{\listofmissingthings}{ % Makes LoMT show up in ToC
    \ifnum\value{missingthings}=0%
    \oldlistofmissingthings%
    \addcontentsline{toc}{section}{List of Missing Things}%
    \else%
    %
    \fi%
}%

\begin{document}
\tableofcontents
\listofmissingthings

The current value of the counter missingthings is \themissingthings.
\section{Example}
\missingthing{First Text goes here}{This text only appears in my document, but not in my List of Missing Things}
\section{Another Example}
The current value of the counter missingthings is \themissingthings.
\end{document}

PS: を KOMA スクリプト クラスと一緒に使用することtocloftは推奨されないことは承知しています。ドキュメント クラスを変更する勇気がないので、これは別の日の話題です。

答え1

2 つの新しいカウンターと補助 ( ) ファイルを使用すると*.aux、コマンド内でカウンター チェックを実行して、\listofmissingthingsこのコマンドが出力を行うか、何も行わないかを決定することができます。正しい出力を得るには、2 回実行する必要があります。

2 つの新しいカウンターは次のように定義されます。

\newcounter{writecn}
\newcounter{existcn}

最初は、すべて 0 に等しくなります。

\missingthing次に、コマンドの定義に以下を追加します。

\ifnum\value{writecn}=0\immediate\write\@auxout{\string\setcounter{existcn}{1}}\fi%
\ifnum\value{writecn}=1\else\setcounter{writecn}{1}\fi%

最初の行は、すぐに aux ファイルに行を書き込みます。これにより、コマンドの前に\setcounter{existcn}{1}コマンドが実行されても、カウンターが0 ではなく 1 になることが保証されます。2 行目は、カウンターが 1 でない場合にカウンターを 1 に設定するだけです。このようにして、複数のコマンドが発行された場合でも、最初の行は 1 回だけ実行されます。\listofmissingthings\missingthingexistcnwritecn\missingthing

そして、の renewcommand 定義で\listofmissingthings、カウンタを使用して、existcnリストとをaddtocontentsline出力に含める必要があるかどうかを判断します。次のようにします。

\renewcommand{\listofmissingthings}{ % Makes LoMT show up in ToC
    \ifnum\value{existcn}=1
    \oldlistofmissingthings%
    \addcontentsline{toc}{section}{List of Missing Things}
    \else\fi%%
}%

完全な動作例:

\documentclass{scrartcl}
\newcounter{writecn}
\newcounter{existcn}
\usepackage{tocloft}
\newlistof[section]{missingthings}{lomt}{List of Missing Things}

\makeatletter
\newcommand{\missingthing}[2]{%
\ifnum\value{writecn}=0\immediate\write\@auxout{\string\setcounter{existcn}{1}}\fi%
\ifnum\value{writecn}=1\else\setcounter{writecn}{1}\fi%
\refstepcounter{missingthings}%
\par\noindent\textbf{Missing Thing \themissingthings: #1.} \newline #2
\addcontentsline{lomt}{missingthings}{\protect\numberline{\themissingthings}#1}\par}
\makeatother

\let\oldlistofmissingthings\listofmissingthings%
\renewcommand{\listofmissingthings}{ % Makes LoMT show up in ToC
    \ifnum\value{existcn}=1
    \oldlistofmissingthings%
    \addcontentsline{toc}{section}{List of Missing Things}
    \else\fi%%
}%

\begin{document}
\tableofcontents
\listofmissingthings
The current value of the counter missingthings is \themissingthings.
\section{Example}
\missingthing{First Text goes here}{This text only appears in my document, but not in my List of Missing Things}
\section{Another Example}
\missingthing{First Text goes here}{This text only appears in my document, but not in my List of Missing Things}
The current value of the counter missingthings is \themissingthings.
\end{document}

関連情報