26 February 2009

LaTeX equation labels

When writing a paper in LaTeX, you often want to put a numerical label on a displayed equation, say the number (1). So you write some code like
$$\label{eq:basel-problem} \sum_{n=1}^\infty {1 \over n^2} = {\pi^2 \over 6}$$
which compiles to give something that looks like

Then later I can insert code like (\ref{eq:basel-problem}) and (1) appears in my docuemnt.

Now, as you may have noticed, I picked an equation that had a nice name, and I labeled it with that name. (The "eq:" in the label, of course, stands for "equation", a convention that I use to tell what sort of entity I'm referencing -- other things I use in that position are def:, thm:, prop:, cor:, lem:, and the like.)

But what do you do when the displayed equation doesn't have a nice "name" -- it's just an equation that occurs somewhere in the course of a calculation? For a while I tried to come up with a name, but I ended up with way too many generic names like "integral" and "sum" and "thing-with-binomial-coefficients". (Okay, so I'm exaggerating on the last one.) These names took time to think of but didn't make things easier on me later. So now I find myself using labels like \label{eq:feb-24-kappa} for the 10th labelled equation that I inserted on February 24. (Why do I use Greek letters? I tried using numbers, but it's too easy to get those confused with the actual numbers that are used to label equations.) But I'm wondering what sort of conventions people use for this; since it's the sort of thing that you can only see when you're looking at other people's LaTeX source, it's hard to know.

Somewhere, somebody is saying that I'm using LaTeX incorrectly. It might be you!

(Yes, I'm taking a break from rewriting a paper. How did you guess?)

Anonymous said...

I look at my notes for that section, and if an equation is (2) in my notes, I use \label{section_name_two}.

It works for me because my writing style kind of eschews numbered equations. I typically use at most 3 or 4 per ten pages, I can afford to be unsystematic.

Anonymous said...

Why do you need to refer to so many equations? It seems strange to me. I usually put the small notes about equations just near them so I don't need to refer to them except for those which do have nice names. It can also be confusing for the reader to refer to too many equations.
A question: Doesn't LaTeX allow local names? I think it will assume a name refers to the closest one when they are more than one references, so you should be able to reuse simple names like eq:t1 (t for temporary)

Aaron said...

I usually label things only when I refer to them in the text. My labels still end up sounding kind of generic, but at least there are only a few of them!

Unknown said...

This is the sort of problem that you'll be running into if you are writing a large piece, e.g. a book heavily relying on mathematics, proofs etc. I usually break the writing into smaller manageable parts (from a complexity POV, not related to equations), and precede all the labels that I reference in that particular part with a prefix (e.g. \label(fig:intro_foo)). Alternatively, you can try an editor that shows all your labels (including equations) that you have defined in the current document --- this makes it even easier to find what you are looking for when you need a reference. But I found out from experience that longer and meaningful names are always better than generics --- just like in variables in programming.

Suresh Venkatasubramanian said...

I let emacs solve the problem for me (or kile, if that floats your boat).

In emacs if you use the AUCTeX package, labels for everything are generated automatically by some algorithm. Aha you say: now how am I to remember said obscure labels ?

That's where RefTeX comes in. It's another emacs package that remembers labels and keeps track of things for you, so you never actually need to remember any label again. If you want to reference an equation you labelled earlier, you can either do TAB completion with an eq: prefix, or you can even view a table of contents in a top-split screen that has all labels, and merely arrow down to the one you want and hit enter.

It seems clunky when described this way, but it's actually extremely fast: and I never need to remember labels for anything.

Anonymous said...

A former colleague used to use not-appropriate-for-workplace language to label things. Finding a creative label is a good exercise. Sometimes you might want to name the equations after your pets. Or you might want to use feline animals.

I know in Mathematica, I use, lion, leopard, tiger etc for lines and I use bird types for planes. Also, there in I use homophones. It is the Dr. Suess approach to programming...

Back to the point. The equation that you are labeling must be doing something for you. What does it do? You can label with verbs, too. So the equation might be an identity. Call it id. The next one might also be an identity call it ego. Or call it self, or mice-elf, or just elf. Then use elf, dwarf, orc, fairy, gnome, for the sequence of identities. You can search back for gnome to figure out which identity you meant by gnome.

Find some quasi-memorable classification of the types of eqns and carry on from there.

Michael Lugo said...

Scott,

those are good ideas. The problem with using Greek letters (as I have been) is that they come in order, so subconsciously I feel wrong when they're out of order. And my labels often are out of order, as I often go back and forth on whether an equation needs a label as I change text elsewhere that may refer to it. But using some memorable collection of things that doesn't have a natural order seems like a good idea. I think I'll try it next time I sit down to write something long.

Anonymous said...

I typically use random names of theorems that I happen to think of at the moment, or other random words. I also use ntheorem, so the use of "lem:" and "thm:" is redundant; LaTeX puts in the appropriate labels.

meep said...

I'd name it Murray.

Unknown said...

Anonymouse wrote: A question: Doesn't LaTeX allow local names? I think it will assume a name refers to the closest one when they are more than one references, so you should be able to reuse simple names

No, LaTeX doesn't have local names; labels don't function like variables in a typical programming language.

The underlying label mechanism in LaTeX is quite primitive: on the first pass through the file, the actual numbers for each label are recorded in the .aux file, and in subsequent passes they're read in. As a result, if you multiply-define a label, all references to it will be the same (either to the first or last definition, no sure which).

Anonymous said...

I occasionally run into this problem too. Your "eq:thing-with-binomial-coefficients" example is not too far removed from actual labels I have given the odd equation.

I tend to have this problem rarely, so I just give a label sufficiently long to describe it properly. Same goes for list items.

Some quickly selected examples from a project I have open:

eqn:charInnerProduct-remark
eqn:SchurConvolve-2
item:vectorOrbitYieldsIrredRepn
eqn:inducedRepresentationIsomorphicIrred

Obviously, if you do this, it is useful to have an editor which is smart enough to identify and remind you of what labels you have defined.

Anonymous said...

When you write a book, lecture notes or your thesis it's better to define shorter commands:

\newcommand{\beq}{}

\newcommand{\eeq}{}

\newcommand{\barr}{\begin{array}}

\newcommand{\earr}{\end{array}}
\newcommand{\bce}{\begin{center}}

\newcommand{\ece}{\end{center}}
and so on...

Jonathan Katz said...

Alternatively, you can try an editor that shows all your labels (including equations)...

If you aren't using WinEdt, you should be.

Anonymous said...

I have exactly the same problem sometimes... I partly solved it first setting labels _only_ to equations that I need to refer to, and then I finally switched from the ugly WinEdt to the wonderful LEd (www.latexeditor.org) that keeps track of every label in the documents and prompts a list where I can easily select all the equations.

Anonymous said...

I often use the showlabels package that displays the names of my labels at the margin of the document. So I can easily look up the labels in the preview.