4

I'm trying to enhance the builtin \label and \ref commands to let me add some additional text to the number typically associated with a reference. That is, I want to say

\labelprop{prop:example}{Theorem}

and then have \kref{prop:example} produce "Theorem 3.1" where the 3.1 is the usual reference number.

The reason I have to do this myself is that the usual packages for doing so like prettyref are conflicting with the packages my publisher is using.

So I tried the code below---it is supposed to write some definitions that hold the desired text into the aux file so they will be defined the next time.

\makeatletter
\def\labelprop#1#2{%
   \label{#2}%
   \protected@write\@auxout{}{\string\@namedef{my-ref-#2}{#1}}
}
\def\kref#1{%
   \expandafter\ifx\csname my-ref-#1\endcsname\relax%
       \PackageWarning{my}{Command my-ref-#1\space undefined}%
   \else\fi   %
   \@nameuse{my-ref-#1}%
   \ref{#1}%
}
\makeatother

I've confirmed that definitions are getting written into the aux file; e.g.

\newlabel{prop:intro}{{1.1}{2}{Results}{theorem.1.1}{}}
\@namedef{my-ref-prop:intro}{Theorem}

appears there. However, the kref invocation is complaining that my-ref-prop:intro is undefined and producing a blank (ie, just the standard reference number).

What am I doing wrong? Debugging advice is also welcome.

Below is a MWE exhibiting the behavior I describe. I added some debugging output.

\documentclass[final,leqno,onefignum,onetabnum,oneeqnum]{siamltex1213}
\usepackage{amsmath}


\makeatletter
\def\labelprop#1#2{%
   \label{#2}%
   \protected@write\@auxout{}{\string\@namedef{my-ref-#2}{#1}}
   \@namedef{my-ref-#2}{#1}
}
\def\foobartest{foo}
\def\kref#1{%
   \typeout{ref \@nameuse{my-ref-#1}}%
   \expandafter\ifx\csname my-ref-#1\endcsname\relax%
       \PackageWarning{my}{Command my-ref-#1\space undefined}%
   \else\fi   %
   \@nameuse{foobartest}%
   \@nameuse{my-ref-#1}%
   \ref{#1}%
}
\makeatother

\begin{document}


\begin{theorem}
%\label{prop:intro}
\labelprop{Theorem}{prop:intro}
This is a theorem.
\end{theorem}

This is a reference to \kref{prop:intro}.
\end{document}
  • Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – cfr Nov 14 '14 at 1:40
  • 1
    If all of the different packages available to do this conflict with those your publisher uses, it may help to know which packages, exactly, generate the conflict since any code you or we write could easily conflict as well, couldn't it? – cfr Nov 14 '14 at 1:44
  • The conflict is with hyperref, because my original macros were redefining \label and hyperref wanted to do that too. My new macros avoid redefining \label or \ref in order to avoid conflicting with anything. I might be able to find some package that doesn't conflict, but I worry that may be more work than just writing my own macro. I'll work on a MWE. – David Karger Nov 14 '14 at 3:26
  • OK, I've editing the question to include an MWE – David Karger Nov 14 '14 at 3:33
5

The .aux file is read in a group, thus you need global definitions, e.g.:

\newcommand*{\labelprop}[2]{%
  \@bsphack
  \label{#2}%
  \protected@write\@auxout{}{\string\@labelpropdef{my-ref-#2}{#1}}%
  \@namedef{my-ref-#2}{#1}%
  \@esphack
}
\newcommand*{\labelpropdef}{%
  \global\@namedef
}

Remarks:

  • I have added comment signs at the line ends to avoid unwanted spaces.
  • \@bsphack and \@esphack should surround the definition to keep the original spacing behavior of \label (a spaces are suppressed after the command, if a space was detected before).

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.