Значение счетчика на основе последних сред

Значение счетчика на основе последних сред

Как с помощью макросов 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}

введите описание изображения здесь

Связанный контент