General ⇒ Full context variable depth references to list labels?
Full context variable depth references to list labels?
What I try to do is the following: In a document, I will have enumerate lists at varying depths in the section structure, e.g., I will have some enumerate lists that occur within a section, while others occur in a subsection, etc.
I now need to cross-reference them in full context (sometimes called "legal style"), e.g., list item 3 in section 2.1 should be referenced ad "2.1.3", whereas list item 4 in subsection 2.1.1 should be referenced as "2.1.1.4". (Just the in-text reference should be "2.1.1.4". The list item label in the enumerate environment should still read "4", however, and not "2.1.1.4".)
Annoyingly, I can get Microsoft Word to do this but after a full night of google, forum posts, and digging through CTAN, all I can do is either:-
- construct the reference manually after storing the section counters for each label, e.g., through package "smartref"
- extend the label with additional counters, e.g., using the ref= option of package "enumitem"
also, all direct counter redefinitions seem to link one counter directly to a "level" above.
All the above thus do not work to link a list label to the deepest actually used sectioning command in which it is nested.
I'd be most grateful for a pointer.
Many thanks,
David.
Learn LaTeX easily with newest books:
The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis
The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more
LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis
Re: Full context variable depth references to list labels?
I had hoped that
\def\savecounter{\protected@edef\savedlabel\@currentlabel}
would store the current label in \savecounter but I can tell that this does not work. Clearly, I don't understand what the above code does and "coding by analogy" from the latex source was apparently not a good strategy.
I had hoped to then say something like
\item\copycounter\label{foo}
to add the saved sectioning label back in front of the new item label, with something like
\def\copycounter{\protected@edef\@currentlabel{\savedlabel-\@currentlabel}}
but, again, this does not do what I thought it should.
Any comments most gratefully received,
David.
Full context variable depth references to list labels?
See the enumitem package:kreil wrote:I'd be most grateful for a pointer.
This works well for me (though I think there are more compact solutions):
Code: Select all
\begin{enumerate}
\item First item
\begin{enumerate}[label={\theenumi.\arabic*.}]
\item Second item
\begin{enumerate}[label={\theenumii\arabic*.}]
\item\label{item:last} Last item
\end{enumerate}
\end{enumerate}
\end{enumerate}
Here is a reference: \ref{item:last}
Code: Select all
\usepackage{enumitem}
Full context variable depth references to list labels?
Consulting the documentation, it gives the legal case as an example. Put in your preamble:
Code: Select all
\usepackage{enumitem}
\newlist{legal}{enumerate}{10}
\setlist[legal]{label*=\arabic*.}
Code: Select all
\begin{legal}
\item First item
\begin{legal}
\item Second item
\begin{legal}
\item\label{item:last} Last item
\end{legal}
\end{legal}
\end{legal}
Here is a reference: \ref{item:last}
Full context variable depth references to list labels?
Many thanks for your comments. The problem is that unless I manually construct a combined label, I cannot easily combine section and enumerate labels.
My best bet so far has been the following:
Code: Select all
\usepackage{enumitem}
\makeatletter
\def\sc{\protected@edef\savedlabel{\@currentlabel\relax\relax}}
\def\cc{\protected@edef\@currentlabel{\savedlabel-\@currentlabel}}
\makeatother
I can then recall this and add it to a particular item label by "copy counter" (\cc).
For example:
Code: Select all
\section{foo}
\subsection{bar}\sc
\begin{enumerate}
\item aaa
\item\cc\label{mine} bbb
\end{enumerate}
\section{foo too}
It is \ref{mine}. % will give 1.1-2 to refer to item 2 in subsection 1.1

Attempts to automate further, e.g., by doing a
Code: Select all
\def\longlabel#1{\cc\label#1}

