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.
\varphi
is a math symbol and requires math mode. Since \mathrm
still "provides" math mode, \varphi
doesn't complain. On the other hand, \text
(from amstext
; loaded by amsmath
) is a macro that makes the correct choice in font size when switching to text mode - super convenient when typesetting test in exponents/subscripts. However, it still switches to text mode.
If you which to use \varphi
in text mode without having to switch to math mode explicitly, use something like
\newcommand{\textphi}{\ensuremath{\varphi}}
which will allow you to use \textphi
in either text or math mode. The \ensuremath
conditional switches to math mode only when necessary (when you're in text mode, say).
\documentclass{article}
\newcommand{\textphi}{\ensuremath{\varphi}}
\begin{document}
$\varphi^\varphi\ \textphi^\textphi$\ \textphi\textsuperscript{\textphi}
\end{document}
Best Answer
The difference is in their definition in
fontmath.ltx
:or, in
plain.tex
,Note that if you do
texdef -t latex backslash
andtexdef -t latex setminus
you getso there's no real difference between the two cases.
There's a substantial definition between a delimiter and a mathchar. The former type will honor
\big
and friends or\left
and\right
, the latter won't.Note that
\backslash
is defined to be class 0 (ordinary symbol) when used alone, whereas\setminus
has class 2 (binary operation symbol); this will affect spacing.If you need a “bigger
\setminus
” you can dobut
\big\setminus
would be like\big+
that's not going to work, because\big
needs a delimiter after it, that is, either a character with a nonzero\delcode
or a macro expanding to\delimiter<27 bit number>
.The usage of
\setminus
is very different from/
that, traditionally, has no space on either side and so TeX treats it as an ordinary symbol, also assigning it a nonzero\delcode
in LaTeX, and
in plain TeX.