GeneralFull context variable depth references to list labels?

LaTeX specific issues not fitting into one of the other forums of this category.
Ted
Posts: 94
Joined: Sat Jun 23, 2007 4:11 pm

Full context variable depth references to list labels?

Post by Ted »

kreil wrote: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).
David, we can do one better. Try this instead

Code: Select all

\usepackage{enumitem}
\newlist{legal}{enumerate}{10}
\makeatletter
\setlist[legal]{label*=\arabic*.,ref=\csname the\enit@prevlabel\endcsname\arabic*.}
\newcommand{\thelegal}{\thesubsection-}
\makeatother
Then get rid of the firstlegal environment.

Then you won't ever have to remember what level you're at. Just use the legal environment all the way through.

Code: Select all

\begin{legal}
        \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{legal}
-- Ted [home/blog]

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

kreil
Posts: 11
Joined: Mon Jul 28, 2008 3:38 am

Full context variable depth references to list labels?

Post by kreil »

Hey, this is getting quite neat! :-)

Yes, your new solution is even better! I still need the "\sc" though because otherwise I'd get labels like "3.0" if I move the "legal" list a level up or such.

So the below is basically your latest version with the \sc trick -- or do you know a more direct way of getting to the latest section level? A bit like your "\enit@prevlabel" but for sections? (Apologies if this sounds a bit naive.)

Best wishes,
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*.}
\newcommand{\thelegal}{\savedlabel-}
\makeatother

\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

\section{Third section}\sc
\begin{legal}
        \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{legal}

\end{document}
Ted
Posts: 94
Joined: Sat Jun 23, 2007 4:11 pm

Full context variable depth references to list labels?

Post by Ted »

kreil wrote:Yes, your new solution is even better! I still need the "\sc" though because otherwise I'd get labels like "3.0" if I move the "legal" list a level up or such.
I think you're being too hard on yourself. :) Try:

Code: Select all

\usepackage{enumitem}
\newlist{legal}{enumerate}{10}
\makeatletter
\setlist[legal]{label*=\arabic*.,ref=\csname the\enit@prevlabel\endcsname\arabic*.}
\newcommand{\thelegal}{\@currentlabel-}
\makeatother
Works for me. Of course, you don't get the results you like if you're legal environment is inside another labeled environment (like another enumerate). I assume that won't be a problem for YOU though, right?

If you really really REALLY want to save labels, it's easy force your save command after every section and subsection.

Code: Select all

