다음 실행에서만 명령을 실행하는 방법은 무엇입니까?

다음 실행에서만 명령을 실행하는 방법은 무엇입니까?

\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

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

관련 정보