Two objections come to mind:
- If you don't know you will be entering math mode, then you may write something "semantically" correct that breaks.
For example:
\documentclass{article}
\newcommand\mathmacro[1][A]{\ensuremath{{#1}_1}}
\begin{document}
\mathmacro[$x^2$] % The dollar signs *leave* math mode
\end{document}
What's going on here is that \mathmacro[$x^2$]
expands to
\ensuremath{{$x^2$}_1}
which expands to (effectively)
\ifmmode
{$x^2$}_1
\else
${$x^2$}_1$
\fi
and you can see that if you write it outside of math mode, the second branch is taken, so the first dollar sign brings you into math mode and the one at the front of $x^2$
takes you out of it, with the reverse operation happening afterwards. This gives an error.
Of course, you aren't supposed to do that, since \mathmacro
is actually a "math macro usable in text mode", so you should think of the thing between the brackets of its argument as being in math mode. Alas, this confuses both the author and the text editor's syntax highlighting, since it is a nonstandard assumption.
Edit: I would define this macro as:
\newcommand\mathmacro[1][A]{{#1}_1}
and use it as:
$x^2$ sub one: $\mathmacro[x^2]$.
This way, the parts that are math are clearly math.
- In the unlikely event that you or some package sets
\everymath
, you will be very surprised when your apparently text-mode macros start to look different.
On the subject of semantics, though, the issue is clear: \ensuremath
breaks the separation between math and text, which are two very different things. TeX even has the distinction built in: different fonts, different spacing rules, different parsing rules. You can probably construct a lot more counterexamples by exploiting these.
What I mean by this is that in the following situation:
\documentclass{article}
\newcommand\mathmacro[1][A]{\ensuremath{{#1}_1}}
\begin{document}
A sub one: \mathmacro
\bfseries A sub one: \mathmacro
\end{document}
you may be surprised that the bold text does not extend to the contents of the apparently text-mode \mathmacro
.
What I'm saying is not so much that \ensuremath
actually breaks anything as that it violates your expectations to the point that it makes things harder rather than easier.
Don Knuth touched on this topic in his article for TUGboat -- "Typesetting Concrete Mathematics". His examples don't include units (for that, the siunitx package is a good choice, as already mentioned), but the method for determining what is math and what isn't is well illustrated otherwise.
(The article is set in Knuth's Concrete fonts, and shows some of the special techniques used in setting that book. Irrelevant for this question, but interesting nonetheless.)
Best Answer
The classic Mathematics into Type, by Ellen Swanson (the AMS has made a PDF copy available
here
), gives a good explanation (Section 3.1 SPACING BETWEEN SYMBOLS IN MATHEMATICS) of when to use no space, thin space, thick space, em quad and two-em quad in math mode. A brief summary:No space
Between two symbols and between a number and the symbol it multiplies.
Before and after subscripts, superscripts, parentheses, braces, brackets, and vertical rules.
In expressions in the subscript or superscript.
Thin space
Before and after symbols used as verbs.
Before and after symbols used as conjunctions.
After, but not before
+
,-
,\pm
,\mp
used as an adjective.After the commas in sets of symbols, sequences of fractions, and coordinates of points.
Before and after the symbols of integration, summation, product, and union.
Before and after functions set in roman type. Exceptions: If any of these functions are preceded or followed by parentheses, braces, brackets, or bars, then the space is eliminated.
Before and after vertical rules appearing singly rather than in pairs; the same rule holds for a colon that is used as a mathematical symbol rather than as punctuation.
Before back subscripts.
Before and after
ds
,dx
, and similar combinations ofd
and another symbol following.Thick space
Before the parenthesis in congruences in text.
Before a mathematical condition in text.
Em quad
Between a symbolic statement and a verbal expression in displayed expressions.
Around conjunctions.
Two-em quad
Between two separate equations or inequalities in the same line.
Between a symbolic statement and a condition on the statement.
TeX has built-in spacing, and most of the times it does an excellent job, so (most of the times) you don't need to add space manually.
The following document contains the summary of the rules given by Swanson and some examples; these examples also contain some cases which are not built-in and which require manual attention.
The compiled code looks like :