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.

Is there a way to access the key of the most-recently-declared label?

for instance:

    \begin{equation}\label{equ:1}
    ...
    \end{equation}

    bla bla bla \ref{\getlastlabelkey} bla bla bla ...

instead of:

    bla bla bla \ref{equ:1} bla bla bla ...
share|improve this question
2  
+1 for an interesting question- however it does sound an incredibly dangerous thing to use. What happens if you insert a labelled equation in between what is now your equ:1 and the reference? –  cmhughes May 23 '12 at 16:57
    
In that case that would obviously be a problem :)! However this was just a simple question to try and achieve a solution to this problem tex.stackexchange.com/questions/57093/… –  Pedro May 23 '12 at 18:32
1  
Note that the current potential label is located in \@currentlabel. This macro is set by all labelable macros like \section, \caption, equation etc. and written by \label under the given label name in the .aux file. If you need to do some internal stuff using \@currentlabel directly might be easier because it doesn't need an explicit \label added by the user. –  Martin Scharrer May 24 '12 at 9:11

1 Answer 1

up vote 6 down vote accepted

A simple solution would be to redefine \label to save its argument in a global macro:

\let\origlabel\label% Save old definition
\def\label#1{\origlabel{#1}\gdef\lastlabel{#1}}

Then use:

\ref{\lastlabel} or \ref\lastlabel
share|improve this answer

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.