我想生成在环境开始和结束时使用的“标签”。不幸的是,计数器的值在环境开始和结束之间被修改了:
有什么想法可以避免这个问题吗?
\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}
编辑
建议的答案不起作用,因为我希望每个环境的标签都是唯一的:
\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}
我本来想使用某种堆栈...但它看起来很丑陋。
答案1
存储当前值并在环境结束时使用它。由于环境形成组,因此这将做正确的事情。
\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}