# Redefine a value during runtime inside another macro

I would like to redefine a value during runtime. More precisely, I'm creating my own environment and in the preamble I would like to change a value and set it back afterward.

\newenvironment{reptheorem}[1]%
{%
\makeatletter%
\def\@thmcounterend{new}%In this environment, @thmcounterend has a new value
\makeatother%
}%
{%
\makeatletter%
\def\@thmcounterend{old}%We reset @thmcounterend to its previous value.
\makeatother%
}


Obviously this code doesn't work since the \def is not executed when the environment is created but later.

Any idea?

• You use the word "counter" but a \def means to use it as a macro. If @thmcounterend is, in fact, a pre-existing counter from another package, for example, the proper use of counters is not with \def, but with \setcounter{@thmcounterend}{value}. With your question, there is not enough information to definitively help. – Steven B. Segletes May 23 '14 at 10:30

\makeatletter and \makeatother are given much too late. The category codes are important at the definition time of the environment, when \@thmcounterend gets tokenized:
\makeatletter