Valor del contador basado en los últimos entornos.

Valor del contador basado en los últimos entornos.

¿Cómo poner la condición usando macros TeX de que la regla debería habilitar solo el último testentorno?

Supongamos que el documento contiene solo una \begin{test}...\end{test} regla que debe estar habilitada.

Si hay varios \begin{test}...\end{test}contenidos en la regla del documento, se debe habilitar solo el último \begin{test}...\end{test}.

Encuentre mi archivo 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}

Respuesta1

Puedes usar totcount; Se necesitan dos ejecuciones de LaTeX si testcambia el número de entornos.

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

ingrese la descripción de la imagen aquí

información relacionada