One can use either cleveref
or hyperref
to get the "pure" name of the environment from the aux file. Unfortunately there are no documented ways on how to do it, so one has to use internal macros (which can change in future versions) in both cases.
First a solution for cleveref
. It uses two internals from the cleveref
package:
- cleveref stores an additional label preceding with "cref@" to hold the environment name
- cleveref offers an internal macro called
\cref@gettype
to get the "pure" environment name
\documentclass{article}
\usepackage{ntheorem}
\usepackage{cleveref}
\newtheorem{theorem}{Theorem}
\makeatletter
\newenvironment{xxxref}[1]{%
\@ifundefined{r@cref@#1}%
{\def\@xxx@env{quote}}% use default environment
{\cref@gettype{#1}{\@xxx@env}}%
\begin{\@xxx@env}}%
{\end{\@xxx@env}}%
\makeatother
\begin{document}
\begin{theorem}
\label{myfirst}
This is the first theorem.
\end{theorem}
\begin{xxxref}{myfirst}
\label{xxxfirst}
This is another environment of the same kind as \ref{myfirst}!.
\end{xxxref}
\begin{xxxref}{myfirst}
This is another environment of the same kind as \ref{xxxfirst}!.
\end{xxxref}
\end{document}
Second, a solution for hyperref
. It uses two internals from the hyperref
package:
- hyperref stores the environment name inside the 4th argument of the label
- The environment name is limited by a period inside that argument.
See also my answer here: Get label target type
\documentclass{article}
\usepackage{ntheorem}
\usepackage{hyperref}
\newtheorem{theorem}{Theorem}
\makeatletter
\newcommand*\@myautoref[2]{% \HyPsd@@@autoref from hyperref, modified
\expandafter\expandafter\expandafter\@@myautoref
\csname r@#2\endcsname{}{}{}{}\@nil#1\@nil
}
\def\@@myautoref#1#2#3#4#5\@nil#6\@nil{% \HyPsd@autorefname, modified
#6#4.\@nil}% Argument #4 = type and number, e.g. "section.1" or "subsection.1.2"
\def\@myreftype#1.#2\@nil{#1}
\newcommand*\beginref{\xxx@ref{\begin}}
\def\endref{\xxx@ref{\end}}
\newcommand*\xxx@ref[2]{%
\@ifundefined{r@#2}%
{\def\@tempa{quote}}% use default environment
{\def\@tempa{\@myautoref\@myreftype{#2}}}%
\edef\@tempa{\noexpand#1{\@tempa}}%
\@tempa}
\makeatother
\begin{document}
\begin{theorem}
\label{myfirst}
This is the first theorem.
\end{theorem}
\beginref{myfirst}
This is another environment of the same kind as \ref{myfirst}!.
\endref{myfirst}
\beginref{xxxfirst}
This is another environment of the same kind as \ref{xxxfirst}!.
\endref{xxxfirst}
\makeatother
\end{document}
hyperref
or a solution based oncleverref
? (Both do store the environment name in the aux file, but in a different way.)\label
for oneself, keeping this working with packages which redefine\label
, too, especiallyhyperref
. So I just posted a solution based oncleveref
and one based onhyperref
.