1

I am currently getting into LaTeX, mainly for writing texts with formulas. I have already some experience writing in latex from other SE sites such as Physics or Mathematics. Now I started learning writing LaTeX in a dedicated software (I use TeXstudio, but this probably won't be relevant).

I noted that I cannot use the \tag command when using equations with $$...$$. For example, the following code:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
    $$a=b\tag{1}$$
\end{document}

outputs "a=b" and not "a=b (1)". I also get a error code:

Package amsmath Error: \tag not allowed here. $$a=b\tag

After some more experimenting, I found that the only (?) way to use the \tag command is by using an equation workspace:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
    \begin{equation}
        a=b\tag{1}
    \end{equation}
\end{document}

which outputs the correct result

enter image description here

Using the equation workspace, you get a default number tag, but this way, you can change it to whatever value you'd like to.

So I have two questions:

  1. Why is it not possible to add a tag when using equations with $$...$$ or $...$?

  2. Is there any workaround? Using an equation environment isn't much of an issue, but I sometimes find the "$" method more comfortable

5
  • 3
    You might want to take a look at What are the differences between $$, [, align, equation and displaymath?
    – leandriis
    Dec 3, 2020 at 19:07
  • 4
    Instead of using $$...$$, use \[...\] that allows you to use \tag.
    – Werner
    Dec 3, 2020 at 19:09
  • $$ should never be used in latex whether or not you use \tag Dec 3, 2020 at 21:34
  • @DavidCarlisle after reading the various linked questions, I probably will never do again :) I was used to using $$ from the SE version of Latex and thought that it was "normal"
    – jng224
    Dec 3, 2020 at 21:48
  • 1
    note the latex-like rendering on the stackexchanege sites is MathJax (Javascript) and doesn't actually use tex at all. Dec 3, 2020 at 21:52

1 Answer 1

3

If you run

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\show\tag
\end{document}

you'll see

> \tag=macro:
->\invalid@tag {\string \tag \space not allowed here}.
l.4 \show\tag

The macro \tag is defined by amsmath, and its "normal" definition is to yield an error. Only when entering one of the amsmath math environments the macro \tag is redefined to do what it should do. For example, the expansion of \begin{equation}, \begin{equation*}, and \[ involve the macro \mathdisplay, whose definition contains the line

\let\tag\tag@in@display

Similarly, align, flalign, gather, and multline result in

\let\tag\tag@in@align

Since these \let assignments happen in a group, the redefinition of \tag is local to the current environment, and upon exiting it \tag resumes its normal definition (yielding an error).

This explains your "why" issue: when you use $$ none of the above \let is executed, and \tag yields an error.

Is there a workaround? There might be. You could do as described in Defining $$$ $$$ to align.

Don't. Ever. Please read Why is \[ … \] preferable to $$ … $$? and then forget the previous paragraph.

If you want to give a manual \tag to a single-line equation, use \[...\]. It involves the same amount of strokes as $$...$$. Admittedly, the German keyboard layout does not make this easy; that's why I have a qwerty keyboard even if I live in Germany :-)

You must log in to answer this question.

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