# Can maths adornments know the “type” of their contents?

By "maths adornments" I mean things like \widehat (from the ams) and their ilk. Perhaps the following best illustrates what I mean:

Code to reproduce:

\documentclass{standalone}
\usepackage{amsmath}

\begin{document}
\begin{gather*}
A \widehat{\otimes} B \\
A \otimes B \\
A \mathbin{\widehat{\otimes}} B
\end{gather*}
\end{document}


Would it be possible to make \widehat (and others) "aware" of the type of their input? By "type" I mean whether the argument is a relation, operator, alphanumeric character, or ... are there any more? What it would do is to set itself to be the corresponding type. Thus, in my example above, \widehat{\otimes} would result in \mathbin{\widehat{\otimes}} whilst \widehat{A} would result in \mathalpha{\widehat{A}}.

I realise that there's a potential issue in that the input might be many characters, but for a single character it should be reasonably safe (and one could supply an optional argument to the augmented \widehat to override its choice if one disagreed).

(I just noticed this due to a fairly obvious spacing jump in a beamer presentation where I want to add a hat to a character on a slide. In the end, my transition command looked like:

\alt<3->{\mathbin{\widehat{\otimes}}}{\vphantom{\widehat{\otimes}}\otimes}


if one isn't bothered about spacing, one can write \only<3->{\widehat}\otimes.)

-
Sure, they can be made aware of their input. Can you explain again what they would do with this awareness? – Matthew Leingang May 16 '11 at 9:24
@Matthew: Whoops! That wasn't very clear, was it? Is the edited version any better? – Loop Space May 16 '11 at 9:41

One can exploit the macros used by ambsy:

\documentclass[a4paper]{article}
\usepackage{amsbsy}
\makeatletter
\DeclareRobustCommand{\iwidehat}[1]{
\begingroup
\math@atom{#1}{\widehat{#1}}
\endgroup}
\makeatother

\begin{document}
$a\otimes b_{a\otimes b}$

$a\iwidehat\otimes b_{a\iwidehat\otimes b}$

\end{document}


The package bm uses a similar approach in order to distinguish more types; this macro recognizes only relations and operations, the rest is treated as an ordinary symbol.

## Explanation

Here's the definition of \math@atom:

\def\math@atom#1#2{\binrel@{#1}\binrel@@{#2}}


The first macro \binrel@ does a measure and sets the meaning of \binrel@@; since this is used in a group, the original meaning of \binrel@@ is restored at the end of the group.

\let\binrel@@\relax

\def\binrel@#1{\begingroup
\setboxz@h{\thinmuskip0mu
\medmuskip\m@ne mu\thickmuskip\@ne mu
\setbox\tw@\hbox{$#1\m@th$}\kern-\wd\tw@
${}#1{}\m@th$}%
\edef\@tempa{\endgroup\let\noexpand\binrel@@
\ifdim\wdz@<\z@ \mathbin
\else\ifdim\wdz@>\z@ \mathrel
\else \relax\fi\fi}%
\@tempa
}


Here's what \binrel@ does. First of all it opens a group and sets box 0 with 0 \thinmuskip, negative \medmuskip and positive \thickmuskip. In the box it sets box 2 to the simple argument (\m@th is used to avoid the insertion of the \mathsurround kern). Then it backs up by the width of box 2 and sets the argument preceded and followed by (empty) ordinary atoms.

Case 1: #1 is an ordinary symbol or an operator.
The width of box 0 will be zero.

Case 2: #1 is an operation symbol.
\medmuskip glue will be inserted between {} and #1, and between #1 and {}, so the width of box 0 will be negative.

Case 3: #1 is a relation symbol.
\thickmuskip glue will be inserted between {} and #1, and between #1 and {}, so the width of box 0 will be positive.

The final trick is to do

\edef\@tempa{\endgroup<test>}\@tempa


that will do \let\binrel@@\relax in case 1, \let\binrel@@\mathbin in case 2, \let\binrel@@\mathrel in case 3.

-