Extended reference for enumerate item using parent environment
Here is my suggestion how to do it. I only used the very powerful zref
-package which can store a lot more (label)informations than the standard label
-command. Additionally there are new commands (\entityLabel
and \entityRef
) which are used in the entity
environment. I don't want to explain all lines of the given code, because I think using the zref
-package documentation in parallel and the commented lines makes it easy to understand the code.
\documentclass{scrartcl}
\usepackage{zref-user}
\usepackage[colorlinks]{hyperref}
\makeatletter
% new properties in zref
\zref@newprop{EntityName}{}
\zref@newprop{EntityTag}{}
\zref@newprop{EntityCounter}{}
\zref@newprop{ItemCounter}{}
% add the new properties to the main list
\zref@addprop{main}{EntityName}
\zref@addprop{main}{EntityTag}
\zref@addprop{main}{EntityCounter}
\zref@addprop{main}{ItemCounter}
% define some hyper references
\def\entityRef@EntityName#1{%
\def\tag{\zref@extractdefault{\zprefix#1}{EntityTag}{}}%
\def\name{\zref@extractdefault{\zprefix#1}{EntityName}{}}%
\hyperref[\zprefix\tag]{\name}}
\def\entityRef@EntityCounter#1{%
\def\tag{\zref@extractdefault{\zprefix#1}{EntityTag}{}}%
\def\count{\zref@extractdefault{\zprefix#1}{EntityCounter}{0}}%
\hyperref[\zprefix\tag]{\count}}
\def\entityRef@ItemCounter#1{%
\def\icount{\zref@extractdefault{\zprefix#1}{ItemCounter}{0}}%
\hyperref[\zprefix#1]{\icount}}
\def\entityRef@EntityPage#1{%
\def\pagenum{\zref@extractdefault{\zprefix#1}{page}{0}}%
\hyperpage{\pagenum}}
% counter for the entities
\newcounter{EntityCounter}
% prefix for all labels and references
\def\zprefix{entity:}
% boolean to check if we work inside an entity environment
\newif\ifIsInEntity
% the new entity environment
\newenvironment{entity}[1]{%
\IsInEntitytrue
\refstepcounter{EntityCounter}%
\def\entityName{#1}
\def\entityTag{Entity\theEntityCounter}
\addcontentsline{toc}{section}{\theEntityCounter. Entity: #1}
\noindent\textbf{\label{\zprefix\entityTag}\theEntityCounter. Entity: #1}%
\begin{enumerate}
}{%
\IsInEntityfalse
\end{enumerate}
}%
% a new label
\def\entityLabel#1{%
\zref@setcurrent{EntityName}{\entityName}%
\zref@setcurrent{EntityTag}{\entityTag}%
\zref@setcurrent{EntityCounter}{\the\value{EntityCounter}}%
\zref@setcurrent{ItemCounter}{\theenumi}%
\zlabel{\zprefix#1}\label{\zprefix#1}}
% a new ref
% the output depends on the place where the reference is used
\def\entityRef#1{%
\ifIsInEntity
% reference is inside an entity
% compare the saved EntityCounter with the current EntityCounter
\ifnum\zref@extractdefault{\zprefix#1}{EntityCounter}{-1}=\theEntityCounter
% reference is inside the same entity
see Item \entityRef@ItemCounter{#1}%
\else
% reference is in an other entity
see \entityRef@EntityCounter{#1}.\entityRef@ItemCounter{#1}%
\fi
\else
% reference is outside of an entity
see Entity "\entityRef@EntityName{#1}" (Nr. \entityRef@EntityCounter{#1}) Item \entityRef@ItemCounter{#1} on page \entityRef@EntityPage{#1}%
\fi
}
\makeatother
\begin{document}
\tableofcontents
\newpage
\begin{entity}{First Test}
\item Some text with label \entityLabel{Entity1Item1}
\item Some more text, oh and \entityLabel{Entity1Item2}
\item More text \entityRef{Entity1Item1}
\end{entity}
\noindent
Refernce in text: \entityRef{Entity1Item1}\\
Refernce in text: \entityRef{Entity2Item2}\\
Refernce in text: \entityRef{Entity3Item2}\\
\begin{entity}{Second Test}
\item Referencing \entityRef{Entity2Item2}
\item Referencing \entityLabel{Entity2Item2}\entityRef{Entity1Item2}
\item Referencing \entityRef{Entity1Item2}
\end{entity}
\newpage
\begin{entity}{Third Test}
\item Referencing \entityRef{Entity1Item1}
\item Referencing \entityLabel{Entity3Item2}\entityRef{Entity2Item2}
\item Referencing \entityRef{Entity1Item2}
\end{entity}
\end{document}