Is it considered bad style to write:
x^2
, 2^\frac{1}{2}
and 2^\sqrt{2}
(without curly brackets around the superscript)
instead of:
x^{2}
, 2^{\frac{1}{2}}
and 2^{\sqrt{2}}
?
The same for subscripts.
Is it considered bad style to write:
x^2
, 2^\frac{1}{2}
and 2^\sqrt{2}
(without curly brackets around the superscript)
instead of:
x^{2}
, 2^{\frac{1}{2}}
and 2^{\sqrt{2}}
?
The same for subscripts.
x^2
is mildly bad style: the LaTeX manual always braces superscripts and subscripts also in cases like this.
2^\frac{1}{2}
is bad style and works by chance. See later.
2^\sqrt{2}
is an outright error and, indeed, you get
! Missing { inserted.
<to be read again>
\let
l.10 $2^\sqrt
{2}$
?
! Missing } inserted.
<inserted text>
}
l.10 $2^\sqrt{2}$
The output for all three examples is correct, but errors should never be ignored as error recovery attempted by TeX is never guaranteed to produce the expected result.
Why does 2^\frac{1}{2}
seem to work? Because \frac{1}{2}
ultimately produces {1\over 2}
, so the output is correct without errors just because of how \frac
is implemented. Luck.
Much similar is the bad habit of typing x_\mathrm{loc}
. For instance, if you use unicode-math
with the option mathrm=sym
, the input $x_\mathrm{loc}$
will raise an error. Conversely, x_\notin
will be accepted when unicode-math
is used, but explode with pdflatex
.
General rule Always brace superscripts and subscripts unless they're simple characters, when braces are optional. So, for instance,
\lim_{x\to 0^+}f(x)
is acceptable. When the superscript or subscript is a single command, it may or may not work without braces (see \notin
above). Learn to use braces. And remember that seeing the expected result does not guarantee that the wrong input will work in every circumstance.
2^\sqrt{2}
because it works in MathJax, but apparently not in real TeX - exactly what you said in your last sentence.
Commented
May 29 at 21:07
In general it is an error, with numbers even with only numbers of more than two digits it does not work correctly, even with some packages like xfrac (different presentation of fractions) when compiling it generates an error due to the lack of curly braces.
\documentclass[]{article}
\usepackage{mathtools}
\usepackage{xfrac}
\begin{document}
$2^3$ ok
$2^36$ incorrect
$2^{36}$ ok
$2^\frac{1}{2}$ ok
$x^{\sfrac{2}{4}}$ ok
$x^\sfrac{2}{4}$ fatal error
\end{document}
parentheses
to braces
or curly brackets
? Parentheses are not the same.
it generates an error due to the lack of parentheses
but that's not correct. Parentheses are (
and )
. {}
would be fine as a substitute and probably clearer to non-English speakers anyway, but parentheses
is wrong and so confusing.
{}
are called "curly brackets", []
are called "square brackets", and ()
are called "brackets". The words "braces" and "parentheses" are primarily American English terms, as far as I know.