\usepackage{enumitem}
\newlist{legal}{enumerate}{10}
\makeatletter
\newcommand\savesectionlabel{\protected@edef\savedlabel{\@currentlabel\relax\relax}}
\setlist[legal]{label*=\arabic*.,ref=\csname the\enit@prevlabel\endcsname\arabic*.}
\newcommand{\thelegal}{\savedlabel-}
\let\origSection\section
\let\origSubsection\subsection
\newcommand\newSectionstar{\origSection*}
\newcommand\newSubsectionstar{\origSubsection*}
\newcommand\newSection[1]{\origSection{#1}\savesectionlabel}
\newcommand\newSubsection[1]{\origSubsection{#1}\savesectionlabel}
\renewcommand\section{\@ifstar\newSectionstar\newSection}
\renewcommand\subsection{\@ifstar\newSubsectionstar\newSubsection}
\makeatother
Until I think of something better, that is probably the most "robust" solution. Note that it prevents you from doing \section[stuff]{stuff}. If that's important to you, it's easy to add. It just makes it longer.

UPDATE: I've posted a much shorter and much more robust solution below. Use that instead.
Last edited by Ted on Mon Jul 28, 2008 8:25 pm, edited 3 times in total.
-- Ted [home/blog]
kreil
Posts: 11
Joined: Mon Jul 28, 2008 3:38 am

Re: Full context variable depth references to list labels?

Post by kreil »

Cool! Such an elegant solution.

I didn't realize that other label setting environments (like equation) would not disturb \@currentlabel :-)

Many thanks again for your kind help,

David.
Ted
Posts: 94
Joined: Sat Jun 23, 2007 4:11 pm

Full context variable depth references to list labels?

Post by Ted »

kreil wrote:I didn't realize that other label setting environments (like equation) would not disturb \@currentlabel :-)
It only causes a problem if you are INSIDE one of those environments. I'm guessing that you won't be using enumerate anymore, and so you'll probably be safe.

That being said, see my most recent edit to that message. I show you how you can redefine \section and \subsection so that they automatically call \savesectionlabel (I renamed it so that it didn't override the old TeX command for small-caps). That's more robust, but the edit (as it stands) prevents you from doing \section[stuff]{stuff}. It would be easy to make it support using the optional section argument, but the implementation gets longer.
-- Ted [home/blog]
Ted
Posts: 94
Joined: Sat Jun 23, 2007 4:11 pm

Full context variable depth references to list labels?

Post by Ted »

kreil wrote:Cool! Such an elegant solution.
Here's a better one:

Code: Select all

\usepackage{enumitem}
\newlist{legal}{enumerate}{10}
\makeatletter
\setlist[legal]{label*=\arabic*.,ref=\csname the\enit@prevlabel\endcsname\arabic*.}
\newcommand{\thelegal}{\ifnum\value{subsection}=0\thesection\else\thesubsection\fi-}
\makeatother
I think that ought to do everything you wanted in every case, right?

[ Of course, you can change that trailing hyphen (-) to whatever you want. It's the separator between the section and the enumeration labels. ]
Last edited by Ted on Mon Jul 28, 2008 8:27 pm, edited 1 time in total.
-- Ted [home/blog]
kreil
Posts: 11
Joined: Mon Jul 28, 2008 3:38 am

Full context variable depth references to list labels?

Post by kreil »

Nice :-)

So how do I similarly redefine my \label command so that all labels (say, also equations) get their full section-context on citation?

As I said, the direct

Code: Select all

\def\mylabel#1{\cc\label#1}
did not seem to work.

Best wishes,
David.
kreil
Posts: 11
Joined: Mon Jul 28, 2008 3:38 am

Full context variable depth references to list labels?

Post by kreil »

Dear Ted,
Ted wrote:Here's a better one:

Code: Select all

\usepackage{enumitem}
\newlist{legal}{enumerate}{10}
\makeatletter
\setlist[legal]{label*=\arabic*.,ref=\csname the\enit@prevlabel\endcsname\arabic*.}
\newcommand{\thelegal}{\ifnum\value{subsection}=0\thesection\else\thesubsection\fi-}
\makeatother
I think that ought to do everything you wanted in every case, right?
Why is that better than using \@currentlabel? Or perhaps we crossed wires.

Cheers,
David.
Ted
Posts: 94
Joined: Sat Jun 23, 2007 4:11 pm

Full context variable depth references to list labels?

Post by Ted »

kreil wrote:So how do I similarly redefine my \label command so that all labels (say, also equations) get their full section-context on citation?
Are you unhappy with (in preamble

Code: Select all

\usepackage{amsmath}
\numberwithin{equation}{subsection}
? The downside is that you can get equation labels with "0" in the middle of them. So perhaps you could use

Code: Select all

\usepackage{enumitem}
\newlist{legal}{enumerate}{10}
\makeatletter
\setlist[legal]{label*=\arabic*.,ref=\csname the\enit@prevlabel\endcsname\arabic*.}
\newcommand\thesectionlabel{\ifnum\value{subsection}=0 \thesection\else\thesubsection\fi}
\newcommand{\thelegal}{\thesectionlabel-}
\makeatother

\usepackage{varioref}
\labelformat{equation}{\thesectionlabel-\textup{(#1)}}
\labelformat{figure}{\thesectionlabel-#1}
\labelformat{table}{\thesectionlabel-#1}
Does that work well for you?
Last edited by Ted on Mon Jul 28, 2008 8:48 pm, edited 1 time in total.
-- Ted [home/blog]
Ted
Posts: 94
Joined: Sat Jun 23, 2007 4:11 pm

Full context variable depth references to list labels?

Post by Ted »

kreil wrote:
Ted wrote:Here's a better one:

Code: Select all

\usepackage{enumitem}
\newlist{legal}{enumerate}{10}
\makeatletter
\setlist[legal]{label*=\arabic*.,ref=\csname the\enit@prevlabel\endcsname\arabic*.}
\newcommand{\thelegal}{\ifnum\value{subsection}=0\thesection\else\thesubsection\fi-}
\makeatother
I think that ought to do everything you wanted in every case, right?
Why is that better than using \@currentlabel?
It prevents problems doing things like

Code: Select all

\subsection{A subsection}
\begin{enumerate}
   \item An enumerate item
      \begin{legal}
         \item A legal item
      \end{legal}
\end{enumerate}
The enumerate item will reset the \@currentlabel. It gets set back after the enumerate closes, but it's still set when the legal gets entered. The newer solution I posted prevents this problem (and any other possible related problem) by looking directly at the section/subsection labels, which is really what you want.
-- Ted [home/blog]
Post Reply