8

I'm used to using \tag without braces like \tag3 instead of \tag{3} in Markdown+MathJax, where it works as fine as does \frac12 in normal LaTeX. But when I tried to do this in a real .tex source processed with pdflatex, I got some errors.

Here's the code:

\documentclass[]{article}
\usepackage{amsmath}
\begin{document}

\[f(x)=\frac32\tag1\]

\end{document}

And this is what pdflatex says

This is pdfTeX, Version 3.14159265-2.6-1.40.18 (TeX Live 2017/Debian) (preloaded format=pdflatex)
 restricted \write18 enabled.
entering extended mode
(./test.tex
LaTeX2e <2017-04-15>
Babel <3.18> and hyphenation patterns for 3 language(s) loaded.
(/usr/share/texlive/texmf-dist/tex/latex/base/article.cls
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
(/usr/share/texlive/texmf-dist/tex/latex/base/size10.clo))
(/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsmath.sty
For additional information on amsmath, use the `?' option.
(/usr/share/texlive/texmf-dist/tex/latex/amsmath/amstext.sty
(/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsgen.sty))
(/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsbsy.sty)
(/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsopn.sty))
No file test.aux.
Runaway argument?
1\] 
! Paragraph ended before \tag was complete.
<to be read again> 
                   \par 
l.6 

When I surround the 1 with braces, it compiles fine, and I get the expected PDF output. So I wonder, why does \tag require braces while \frac doesn't? Is there some difference in "type" of the object, like e.g. \tag being a package-supplied facility while \frac is from the language core (doesn't need \usepackage at least) or something like that?

Ruslan
  • 262

1 Answers1

12

The macro \frac is defined (robustly) as

\frac#1#2 -> \begingroup #1 \endgroup \@@over #2

where \@@over is (a copy of) the TeX primitive. So in this case you can indeed write \frac12 and it will be the same as \frac{1}{2}. A lot of people, included myself, will tell you not to do this. (Although for such short fractions I don't always follow my own advice...)

On the other hand, at the beginning of displaymath \tag is \let to \tag@in@display, whose definition reads

\def\tag@in@display#1#{\relax\tag@in@display@a{#1}}

The tricky bit is the # before the opening brace of the replacement text. TeX looks for the first argument up to an opening brace (later it will check if this first argument is a star *). For details on how this mechanism works I refer to the questions Grab to #{ macro arguments (in particular Martin Scharrer's answer) and Macros with # as the last parameter. But the short answer is: TeX is scanning for an opening brace to collect the argument #1 and at some point finds the empty line (which is converted to a new paragraph), which isn't allowed, and hence the Paragraph ended error.

campa
  • 31,130
  • I think the last sentence is not correct. TeX finds \par (namely the empty line after \]) before expanding \tag, while it is scanning for its argument. It isn't looking for a closing brace but scanning for the opening brace delimiting that argument. – schtandard Aug 13 '19 at 21:54
  • @schtandard Yeah, that's absolutely true. I've edited. – campa Aug 14 '19 at 07:18