# Can one determine the first character of a given math list?

I'm still pursuing my quest to code a better \widebar command. I can do bars over single characters quite well already, but I have problems when it comes to letter combinations such as AW. \overline{AW} produces

which doesn't look nice in my opinion: the bar extends too far to the left; it doesn't take the skew of the A into account.

To correct this, I must be able to find out the first character of the argument of my \widebar command. For AW this is easy, but I also would like to cover the following arguments:

1. \mathcal{AW} or \mathcal{A}W (where \mathcal{A} is the first character),
2. \sin x (where an upright s is the first character),
3. \mathchar"0141 (which is just the character A from the standard math font),
4. \left(a^2+b^2\right) (where some ( is the first character).

Maybe #4 is too tricky since a large ( might turn out to be a box and not a character. Of course, fractions and radicals shouldn't come up in the beginning of the argument; personally, I wouldn't want to overline such quantities.

So my question is: given a math list that starts with a character, can one find out what that first character is?

-
you cant deconstruct a math list in classic tex, however you could grab the tokenlist and inspect it by hand, similar to the way bm deconstructs math expressions to build an equivalent bold version –  David Carlisle Sep 12 '12 at 19:07
@David: Thanks, I had already feared that one has to do it the hard way. Maybe one can hack into bm to solve the problem? –  Hendrik Vogt Sep 12 '12 at 19:16
bm is a delicate thing, treat it with care if hacking..... –  David Carlisle Sep 12 '12 at 19:20
@David: OK :-) –  Hendrik Vogt Sep 12 '12 at 19:21

This is more or less bm reconstructed a bit. It produces

First letter A in \symsymbols
First letter A in \symsymbols
First letter s in \symoperators
First letter A in 1
First letter (


On the examples given in the question.

\documentclass{article}

\makeatletter
\def\firstchar#1{\begingroup%
\let\fcmathgroup\relax
\let\protect\@empty
\let\@typeset@protect\@empty
\let\mathop\@firstofone
\let\use@mathgroup\insert
\def\left##1{\ifx.##1\null\fi##1}%
\fc@expand#1\null\valign
\endgroup}

\def\fc@expand{\afterassignment\fc@exp@nd\count@\a}
\def\fc@exp@nd{\afterassignment\fc@test\count@\a}
\def\fc@test{\futurelet\@let@token\fc@test@}

\def\fc@test@{%
\let\fc@next\@empty
%\show\@let@token
\ifx\@let@token\relax
\let\fc@next\fc@gobble
\else\ifx\@let@token\bgroup
\let\fc@next\fc@group
\else\ifx\@let@token\use@mathgroup
\let\fc@next\fc@usemgroup
\else\ifx\@let@token\mathgroup
\let\fc@next\fc@mgroup
\else\ifx\@let@token\mathchar
\let\fc@next\fc@mathchar
\else
\let\fc@next\fc@show
\fi\fi\fi\fi\fi
\fc@next
}

\def\fc@show#1#2\valign{%
%\def\xshow{#1|||[#2]}\show\xshow
\typeout{First letter #1 %
\ifx\fcmathgroup\relax\else in \expandafter\string\fcmathgroup\fi
}
}

\def\fc@gobble#1{%
%\def\xgobble{#1}\show\xgobble
\fc@expand
}

\def\fc@group#1{%
%  \def\xgroup{#1}\show\xgroup
\fc@expand#1}

\def\fc@usemgroup#1#2#3#4{%
%  \def\xumathgroup{#1//#2//#3/#4}\show\xumathgroup
\def\fcmathgroup{#3}%
\fc@expand#4}

\def\fc@mgroup#1#2{%
%\def\xmathgroup{#1//#2}\show\xmathgroup
\def\fcmathgroup{#2}%
\fc@expand}

\def\fc@mathchar#1{%
\afterassignment\fc@mathchar@\count@}

\def\fc@mathchar@{%
\@tempcnta\count@
\@tempcntb\count@
\divide\@tempcnta"100
\multiply\@tempcnta"100
\uccode\a\@tempcntb
\@tempcntb\@tempcnta
\divide\@tempcntb"1000
\multiply\@tempcntb"1000
\divide\@tempcnta"100
\edef\fcmathgroup{\the\@tempcnta}%
\uppercase{\fc@expand a}}

\makeatother

\begin{document}

$a$

%\tracingall
$\firstchar{\mathcal{AW}}$

$\firstchar{\mathcal{A}W}$

$\firstchar{\sin x}$

$\firstchar{\mathchar"0141}$

$\firstchar{\left(a^2+b^2\right)}$
\end{document}

-
The \fc@mathchar@ part is the best. I got to the end, having skimmed the big ugly TeX arithmetic part, and thought "but what if the character is given by its code?" And there it is: \uppercase{a}` gets a typesetting construct into the input. Clever! –  Ryan Reich Mar 17 '13 at 15:30