Many thanks,
David.
Full context variable depth references to list labels?
Thanks for your patience. I see that I completely overlooked the real problem. To start on a cleaner solution, have you triedkreil wrote:Many thanks for your comments. The problem is that unless I manually construct a combined label, I cannot easily combine section and enumerate labels.
Code: Select all
\begin{legal}[ref=\arabic{section}.\arabic*.]
\item\label{item:first} First item
\begin{legal}[ref=\thelegali\arabic*.]
\item \label{item:second} Second item
\begin{legal}[ref=\thelegalii\arabic*.]
\item\label{item:last} Last item
\end{legal}
\end{legal}
\end{legal}
[ Of course, you could change the initial \arabic{section} so that it includes subsections, etc. ]
Full context variable depth references to list labels?
---
Here's a better idea...Ted wrote:Thanks for your patience. I see that I completely overlooked the real problem. To start on a cleaner solution, have you tried...
First, in the preamble:
Code: Select all
\usepackage{enumitem}
\newlist{legal}{enumerate}{10}
\makeatletter
\setlist[legal]{label*=\arabic*.,ref=\csname the\enit@prevlabel\endcsname\arabic*.}
\makeatother
Code: Select all
\begin{legal}[ref=\thesubsection.\arabic*.]
\item\label{item:first} First item
\begin{legal}
\item \label{item:second} Second item
\begin{legal}
\item\label{item:last} Last item
\end{legal}
\end{legal}
\end{legal}
You can simplify that first legal environment a little... In your preamble, put
Code: Select all
\newenvironment{firstlegal}{\begin{legal}[ref=\thesubsection.\arabic*.]}{\end{legal}}
Code: Select all
\begin{firstlegal}
\item\label{item:first} First item
\begin{legal}
\item \label{item:second} Second item
\begin{legal}
\item\label{item:last} Last item
\end{legal}
\end{legal}
\end{firstlegal}
Full context variable depth references to list labels?
Yes, that looks nice! I tried your approach ...
Are the inner loops supposed to inherit the ref from the outer loops? That doesn't seem to work for me. A label/ref to the outer loop works fine but the inner loops just have the "normal" legal style refs.Ted wrote: First, in the preamble:Then you can doCode: Select all
\usepackage{enumitem} \newlist{legal}{enumerate}{10} \makeatletter \setlist[legal]{label*=\arabic*.,ref=\csname the\enit@prevlabel\endcsname\arabic*.} \makeatother
So the only special thing you need to do within the document is add that line to the first legal environment. All nested ones are handled without any special arguments.Code: Select all
\begin{legal}[ref=\thesubsection.\arabic*.] \item\label{item:first} First item \begin{legal} \item \label{item:second} Second item \begin{legal} \item\label{item:last} Last item \end{legal} \end{legal} \end{legal}
What works quite nicely is substituting the first line with
Code: Select all
\begin{legal}[ref=\savedlabel-\arabic*.]
So if we could get the inner loops to inherit that would be great!

Best wishes,
David.
Full context variable depth references to list labels?
kreil wrote:Yes, that looks nice! I tried your approach ...
Yes, they are. It's in the \setlist. The \csname should cause the previous label to built into the refs.kreil wrote:Are the inner loops supposed to inherit the ref from the outer loops?
It works for me. Here's a (working) complete example:kreil wrote:That doesn't seem to work for me. A label/ref to the outer loop works fine but the inner loops just have the "normal" legal style refs.
Code: Select all
\documentclass{article}
\usepackage{enumitem}
\newlist{legal}{enumerate}{10}
\makeatletter
\setlist[legal]{label*=\arabic*.,ref=\csname the\enit@prevlabel\endcsname\arabic*.}
\makeatother
\newenvironment{firstlegal}{\begin{legal}[ref=\thesubsection.\arabic*.]}{\end{legal}}
\begin{document}
\section{First section}
First: \ref{item:first}\\
Second: \ref{item:second}\\
Third: \ref{item:last}
\section{Second section}
\subsection{A subsection}
\subsection{Another subsection}
\begin{firstlegal}
\item An item
\item Another item
\item\label{item:first} First labeled item
\begin{legal}
\item \label{item:second} Second item
\begin{legal}
\item\label{item:last} Last item
\end{legal}
\end{legal}
\end{firstlegal}
\end{document}
which is what I THINK you want. Right?First: 2.2.3.
Second: 2.2.3.1.
Third: 2.2.3.1.1.
Full context variable depth references to list labels?

Many thanks, I don't know what I got wrong when I first tried. The below does exactly what I want (even semi-automatically picks up the section level).
Thanks a lot again for your help!
All the best,
David.
Code: Select all
\documentclass{article}
\usepackage{enumitem}
\newlist{legal}{enumerate}{10}
\makeatletter
\def\sc{\protected@edef\savedlabel{\@currentlabel\relax\relax}}
\setlist[legal]{label*=\arabic*.,ref=\csname the\enit@prevlabel\endcsname\arabic*.}
\makeatother
\newenvironment{firstlegal}{\begin{legal}[ref=\savedlabel-\arabic*.]}{\end{legal}}
\begin{document}
\section{First section}
First: \ref{item:first}\\
Second: \ref{item:second}\\
Third: \ref{item:last}
\section{Second section}
\subsection{A subsection}
\subsection{Another subsection}\sc
\begin{firstlegal}
\item An item
\item Another item
\item\label{item:first} First labeled item
\begin{legal}
\item \label{item:second} Second item
\begin{legal}
\item\label{item:last} Last item
\end{legal}
\end{legal}
\end{firstlegal}
\end{document}