You can display the definitions of this single-letter macros using:
$ texdef -t latex a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
The texdef
command is available on CTAN.
Most of them seem to be diacritics. See https://en.wikibooks.org/wiki/LaTeX/Special_Characters for a table.
Here the post-formatted output of the above texdef
:
Undefined:
\e \f \g \h \m \n \p \q \s \w \x \y \z
\A \B \C \D \E \F \G \I \J \K \M \N \Q \R \T \U \V \W \X \Y \Z
\a:
macro:#1->\expandafter \@changed@cmd \csname \string #1\endcsname \relax
\b:
macro:->\OT1-cmd \b \OT1\b
\c:
macro:->\OT1-cmd \c \OT1\c
\d:
macro:->\OT1-cmd \d \OT1\d
\i:
macro:->\OT1-cmd \i \OT1\i
\j:
macro:->\OT1-cmd \j \OT1\j
\k:
macro:->\T1-cmd \k \T1\k
\l:
macro:->\OT1-cmd \l \OT1\l
\o:
macro:->\OT1-cmd \o \OT1\o
\r:
macro:->\OT1-cmd \r \OT1\r
\t:
macro:->\OML-cmd \t \OML\t
\u:
macro:->\OT1-cmd \u \OT1\u
\v:
macro:->\OT1-cmd \v \OT1\v
\H:
macro:->\OT1-cmd \H \OT1\H
\L:
macro:->\OT1-cmd \L \OT1\L
\O:
macro:->\OT1-cmd \O \OT1\O
\P:
macro:->\protect \P
\P :
\long macro:->\ifmmode \mathparagraph \else \textparagraph \fi
\S:
macro:->\protect \S
\S :
\long macro:->\ifmmode \mathsection \else \textsection \fi
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.
Best Answer
With the
amsmath
package loaded:in your preamble. Use it in an equation with
\diam
. This will give you the upright text that you want, and the proper spacing. See section 5.1 of theamsmath
documentation for more details. For single use, instead of defining a macro\operatorname{diam}
will have the same effect if you use it directly inside an equation.For bits of text that are not functions in the mathematical sense, e.g. if you want to put some upright text in a subscript, this package also makes the
\text{...}
command available. You can use this in a\newcommand
as usual. See this question for a demonstration of the\text
command.