# Using labels with mathJax?

When using an equation in an question or an answer, it is sometimes useful to give it a number and refer to it in the text. The \tag command works : if you add \tag{1} in the equation environment, a nice "(1)" shows up on the right hand side of the equation. But how can one refer to it in the text in another way than typing (1) ? I have tried to use \eqref{1} but this only shows "(???)". Using the command \label{1} in the equation solves the \eqref issue, but the equation is not displayed correctly anymore, only the LaTeX code appears in the preview window. Is there a way to have \eqref work and \label as well ?

• Are you and V Moretti related ? – Abhimanyu Pallavi Sudhir Jan 9 '14 at 11:23
• @Dimensio1n0. Not at all. I think he and I have quite common Italian names. – Tom-Tom Jan 9 '14 at 13:21
• Are you really writing something so long that you need equation labeling? – Emilio Pisanty Jan 10 '14 at 1:37

Note that \tag, \label, and \eqref all play different roles in the process, and you would need to use all three to accomplish your goal. The \tag{x} macro adds an equation number that appears at the right of the equation; it does not create any symbolic name for use with \eqref. That is done through \label{y} which assigns the name y to the tag that is used in the equation where \label{y} appears. Note that \label{y} does not create an equation number, and so if you use \label without a \tag, the \eqref will have no number to refer to, and you get the (???).

The reason that you are having trouble with labels in question and answer previews is that the preview code reprocesses all the math every time that you type a new character. That means that the \label commands are processed over and over again. Since a label name can only be used once, when a label is processed a second time, MathJax throws an error (which is trapped and shown as the TeX code in a box). If you finish answer and post it, it would work properly, since when the page is shown, each label is only used once.

Since the StackExchange sites don't use automatic equation numbering, you have to give an explicit \tag, so there is really no need to use \label (unless you plan to change the tags after the fact), because you know what the equation tag is already. So you might as well use the explicit references by typing (1) as you suggest above. If you want a link to the equation (the other thing that \eqref provides), then you can make one explicitly. For example, if you use

$$E=mc^2\tag{1}$$


then you can use

[(1)](url-to-this-question#mjx-eq-1)


to link to the tagged equation (where url-to-this-question is replaced by the URL to the current question).

• Note that MathJax does provide a method to reset the labels, and that could be used in the preview to avoid the problem you are having. But labels and references were added to MathJax after the preview code was written, so it's not there now, and would require modifying the code. I'm not sure if the SE staff would be interested in that or not. – Davide Cervone Jan 10 '14 at 0:59
• I think they might be, if it wouldn't be a complicated change. I'll point out your answer to the community team so they can consider it. – David Z Jan 10 '14 at 5:55
• Thank you all for your answers. The point was actually to get the link, because I find it comfortable for me as a reader to click an equation's number, remember what it says, and then move back to where I was. The solution proposed by Davide works but it requires posting and then editing the links, because you don't know the URL in the first place. – Tom-Tom Jan 10 '14 at 8:37
• @David. @Davide. I comment now on the above example URL. One would actually get a link to url-to-this-question#mjx-eq-label1 if one had used \tag{tag1}\label{label1} in the equation and this link would appear as "(tag1)". So this means that in a question and all answers to it, the label \label1 can be defined only once. When typing an answer to a question, according to what Davide explained, it is not possible to know whether this label was already used in another answer. Edits could be mandatory as well. This is probably a reason why the use of labels is not recommended in SE. – Tom-Tom Jan 10 '14 at 8:46
• @V.Rossetto: While you are right that there could be conflicts between the use of \label in different answers, I think the real reason they don't recommend it is the one that you ran into above, which is that the previewer doesn't reset the labels, and so you get errors while previewing. Should they fix that, then yes, you will have to be careful not to reuse labels that others have used. Perhaps a convention that prepends your userID to the labels you create would be a good idea. – Davide Cervone Jan 10 '14 at 12:07