续订已使用的标签

续订已使用的标签

根据我的问题这里我的设置目前如下所示:

\newenvironment{A}{\label{env:A}}{}
\newenvironment{B}{
%%Pseudocode
\if\thepage!=\pageofref{env:A}
Pagebreak happened!
\fi
%% End Pseudocode
}{}

\begin{A}
Text
\begin{B}
SubText
\end{B}
\end{A}
\begin{A}
Text2
\begin{B}
SubText2
\end{B}
\end{A}

我现在的问题是,我基本上重复使用了第一次定义的标签。有没有办法在环境中设置标签检查B以检查当前环境创建的标签A,而不是之前创建的任何标签?

答案1

以下是使用 Heiko Oberdiek 的 refcount 包的变体:

\documentclass{article}
\usepackage{refcount}

\newcounter{envAcounter}
\newcounter{envBcounter}

\newenvironment{A}{%
  \stepcounter{envAcounter}%
  \null\label{env:A:start:\arabic{envAcounter}}%
}{\label{env:A:end:\arabic{envAcounter}}}%

\newenvironment{B}{%
  \stepcounter{envBcounter}%
  \null\label{env:B:start:\arabic{envBcounter}}%
  \ifnum\getpagerefnumber{env:A:start:\arabic{envAcounter}}=%
        \getpagerefnumber{env:B:start:\arabic{envBcounter}}\relax%
    Pagebreak did not take place before starting environment B!% 
  \else
    Pagebreak took place before starting environment B!%
  \fi
}{\label{env:B:end:\arabic{envBcounter}}}%

\begin{document}

\begin{A}
Text
\begin{B}
SubText
\end{B}
\end{A}
\begin{A}
Text2
\newpage
\begin{B}
SubText2
\end{B}
\end{A}

\end{document}

以下是使用 Heiko Oberdiek 的 zref 包与 abspage-module 的变体 - 以防并非所有页面都以阿拉伯风格编号:

\documentclass{article}
\usepackage[user,abspage]{zref}

\newcounter{envAcounter}
\newcounter{envBcounter}

\newenvironment{A}{%
  \stepcounter{envAcounter}%
  \null\zlabel{env:A:start:\arabic{envAcounter}}%
}{\zlabel{env:A:end:\arabic{envAcounter}}}%

\begingroup
\makeatletter
\@firstofone{%
  \endgroup
  \newenvironment{B}{%
    \stepcounter{envBcounter}%
    \null\zlabel{env:B:start:\arabic{envBcounter}}%
    \ifnum\zref@extract{env:A:start:\arabic{envAcounter}}{abspage}=%
          \zref@extract{env:B:start:\arabic{envBcounter}}{abspage}\relax%
      Pagebreak did not take place before starting environment B!%
    \else
      Pagebreak took place before starting environment B!%
    \fi
  }{\zlabel{env:B:end:\arabic{envBcounter}}}%
}%

\begin{document}

\begin{A}
Text
\begin{B}
SubText
\end{B}
\end{A}
\begin{A}
Text2
\newpage
\begin{B}
SubText2
\end{B}
\end{A}

\end{document}

相关内容