0

I'd like to generate "labels" to use at the beginning and at the end of an environment. Unfortunately the value of the counter is modified between the start and the end of the environment:

enter image description here

Any idea how I could avoid that issue?

\documentclass[a4paper,12pt]{article}

\newcounter{nextMarkId}

\newenvironment{subproof}{\thenextMarkId\stepcounter{nextMarkId}}{\thenextMarkId}

\begin{document}

\begin{subproof}
  \begin{subproof}
    blabla
  \end{subproof}
\end{subproof}

I'd like to have: 0 1 blabla 1 0

\end{document}

EDIT

The proposed answer does not work because I want the label of each environment to be unique:

\documentclass[a4paper,12pt]{article}

\newcounter{nextMarkId}

\newenvironment{subproof}
{\thenextMarkId\stepcounter{nextMarkId}}
{\addtocounter{nextMarkId}{-1}\thenextMarkId}

\begin{document}

\begin{subproof}
  \begin{subproof}
    blabla
  \end{subproof}
  \begin{subproof}
    blabla
  \end{subproof}
\end{subproof}

I'd like to have: 0 1 blabla 1 2 blabla 2 0

\end{document}

enter image description here

I was thinking to use some sort of stack... but it seems quite ugly.

3
  • Sorry, but it's not really clear what you want to achieve. Should the next batch of nested subproof environments always start with 0?
    – egreg
    Nov 17, 2021 at 15:23
  • No, each subproof environment should have a different label (or maybe I should call it an identifier). Basically I just want to assign a different identifier to each environment to be able to address them individually later.
    – tobiasBora
    Nov 17, 2021 at 15:25
  • OK, that's what the answer below obtains.
    – egreg
    Nov 17, 2021 at 15:30

1 Answer 1

2

Store the current value and use it at the end of the environment. Since environments form groups, this will do the right thing.

\documentclass[a4paper,12pt]{article}

\newcounter{nextMarkId}

\newenvironment{subproof}
  {\thenextMarkId\edef\thisMarkId{\thenextMarkId}\stepcounter{nextMarkId}}
  {\thisMarkId}

\begin{document}

\begin{subproof}
  \begin{subproof}
    blabla
  \end{subproof}
\end{subproof}

I'd like to have: 0 1 blabla 1 0

\begin{subproof}
  \begin{subproof}
    blabla
    \begin{subproof}
      blabla
    \end{subproof}
  \end{subproof}
  \begin{subproof}
    blabla
  \end{subproof}
\end{subproof}

\end{document}

enter image description here

1
  • Oh, good point, I was not thinking it would work, I forgot environment form groups. Thanks!
    – tobiasBora
    Nov 17, 2021 at 15:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .