# How to find out if a math list consists of a single character?

When accents are placed, the TeX engine gives single characters a special treatment; see the TeXbook, Rule 12 on page 443. See also this question and its accepted answer for details. Thus, for fake accents like those provided by the accents package, it would be useful to find out whether a given math list consists of a single character. Examples of single characters: a, \alpha, \mathcal{A}. In contrast, ab, a{} and a\hspace{0pt} are not single characters.

Thus my question: How do I find out if a given math list consist of a single character?

-

Part of the special treatment of single characters in accent placement is the handling of sub- and superscripts. From the TeXbook, p. 443, Rule 12:

If the nucleus is a single character, replace box x [which contains the nucleus in style C'] by a box containing the nucleus together with the superscript and subscript of the Acc atom, in style C, and make the sub/superscripts of the Acc atom empty; ...

Roughly speaking, the effect is that in \hat{a}^H, the H is "attached" to the a, whereas in \hat{\kern0pt a}^H it is "attached" to \hat{\kern0pt a} and thus placed quite a bit higher. Here's an \ifsingle command (with a true- and a false-branch), based on this idea. First the output:

\documentclass{article}
\newcommand*\ifsingle[3]{%
\setbox0\hbox{$\mathaccent"0362{#1}^H$}%
\setbox2\hbox{$\mathaccent"0362{\kern0pt#1}^H$}%
\ifdim\ht0=\ht2 #3\else #2\fi
}
\newcommand*\test[1]{$#1$ is\ifsingle{#1}{}{ not} a single character\par}
\newcommand*\stupida{\string a}
\newcommand*\awithbraces{a{}}
\begin{document}
\test{a}
\test{\alpha}
\test{\mathcal{A}}
\test{ab}
\test{a_1}
\test{\hat{a}}
\test{\stupida}
\test{\awithbraces}
\end{document}


As egreg points out in the comments, \ifsingle does not identify [, \sum, , and + as single characters. (And I modified the code so that = isn't identified as single, either.) However, I think this is not a big deal: in \hat{=}, the \hat doesn't see a single character, either, since {=} is not a single character. Only in \hat=, the \hat sees the single character =. See this awesome answer by Frank Mittelbach for details.

-
The test breaks when you use a delimiter, a math operator such as \sum, a punctuation symbol or a binary operation: try \test{[}, \test{\sum}, \test{,} and \test{+}. It works for \test{=}. –  egreg Sep 7 '12 at 12:02
@egreg: That's an interesting observation! I'm not sure what to conclude from it. My guess: from the point of view of the TeX engine, [ is not a single character. What do you think? (Changing the \mathcode of [ to "705B will make it a single character.) –  Hendrik Vogt Sep 7 '12 at 17:47
One should go in depth into Appendix G, I guess. My attempt was to compare the heights of \hbox{$#1$} and \hbox{$\mathop{#1}$}, which gives similar results (but misses relation symbols, in addition to big operators, punctuation, delimiters and binary operations). –  egreg Sep 7 '12 at 18:00
@egreg: I fear that even Appendix G might not help here; I've read it many times. Note that \mathord\sum and so on result as single characters in my test. –  Hendrik Vogt Sep 7 '12 at 18:11
@egreg: That = was identified as a single character was a mistake in the code. It appears that only ordinary characters are entitled to be "single characters" as in Rule 12. See my edit. –  Hendrik Vogt Sep 8 '12 at 8:36