
如何使用 TeX 巨集設定條件,規則應僅啟用最後一個test
環境?
假設文檔僅包含一條\begin{test}...\end{test}
應啟用的規則。
如果\begin{test}...\end{test}
文件中包含多個規則,則應僅在最後\begin{test}...\end{test}
單獨啟用。
請找到我的 MWE 檔:
\documentclass{book}
\pagestyle{empty}
\makeatletter
\newif\ifruletest\global\ruletestfalse
\newcounter{testcounter}
\setcounter{testcounter}{0}
\newenvironment{test}{\noindent\parindent0pt}
{\par\stepcounter{testcounter}
\ifnum\value{testcounter}>0\rule{\textwidth}{1bp}\global\ruletestfalse\else\fi\bigskip}
\makeatother
\begin{document}
\begin{test}
Sample text Sample text Sample text Sample text
Sample text Sample text Sample text Sample text
Sample text Sample text Sample text Sample text
Sample text Sample text Sample text Sample text
\end{test}
\begin{test}
Sample text Sample text Sample text Sample text
Sample text Sample text Sample text Sample text
Sample text Sample text Sample text Sample text
Sample text Sample text Sample text Sample text
\end{test}
\begin{test}
Sample text Sample text Sample text Sample text
Sample text Sample text Sample text Sample text
Sample text Sample text Sample text Sample text
Sample text Sample text Sample text Sample text
\end{test}
\end{document}
答案1
您可以使用totcount
;如果test
環境數量發生變化,則需要執行兩次 LaTeX 。
\documentclass{book}
\usepackage{totcount}
\newtotcounter{testcounter}
\newenvironment{test}
{\par\setlength{\parindent}{0pt}}
{\par\stepcounter{testcounter}
\ifnum\totvalue{testcounter}=\value{testcounter}%
\nobreak\rule{\textwidth}{1bp}\bigskip
\fi}
\begin{document}
\begin{test}
Sample text Sample text Sample text Sample text
Sample text Sample text Sample text Sample text
Sample text Sample text Sample text Sample text
Sample text Sample text Sample text Sample text
\end{test}
\begin{test}
Sample text Sample text Sample text Sample text
Sample text Sample text Sample text Sample text
Sample text Sample text Sample text Sample text
Sample text Sample text Sample text Sample text
\end{test}
\begin{test}
Sample text Sample text Sample text Sample text
Sample text Sample text Sample text Sample text
Sample text Sample text Sample text Sample text
Sample text Sample text Sample text Sample text
\end{test}
\end{document}