31

Sometimes when I work I write something like

\begin{proposition} \label{my new result}
...
\end{proposition}
...
blah blah in Proposition \ref{my new result} blah...

but then I realize that the Proposition should actually become a Lemma. So I change it to

\begin{lemma} \label{my new result}
...
\end{lemma}

The problem is that I then have to hunt down all the places where I wrote Proposition and change it to Lemma, which is tedious and error-prone. Well, we have search facilities in editors, so it is not really that complicated, but still it feels the wrong way to do this. I would be happier to write

\begin{proposition} \label{my new result}
...
\end{proposition}
...
blah blah in \labelNameFor{my new result} \ref{my new result} blah...

where labelNameFor is a hypothetical magic command which outputs "Proposition". Then I could forget about all the places where I reference the result and just make any changes I wish.

Is there a way to do something like this? As far as I understand the label/ref mechanism is simply based on counters, so I do not see a straightforward way, but maybe someone more clever and industrious then I am has given the problem some thought.

5
  • Good question. I had a hack for this in my thesis, but I'll wait a bit to see if anyone has a proper solution before offering that up. Commented Nov 22, 2010 at 13:18
  • Maybe your question is answered here. (Possible duplicate?) Commented Nov 22, 2010 at 13:26
  • It seems to me that the question there is different, but there are many answers for my question! :-)
    – Andrea
    Commented Nov 22, 2010 at 13:38
  • Your right, just looking at the questions it's definitely not a duplicate. I only looked at the answers, which I found very useful. Commented Nov 22, 2010 at 13:40
  • It is also easy to do it yourself as discussed in this post. Commented Nov 23, 2010 at 15:17

3 Answers 3

24

I recommend to use the cleveref package which enhances LaTeX's cross-referencing features determining types of cross-references and context.

In your case, \cref{my new result} would output proposition 1. If you then change this particular environment into a lemma environment, the same \cref{my new result} would output lemma 1.

You could even change the referencing name for an environment in the whole document if you would like to change the style completely. For example:

\documentclass{article}
\usepackage{cleveref}
\newtheorem{proposition}{Proposition}
\begin{document}
\begin{proposition}\label{my new result}
...
\end{proposition}
...
blah blah in \Cref{my new result} blah...
\end{document}

outputs ... blah blah in Proposition 1 blah...

If you add this modification to the preamble

\Crefname{proposition}{Lemma}{Lemmas}
\crefname{proposition}{lemma}{lemmas}

the output changes to ... blah blah in Lemma 1 blah...

Other packages with similar features are ntheorem (with thref option), hyperref (with \autoref) and fancyref. Your question might be solved by any of them, though cleveref seems to be more flexible and generally provides more features - have a look at it if you're interested in further features for your theorem environments or for cross-referencing in general.

5
  • If I understand correctly, that would change all Propositions in Lemmas, right? I need a tool which is able to figure out that I changed a particular Proposition to a Lemma.
    – Andrea
    Commented Nov 22, 2010 at 13:37
  • 2
    \cref does it as well - if you change to \begin{lemma}\label{my new result} ... \end{lemma} then \cref{my new result} outputs lemma 1.
    – Stefan Kottwitz
    Commented Nov 22, 2010 at 13:46
  • @Andrea: as I read hunt down all the places where I wrote Proposition and change it to Lemma I firstly assumed you wish to switch from propositions to lemma completely, so I came up with \crefname.
    – Stefan Kottwitz
    Commented Nov 22, 2010 at 13:49
  • Should the "c" in \cref and \crefname be capitalized or not?
    – tvk
    Commented Apr 27, 2016 at 18:40
  • 1
    @FangJing There are two versions, capitalized and not, see the manual (texdoc cleveref).
    – Stefan Kottwitz
    Commented Apr 27, 2016 at 19:29
11

The ntheorem package handles this nicely with its \thref command.

Another option would be to make use of hyperref and its \autoref command.

Both of these would obviate the need to hunt down all instances of the reference.

So instead of writing Proposition \ref{thm:label} you'd just write \autoref{thm:label}. Then, if you later changed the theorem to a lemma, \autoref would automatically update (after two compiles...)

6

This question is very similar to a previous one, as noted. Here is my (accepted) answer to that question. Summary: use fncylab.

To address your comment that referencing is based on counters: this is true, but that does not preclude a more detailed scheme like you want! In fact, every counter is a bundle of several related macros (in C it would be a struct) one of which is called p@<counter name> and contains material to be printed before the counter number when used in a reference. The fncylab package actually modifies this (in a manner suggested in the LaTeX sources themselves) so that p@<counter name> can be a macro taking the number as an argument.

2
  • 1
    As the comments to the question show: It hasn't been asked there, but your answer there is great. Commented Nov 22, 2010 at 13:55
  • Corrected already!
    – Ryan Reich
    Commented Nov 22, 2010 at 13:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .