
\foo
TeX の次回実行時にのみ実行したいコマンドがあるとします。これを行うコマンドを作成するにはどうすればよいでしょうか? (ファイルが侵害された場合でも\onceFoo
実行が許可されます。)\foo
aux
最小限の(非)動作例:
\documentclass{article}
\def\foo{First execution}
\def\onceFoo{This doesn't work.}
\begin{document}
testing: \onceFoo
\end{document}
望ましい出力は
testing: First execution
の最初の実行時にlatex
、
testing:
以降の実行のたびに。
上記のファイル(の正しい定義を含む\onceFoo
)を複数回実行し、行new testing: \onceFoo
を追加すると、目的の出力は次のようになります。
testing: new testing: First execution
その後の最初の実行で、
testing: new testing:
それ以降のすべての実行で。
動機: 特定の有用なファイルをクリアして再計算するコマンドを作成したいと思います。通常、このコマンドは破損した情報を削除するために (またはテストのために) 1 回実行し、その後はソース ファイルからコマンドを削除する必要があります。ただし、一部のユーザー (特に私) は、このコマンドによって実際に何かが壊れるわけではなく、多くのものを再計算する必要があるため、効率が大幅に低下するだけなので、コマンドを削除することを忘れる可能性があります。したがって、追加後の最初の実行にのみ影響する、このコマンドの 2 番目のバージョンが必要です。
答え1
\documentclass{article}
\def\foo{First execution}
\newif\ifFirstRun
\makeatletter
\AtBeginDocument{%
\ifx\FirstRunTest\@undefined
\FirstRuntrue
\else
\FirstRunfalse
\fi
\write\@auxout{\string\gdef\string\FirstRunTest{}}}
\makeatother
\begin{document}
testing: \ifFirstRun\foo\fi
\end{document}
ランを数える場合も同様です。
\documentclass{minimal}
\def\foo{First execution}
\makeatletter
\AtBeginDocument{%
\ifx\runNo\@undefined
\newcount\runNo \runNo=1
\else
\advance\runNo by \@ne
\fi
\write\@auxout{%
\string\newcount\string\runNo ^^J
\string\global\runNo=\the\runNo}}
\makeatother
\begin{document}
testing: \ifnum\the\runNo=1\foo\else Run no \the\runNo\fi
\end{document}