6

I am trying to do something simple: I want to be able to store and retrieve texts and equations via labels. In particular, I want something like

\labelput{<label>}{<text>}  % sets the label. Associates text to label. 
\labelget{<label>}    % replaces by the text associated to label. 

In the following MWE, the definitions work just fine in text mode. But it doesn't work in mathmode.

\documentclass{article}

\makeatletter
\newcommand\labelput[2]{%
  \label{#1}%
  \@namedef{label@store@content@#1}{#2}%
  #2}
\newcommand\labelenvput[3]{%
  \@namedef{label@store@content@#2}{#3}%
  \begin{#1}%
  \label{#2}%
  #3%
  \end{#1}}
\newcommand\labelget[1]{\@nameuse{label@store@content@#1}}
\makeatother

\begin{document}

Test this first in \labelput{test1}{text mode}.

Does \labelget{test1} work?  % yes, it does

Test this again in math mode.

\begin{equation}\labelput{eq1}{e = mc^2}
\end{equation}

Now let us try to retrieve equation \ref{eq1} with
$\labelget{eq1}$   % the \ref works, but the \labelget fails silently


But the following works 
\labelenvput{equation}{eq2}{e = mc^3}

Retrieve
\[ \labelget{eq2} \]
\end{document}

So my questions

  1. How to correctly use \@namedef in mathmode? Is there another command I should use instead?
  2. Am I trying to reinvent the wheel?
  • The command \labelretrieve isn't defined. The command \label requires an anchor. In your case of \labelput there is no anchor. – Marco Daniel Nov 24 '13 at 12:06
  • @MarcoDaniel: \labelretrieve is meant to be \labelget, made a mistake with "copying and pasting" from two different files. That the command \label requires an anchor has nothing to do with it, I think. Else it wouldn't even work in text mode. – Willie Wong Nov 24 '13 at 12:17
  • Please try Does \labelget{test1} work or \ref{test1}? % yes, it does and you will see the issue. – Marco Daniel Nov 24 '13 at 12:19
  • @MarcoDaniel I know that \ref{test1} will give me nothing in my MWE. But that's not the problem. – Willie Wong Nov 24 '13 at 12:21
  • The main issue is solved by @egreg (as usual) :-) -- I thought it's just a point, you should know about. – Marco Daniel Nov 24 '13 at 12:22
5

Your \@namedef is issued inside a group, so the control sequence disappears at the end of the group: every environment forms a group. Prefix \@namedef with \global.

\documentclass{article}

\makeatletter
\newcommand\labelput[2]{%
  \label{#1}%
  \global\@namedef{label@store@content@#1}{#2}%
  #2}
\newcommand\labelenvput[3]{%
  \global\@namedef{label@store@content@#2}{#3}%
  \begin{#1}%
  \label{#2}%
  #3%
  \end{#1}}
\newcommand\labelget[1]{\@nameuse{label@store@content@#1}}
\makeatother

\begin{document}

Test this first in \labelput{test1}{text mode}.

Does \labelget{test1} work?  % yes, it does

Test this again in math mode.

\begin{equation}\labelput{eq1}{e = mc^2}
\end{equation}

Now let us try to retrieve equation \ref{eq1} with
$\labelget{eq1}$   % the \ref works, but the \labelget fails silently


But the following works 
\labelenvput{equation}{eq2}{e = mc^3}

Retrieve
\[ \labelget{eq2} \]
\end{document}

enter image description here

  • Oh crikey.... thanks. That's really obvious in hindsight. – Willie Wong Nov 24 '13 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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