
나는 만 사용 lualatex
하지만 아마도 이것은 일반적인 LaTeX 질문일 것입니다. 이전 버전과의 호환성이 필요하지 않으므로 TeXlive 2023 이상을 사용한다고 가정합니다.
다른 문서도 살펴봤지만 atbegshi
필요한 작업을 수행하지 않거나 필요한 것보다 훨씬 더 복잡합니다. 또한 얼마 전에 누군가 비슷한 질문을 했지만 유용한 답변이 없는 것을 보았습니다.
배송 후크를 사용하여 전역 상태 조작
상황: 두 개의 명령이 있는데, 이를 \foo
과 이라고 부를 것입니다 \unfoo
. 명령 은 맨 위에 \foo
포함되어 있으므로 조판할 때 \clearpage
어디에 나타날지 걱정할 필요가 없습니다 \foo
. 명령 \unfoo
은 로 끝납니다 \clearpage
.
페이지에 이 있으면 \foo
동일한 페이지에도 이 있어야 합니다 \unfoo
. 페이지가 배송되기 \unfoo
전에 사용자가 쓰기에 실패하면 \foo
오류가 발생합니다. 적절한 오류 메시지를 작성하여 컴파일을 중단하는 방법을 알고 있습니다. 페이지 내용을 저장, 편집, 폐기 또는 조작할 필요가 없습니다.
\shipout
내 질문: 이 작업을 수행하기 위해 후크(또는 유사한 것)를 사용하는 간단한 방법이 있습니까 ? 능력이 있어야 할 것 같지만 atbegshi
그 패키지에는 많은 일이 일어나고 있어 긴장됩니다.
MWE:
\documentclass{article} % Compile with lualatex.
\usepackage{fontspec}
\newif\ifusedfoo
\def\foo{\clearpage Hello.\par\usedfootrue}
\def\unfoo{Goodbye.\par\usedfoofalse\clearpage}
% \WhenPageShips{ % This is what I need. I know how to create an error, instead of typeout.
% \ifusedfoo
% \typeout{EEEEK. The page with \string\foo\space did not have \string\unfoo.}
% \fi
% }
\begin{document}
Welcome.\par
\foo
No problem here.\par
\unfoo
\foo % With too many lines on that page, causes problem:
x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\
x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\
x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\
x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\par
% Whether or not there is a later \unfoo, does not matter.
Moving right along\par
\clearpage
This is too late.\par
\unfoo
I cannot wait until this far in the document.\par
\end{document}
또한: 나는 이것을 위해 파일을 사용하고 싶지 않습니다 aux
. 코드는 복잡한 것(예: 참고문헌, 목차 등)에는 나타나지 않습니다. 실행중인 텍스트 내에서.
편집: 문서의 뒷부분에서가 아니라 문제를 신속하게 감지해야 함을 분명히 했습니다.
EDIT2: 허용되는 솔루션은 다음에서 작동합니다.나의 구체적인 상황그러나 일반적인 상황에서는 작동하지 않을 수 있습니다. 댓글을 참조하세요.
답변1
\documentclass{article} % Compile with lualatex.
\usepackage{fontspec}
\newif\ifusedfoo
\def\foo{\clearpage Hello.\par\usedfootrue}
\def\unfoo{Goodbye.\par\usedfoofalse\clearpage}
\AddToHook{shipout/before}[rallg]{%
% \WhenPageShips{ % This is what I need. I know how to create an error, instead of typeout.
\ifusedfoo
\typeout{EEEEK. The page with \string\foo did not have \string\unfoo.}
\fi
}
\begin{document}
Welcome.\par
\foo
No problem here.\par
\unfoo
\foo % With too many lines on that page, causes problem:
x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\
x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\
x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\
x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\\x\par
% Whether or not there is a later \unfoo, does not matter.
\end{document}
두 페이지 모두 가 없기 때문에 마지막 두 페이지에 대한 메시지를 입력합니다 \unfoo
. 두 번째를 피하려면 조건부 논리를 적절하게 관리해야 합니다.