Valor do contador baseado nos últimos ambientes

Valor do contador baseado nos últimos ambientes

Como colocar a condição usando macros TeX de que a regra deve ser habilitada apenas no último testambiente?

Suponha que o documento contenha apenas uma \begin{test}...\end{test} regra que deve ser habilitada.

Se \begin{test}...\end{test}houver vários conteúdos no documento, a regra deve ser habilitada apenas por último \begin{test}...\end{test}.

Por favor, encontre meu arquivo 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}

Responder1

Você pode usar totcount; duas execuções de LaTeX serão necessárias se o número de testambientes mudar.

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

insira a descrição da imagem aqui

informação relacionada