# Mathaccents, fontdimens, macros, oh my!

Why doesn't this work?

\def\circacc{%
\dimen0=\fontdimen5\textfont2
\fontdimen5\textfont2=-.5ex
\mathaccent\circ
\fontdimen5\textfont2=\dimen0
}$\circacc a$\bye


The last line produces an error (! Missing { inserted.), but why on earth when it worked just two lines ago?!

If I change the macro to include an argument I can get it to work, but then the last \fontdimen-change gets applied before the accent has been typeset!

I'm afraid I'm on TeX Wizard -lands now, and I'm lost.

Update: The first part of the question is now clear to me. However, the grouped version didn't work for me for the second (w/ args), is there something I'm still missing?

\def\circacc#1{
\dimen0=\fontdimen5\textfont2 \dimen1=\fontdimen5\scriptfont2 \dimen2=\fontdimen5\scriptscriptfont2
{\fontdimen5\textfont2=-.25ex \fontdimen5\scriptfont2=-.25ex \fontdimen5\scriptscriptfont2=-.25ex
\mathaccent\circ{#1}}
\fontdimen5\textfont2=\dimen0 \fontdimen5\scriptfont2=\dimen1 \fontdimen5\scriptscriptfont2=\dimen2
}$\circacc W$\bye


(produces the accent with the original x-height)

-
As always please provide a minimal example even/especially if it is more or less trivial. Also stating the exact error would be great. –  Martin Scharrer May 17 '11 at 19:25
@Martin: better now? –  morbusg May 17 '11 at 19:32
Yes, much better. Thanks. In general you should also add that it should be compiled with plainTeX. –  Martin Scharrer May 17 '11 at 19:34
@Martin: I don't see anyone mentioning that they should compile with LaTeX in other questions. Discrimination! ;-) –  morbusg May 17 '11 at 19:38

Between \accent and the character to be accented assignments can intervene; not between \mathaccent and the atom to be accented. Since TeX expects either a math character or a subformula and \fontdimen isn't a character, it tries to insert a brace.

The first idea would be to define \circacc with an argument and do the reassignment after having typeset the accented symbol:

\def\circacc#1{%
\dimen0=\fontdimen5\textfont2
\fontdimen5\textfont2=-.5ex
\mathaccent\circ{#1}
\fontdimen5\textfont2=\dimen0
}


This, unfortunately, fails: the two assignments do nothing, since TeX converts a math list into a horizontal list when the math list is finished and so when the \fontdimen is again what was before. One has to add a group and convert the math list to a horizontal list:

\def\circacc#1{%
\dimen0=\fontdimen5\textfont2
\hbox{$\fontdimen5\textfont2=-.5ex \mathaccent\circ{#1}$}
\fontdimen5\textfont2=\dimen0
}


The reassignment to \fontdimen5 has to be redone, since assignments to a \fontdimen are inherently global (they are <font assignments>, TeXbook p. 277).

Of course this doesn't work for $a_{\circacc{W}}$, for example, so I wouldn't recommend this approach: the accents package does this in a safer way.

-
Thanks, that answers the first part perfectly! –  morbusg May 17 '11 at 19:35
Thanks for the critic :-) I was just pointing out that the error would disappear when using an argument but didn't looked further into the task. –  Martin Scharrer May 17 '11 at 20:32
Hmm, if I'm reading you right, you are saying that the grouped version should work, right? But I only get the original x-height for \circacc. I'll update the question. –  morbusg May 18 '11 at 7:17
@morbusg: you're right, there was a small error. :( A \hbox is necessary. –  egreg May 18 '11 at 8:06