# Renew an already used label

Based on my question here my setup is currently looking like:

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


My problem now is that I am basically reusing the label defined the first time. Is there a way to set the label-check in environment B to check the label which was created by the current environment A, and not any labels created earlier?

Here is a variant using Heiko Oberdiek's refcount package:

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


Here is a variant using Heiko Oberdiek's zref package with abspage-module -- in case not all pages are numbered in arabic style:

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