Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

When I define a label inside an enumerate-list I get a reference to the item number.

Is it possible to get a reference to the section?

Example:

\documentclass[english]{scrartcl} 
\usepackage{babel} 
\usepackage{blindtext} 

\newcommand\secref[1]{[section of label '#1']}%<-- I ask for this function.
\begin{document}
%Should be: The 2nd item point on page 3 is in section 2.3.1
The \ref{mylabel}nd item point on page \pageref{mylabel} is in section \secref{mylabel}

%Should be: The 2nd item point on page 3 is in section 2.3.1
The \ref{mylabel}nd item point on page \pageref{mylabel} is in section \ref{sec:mylabel}

\blinddocument%create some (sub)sections

\label{sec:mylabel}
\begin{enumerate}
\item point 1
\item \label{mylabel} here is a label
\end{enumerate}

\end{document}

Remark I: I don't want to replace \ref to show always the section reference. I want to have the choice to get the item reference or the section reference.

Remark II: The 2nd sentence in my example shows the result I want. But I want to avoid the definition of the help-label sec:mylabel.

share|improve this question
    
This sounds like an interesting problem... Thinking about it from the reader's perspective: how many such lists and references will you have? If there are a lot, then you might consider simply labelling the list using label=(\thesection.\label*) (loading enumitem) and then using a simple \pageref, or \vref (from varioref) –  cmhughes May 13 '13 at 21:23

3 Answers 3

up vote 7 down vote accepted

Emit two \label commands:

\documentclass{article}
\makeatletter
\newcommand{\seclabel}[1]{{%
  \label{#1}%
  \edef\@currentlabel{\thesection}%
  \label{sec:#1}}}
\makeatother

\begin{document}
\section{A}

\begin{enumerate}
\item X
\item\seclabel{mylabel} Y
\item Z
\end{enumerate}

This is a reference to item~\ref{mylabel} in section~\ref{sec:mylabel}
on page~\pageref{mylabel}.

\end{document}

This gives

This is a reference to item 2 in section 1 on page 1.

Of course, if hyperref is loaded, clicking on the link created by \ref{sec:mylabel} will take to the item in the enumerate environment.

share|improve this answer
    
Should there be an extra group around the replacement? If another \label is made to the same numbered item then \@currentlabel will still have \thesection in it. –  Matthew Leingang May 14 '13 at 0:13

zref is meant as an extension to the traditional 2-piece \label-\ref system provided by LaTeX which yields a (1) label and (2) page number. Instead it functions on so-called property lists that supports multiple references using the same label:

enter image description here

\documentclass{article}
\usepackage{zref}% http://ctan.org/pkg/zref
\makeatletter
\zref@newlist{allref}% Reference details stored in list 'allref'
\zref@newprop*{section}[??]{\thesection}% Create new 'section' property
\zref@addprops{allref}{section, page, default}% allref reference (or property list) has section, page and default
\newcommand{\mylabel}[1]{%
  \zref@labelbylist{#1}{allref}% Write allref list to .aux (using zref)
}
\newcommand{\myref}[2][default]{\zref@extract{#2}{#1}\zref@refused{#2}}%
\makeatother

\begin{document}

\section{A}

\begin{enumerate}
  \item X
  \item Y \mylabel{mylabel}
  \item Z
\end{enumerate}

This is a reference to item~\myref{mylabel} in section~\myref[section]{mylabel}
on page~\myref[page]{mylabel}.

\end{document}

The above MWE provides \mylabel which functions just like \label and an accompanying \myref for \ref. If used as-is, \mylabel{<label>} is equivalent to \label{<label>} as is \myref{<label>} to \ref{<label>}. However, to yield the section number associated with <label>, use \myref[section]{<label>}. This extracts the property value stored in the list. It would be possible to add any number of additional properties that could be extracted.

One caveat with the above approach is that the label is stored at page ship out (for obvious reasons related to the asynchronous ship-out routine). And, values other than the page property may change between being \mylabeled and being \myrefed. Some work has to be done to overcome this problem, possibly using a dual label.

Of course, one could also overwrite the meaning of \label and \ref itself to use this, but it would imply a global change.

share|improve this answer

My approach was to redefine the \label command, to also create a reference of the same name prefixed by sec: against the current section counter via the use of \refstepcounter{section}, making sure to undo the step beforehand via use of the \addtocounter{section}{-1} command.

\documentclass[english]{article}
\usepackage{lipsum}

%Redefine commands.
\let\oldlabel\label
\makeatletter
    \renewcommand{\label}[1]{%
         \oldlabel{#1}%
         \edef\@currentlabel{\thesection}
         \oldlabel{sec:#1}%
    }
\makeatother

\begin{document}
    \section{Section A}
        \lipsum[2]
    \section{Section B}
        \lipsum[2]
    \section{Section C}
        \begin{enumerate}
        \item Point 1
        \item Point 2\label{mylabel} here is a label
        \end{enumerate}
    \section{Section D}
        The \ref{mylabel}nd item point on page \pageref{mylabel} is in section \ref{sec:mylabel}
\end{document} 

If you then wanted to create a command secref similar to pageref, then this is easy via:

\newcommand{\secref}[1]{\ref{sec:#1}}

Output:

Output

By the same token, in the event that the book class is in use, could be extended further to include chpref for chapter references via:

%Redefine commands.
\let\oldlabel\label
\makeatletter
    \renewcommand{\label}[1]{%
        \oldlabel{#1}%
        \edef\@currentlabel{\thesection}
        \oldlabel{sec:#1}%
        \edef\@currentlabel{\thechapter}
        \oldlabel{chp:#1}%
    }
\makeatother
\newcommand{\secref}[1]{\ref{sec:#1}}
\newcommand{\chpref}[1]{\ref{chp:#1}}
share|improve this answer
    
If I use \refstepcounter{chapter} I reset the section counter. There should be a mechanism to remember the section number. Bit it is a nice idea. –  knut May 13 '13 at 22:09
    
Cheers, forgot about that @egreg had the right idea via @currentlabel, modified accordingly. My original mechanism was flawed, since section resets subsection, subsection resets subsubsection etc as you have correctly identified. –  Nicholas Hamilton May 13 '13 at 22:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.