Zählerwert basierend auf letzten Umgebungen

Zählerwert basierend auf letzten Umgebungen

Wie kann mithilfe von TeX-Makros die Bedingung festgelegt werden, dass die Regel nur die letzte testUmgebung aktivieren soll?

Angenommen, das Dokument enthält nur eine \begin{test}...\end{test} Regel, die aktiviert werden soll.

\begin{test}...\end{test}Wenn das Dokument mehrere Regeln enthält, sollte nur die letzte \begin{test}...\end{test}alleine aktiviert werden.

Hier finden Sie meine MWE-Datei:

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

Antwort1

Sie können verwenden ; wenn sich die Anzahl der Umgebungen ändert, totcountsind zwei LaTeX-Durchläufe erforderlich .test

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

Bildbeschreibung hier eingeben

verwandte Informationen