# TexStudio script for autolabeling formulas

I was thinking of a script for auto labeling the formulas in TexStudio. It should be able to

1. Find a "{key}"
2. Determine if it is in a math environment
3. Choose a unique number for each equation
4. Replace that key with the chosen unique number

Any suggestions?

• Er ... why would you want to do that? – cfr Aug 13 '16 at 0:59
• I have lots of entered equations without labels. I want just to label them automatically. – Hamid Aug 13 '16 at 1:02
• But you said they already had keys. What is that if not a key in a label? – cfr Aug 13 '16 at 1:44
• When I pressed Crtl+Shift+N a \label{key} was generated but I didn't alter them because I was not sure not to put some other equations above it. now I have the equations but the placeholder "key" was not altered. – Hamid Aug 13 '16 at 1:47

The whole point of the key mechanism LaTeX provides is not to use a number but to use some descriptor you'll remember.

For example,

• \label{eq:fermatslastthm}

or

• \label{eq:lifeuniverseeverything}.

The point is, the label stays the same regardless of how things get inserted or moved around.

Then you can say \ref{eq:lifeuniverseeverything} and LaTeX will figure out what number to give it, depending on its relative position in the document right now. (Well, its relative position on the previous run, actually.)

It doesn't matter two hoots what the label is. However, using numbers is confusing for human beings because we find it difficult if the fifth equation is labelled 4 rather than 5.

That is, LaTeX will happily give the equation with label \label{eq:43} the number 2 if it is now second or the number 9,456 if it is now nine thousand four hundred and fifty-sixth, but it is much more confusing to need to write \ref{eq:43} for the nine thousand four hundred and fifty-sixth equation rather than \ref{eq:thomasconj} if the equation is, say, a famous conjecture by Thomas.

Just remember that you need to compile at least twice to ensure that all of the cross-references are correct. LaTeX warns you on the console if another compilation may be needed.

• This fact is known. However, I want to add the keys orderly with numbers. This will facilitate the referencing for me. – Hamid Aug 13 '16 at 2:12
• And if you need to move an equation or delete or add one? Or if you decide one equation should be unnumbered rather than numbered or vice-versa? – cfr Aug 13 '16 at 2:43
• no problem the number is omitted but the equation is near that number. is it my question that is hard to understand? – Hamid Aug 13 '16 at 2:47
• You obviously don't edit much. I would say there was no guarantee that it would be anywhere near that number. Indeed, that number might not even now occur in a label anywhere or it might be in a different part of the document. It doesn't make sense to me. If you know the number, why do you need \ref anyway? You might as well hard-code your numbers in that case. (I'm not suggesting this, obviously.) As I understand it, the whole point is not to number them. If you are going to use numbers, you don't need anything this complicated! – cfr Aug 13 '16 at 2:53
• In fact, I have lots of formulas. In fact, for a document, the number is like 100 numbered equations. Consdiring several document I can not name each of them by hand. instead I need to be labeled somehow automatically (better be by numbers). – Hamid Aug 16 '16 at 17:47