1

Can we automatically assign labels to numbered equations corresponding to their number in the text?

Is there a latex macro that does that, regardless of the particular editor which I'm using?

Can the automatic labels also update automatically when new equations are inserted?

Thank you.

  • Update: I got great responses to my question, thanks to all, however I feel that I need to make my question more clear:

What I mean is, if Eqs. 1,2,3 and 4 in the can be assigned the labels \label{Eq1}, \label{Eq2}, \label{Eq3} automatically (as opposed to calling them e.g., \label{Derv1}, \label{Derv2} and \label{FinalResult}...), it is easier to cite them in the text (something like: "as seen in Eq. \ref{Eq2}). However, as many people have mentioned below, this is very problematic if one has to manually update all the labels and the Ref's to these labels, if he chooses to add a new equation, say, between Eq. 2 and three. This means that I am looking for a way to get the existing labels and references to be updated automatically.

So can this at all be done?

  • 6
    Yes, it's possible, but this will defeat the whole purpose of labels. If you insert an equation between, for instance, equations 2 and 3, all references to equations 3 onwards will now refer to the wrong equations. – Phelype Oleinik Jan 9 at 15:21
  • 4
    Don't pursue such ideas. They are error prone. You would have to remember the number of the equation each time you wan't to reference it. – user31729 Jan 9 at 15:35
  • 2
    This is a duplicate I will see if i can find the existing question but you absolutely do not want to do this it would defeat the entire purpose of the \label mechanism. You do not need to know the number that will be used, nor change the label if the number changes. just use \label[fermat} in the equation and then \ref{fermat} will print whatever number is needed. – David Carlisle Jan 9 at 22:30
  • 1
  • The other reason for using \label and \ref is hyperref, which is better served by \hypertarget and \hyperlink. – John Kormylo Jan 9 at 23:16
3

What exactly do you mean by "assign labels to numbered equations corresponding to their number in the text" ?

What exactly are you trying to achieve? What is the purpose of this in practice?


Let's use some vocabulary out of the area of database management and relational algebra:

Take all the referencing labels for a database for referencing whatsoever items of numbered sectioning within the document:

Each referencing label forms an entire data record belonging to that database.

(With this understanding of the term "referencing label" your question could be rephrased as:
Can we automatically assign entire data records belonging to the database of referenceable items of numbered sectioning to numbered equations corresponding to their number in the text?
That's why in the beginning of this answer I asked what exactly you mean.)

The single records are stored within the .aux-files in terms of these nice \newlabel-entries.

The name of such a referencing label, which you introduce by the command
\label{⟨name of referencing label⟩},
forms the value of the data field "primary key" of the data record formed by the referencing label in question.

LaTeX will fill in for you the values of all other data fields belonging to that data record (page number, number of item of sectioning, name of anchor for hyperlinking, ...) automatically.

One thing you should never do in whatsoever kind of database management is changing values of primary keys of data records.

You are asking for a mechanism by means of which LaTeX shall do exactly this automatically.

Usually the set of data fields available in data records for \label-\...ref-referencing is hardcoded in LaTeX:

  • The name of the referencing label. This is the primary key. You provide that name/the value of this data field via \label{⟨name of referencing label⟩}.
  • Page number where the item of sectioning in question occurs. LaTeX will provide the value of this data field automatically. This data field is used by the \pageref-command.
  • Formatted output of the value of the counter by means of which the item of sectioning in question is numbered. LaTeX will provide the value of this data field automatically. This data field is used by the \ref-command.
  • In case of also using hyperref: Title of the item of sectioning in question. LaTeX will provide the value of this data field automatically. This data field is used by the \nameref-command.
  • In case of also using hyperref: The name of the anchor for creating hyperlinks to the item of sectioning in question. LaTeX will provide the value of this data field automatically. This data field is used by all referencing-commands that also create hyperlinks. With the \autoref-command it is also used for concluding the correct contextual phrase.
  • In case of also using hyperref: In case of importing referencing labels from other documents via the xr-package/the xr-hyper-package: URL of the external document which is to be hyperlinked. You provide the value of this data field as one of the arguments of the \externaldocument-command. This data field is used by all referencing-commands that also create hyperlinks.

Heiko Oberdiek's zref package provides means for adding and accessing and having LaTeX maintain automatically more data fields.


Be aware that (La)TeX is not intended to be a tool for modifying the tex-source-code of your document.

In other words: You don't use (La)TeX for producing the tex-source-code of your document/for working on the tex-source-code of your document.

You use (La)TeX in moments where you don't work on the tex-source-code of your document. In these moments you use (La)TeX in order to compile the tex-source-code as is for having (La)TeX produce a .dvi- or .pdf-output-file.

Thus automatic updating of names of referencing labels within arguments of referencing commands (e.g., \label, \ref, \pageref, \nameref, \autoref, \hyperref, and many more in case of using additional packages like refcount or cleveref or titleref or ...) is something that probably can take place while you work on the tex-source-code of your document and hereby use the tool/tex-input-editor of your choice.
(Even here it can be a difficult task because you can define referencing commands on your own and/or hide the names of referencing labels within macros. E.g., you can do:

\let\IveGotTheBluesOfWeirdness=\pageref
\newcommand\foo{\bar}%
\newcommand\bar{Eq3}%
...
\label{\foo}%
...
\ref{\bar} on page~\IveGotTheBluesOfWeirdness{Eq3}
...

)

Automatic updating of names of referencing labels while working on the tex-source-code of your document is something that cannot take place while (La)TeX is running.

Thus automatic updating of names of referencing labels is something that cannot be achieved during a (La)TeX-run by means of (La)TeX-macros.

This is something that probably can be achieved by means of the tool/tex-input-editor of your choice which you use while working on the tex-source-code of your document.


By the way:

When using the hyperref package, the macro \@currentHref usually holds the name of the last hyperlink-anchor created so far.

That name in turn usually is created by means both of the name and the value of the counter and the values of all superordinate counters of the item of sectioning which a referencing label which is about to be created will refer to.

Thus when using the hyperref-package, you can probably patch the \label-command so that it also adds an entry to a list of referencing labels wherin both the name of the label and name and numbering of the labeled item of sectioning do occur.

This list gives you a clue about which referencing label is related to which item of numbered sectioning.

This list might be useful when the stage of developing the tex-source-code of the document is (almost) over:

During the development of the tex-source-code of the document you can provide names for referencing labels that reflect the content or purpose of the item of sectioning that you are labeling.

If you really absolutely want this, you can, when developing is (almost) over, use this list for making more easy the task of replacing these names by names that reflect the number which the item of sectioning that you are labeling does have within the .dvi-/.pdf-output-file.

The following code comes without warranties of any kind. I, e.g., cannot guarantee that it will not break whatsoever package that also fiddles around with cross-referencing.

The following code is intended to provide an outline about how patching the \label-command and maintaining a list of referencing labels could be implemented:

\documentclass{article}

\newcommand\mysavedcontentsline{}%
\let\mysavedcontentsline=\contentsline
\newcommand\mysavedaddcontentsline{}%
\let\mysavedaddcontentsline=\addcontentsline
\newcommand\mysavedlabel{}%

\usepackage{hyperref}

\makeatletter
\newcommand\addlabelnametolistofreferencinglabels[1]{%
  \mysavedaddcontentsline{lrp}{referencinglabel}{%
     Referencing~label \hyperref[#1]{\textbf{#1}} denotes \hyperref[#1]{\textbf{\@currentHref}}.%
  }%
}%
\newcommand\l@referencinglabel[2]{#1\smallskip\\}%
\newcommand\listofreferencinglabels{%
  %\begin{NoHyper}
  \section*{List of referencing~labels}%
  \begingroup
  \let\contentsline=\mysavedcontentsline
  \@starttoc{lrp}%
  \endgroup
  %\end{NoHyper}
}%
\AtBeginDocument{%
  \let\mysavedlabel=\label
  \renewcommand\label[1]{%
    \@bsphack
    \addlabelnametolistofreferencinglabels{#1}%
    \mysavedlabel{#1}%
    \@esphack
  }%
}%
\makeatother

\begin{document}
\section{A section}\label{A section}
\subsection{A subsection}\label{subsectionlabel}
\subsection{Another subsection}\label{subsectionlabel2}
\begin{equation}
1+1=2\label{eq1+1=2}%
\end{equation}

\listofreferencinglabels
\end{document}

enter image description here

E.g., the anchor-name "equation.1.1" tells you that the referencing label "eq1+1=2" is about something where the equation-counter plays a rôle and where the equation-counter has a superordinate counter (the section-counter, by the way; in other words: In this document equations are numbered within \sections and the equation-counter will be reset to 0 with the start of a new \section) whose value was 1 and the equation-counter itself also had the value 1.


The refcheck-package might be of interest to you, too. It provides means for detecting referencing labels which are not referenced within the current document.

  • Thank you for the well detailed answer. However, my question meant the following: If I have 3 numbered equations in my manuscript, I can assign each of them a \label{NiceEq}, \label{CoolEq}, \label{FinalResult}. On the other hand, I can write \label{Eq1}, \label{Eq2}, \label{Eq3}. Is there a way to get the latter result, but to be set automatically, and also update itself if for example I will add another equation between 2 and 3? This would make it so much easier for me to add references to these equations in the text. – user1611107 Jan 10 at 9:35
  • @user1611107 I just added a few remarks to my answer for addressing the issue of automatic updating of the names of referencing labels. – Ulrich Diez Jan 10 at 10:16
  • 1
    @user1611107 - The fact that LaTeX's cross-referencing system is asynchronous creates a serious complicating factor. Unless you can guarantee that your LaTeX document will never, ever crash, the dynamically updated label-ref system you appear to have in mind will necessarily become unstable. If compilation crashes half-way through the document, the aux file will necessarily be out of sync with the tex file -- without a reliable way for ascertaining whether the information in the aux file or in the tex file is "correct". Worse still, both sets could be incorrect! – Mico Jan 10 at 10:38
  • I think I understand the problem better now, thanks both. – user1611107 Jan 10 at 10:40

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.