# Why do arguments to nested \tilde or \breve commands reappear when amsmath is used?

On LaTeX-Community.org I've noticed a strange problem with nested \tilde and \breve commands, when amsmath is used.

In this code the X appears also in the output of the second and the third formula:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
$\breve{\tilde{X}}$
$\breve{\breve{{T}}\breve{{v}}}$
$\tilde{{\tilde{T}}{\tilde{v}}}$
\end{document}


If I don't load amsmath, the code works fine. This seems to be a bug in amsmath. How can it be solved?

-
Question 10.000 ;-) –  Marco Daniel Oct 3 '11 at 10:37
Our ten thousandth question! \o/ –  Paulo Cereda Oct 3 '11 at 10:37
Your work-round at latex-community.org/forum/viewtopic.php?f=46&p=56594 can be slightly simplified by making the first equation line \breve{\tilde{X}} \vphantom{\breve{}} –  mas Oct 3 '11 at 11:12

The problem comes up already with a "more minimal" example:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
$\breve{\breve{a}\breve{b}}$
\end{document}


which produces

! Undefined control sequence.


thus showing that the problem is not only in the repetition of the symbol. It happens with all math accents defined in terms of \mathaccentV,

\hat \check \tilde \acute \grave \dot \ddot \bar \vec \mathring


because these accents work by looking if their argument contains another accented symbol, in order to stack precisely the accents. This requires doing some global definitions, but somehow, if the argument contains two of these accents, the mechanism fails.

The "repeated symbol" is contained in the macro \macc@nucleus: after $\tilde{\breve{X}$, it expands to X and this is why this symbol is repeated (but I've not digged much into the details), since the definition of \macc@nucleus is done via \gdef.

The amsmath documentation doesn't point out that only stacked accents on one symbols should be used and, actually, it's safe to put an accent on a subformula provided it doesn't contain accents.

## Solution.

As the macros are quite complex and require global assignments, it seems quite difficult to do surgery on them, so a different approach is easier. Define

\newsavebox{\accentbox}
\newcommand{\compositeaccents}[2]{%
\sbox\accentbox{$#2$}#1{\usebox\accentbox}}


Now \breve{\breve{a}\breve{b}} can be changed into

\compositeaccents{\breve}{\breve{a}\breve{b}}


and all goes well. As one can see, the argument is typeset before applying the "global accent" and stored in a bin, over which we can safely put the "global accent".

Should one need such buildups also in superscripts or subscripts

\newcommand{\compositeaccentsX}[2]{%
\let\accenttemp#1\mathpalette\docompositeaccents{#2}}
\def\docompositeaccents#1#2{\compositeaccents\accenttemp{#1#2}}


and $A_{\compositeaccentsX{\breve}{\breve{a}\breve{b}}}$ will work. It's better to stick with the simpler command, as the "extended" one requires to typeset four times the same formula.

Curiously enough, the accents package shows a bug in situations like this one:

\documentclass{article}
\usepackage{amsmath,accents}
\begin{document}
$\breve{\breve{a}\breve{b}}$
\end{document}


will give no error, but will eat up the "a".

Very nice 10000th question on TeX.SE!

-
your first code example is missing a closing brace. –  Bruno Le Floch Oct 3 '11 at 11:54
Very good and it works well! I'll leave the question still open, looking for further fixes. –  Stefan Kottwitz Oct 3 '11 at 12:50
@StefanKottwitz I'm hoping that Javier Bezos is reading. His accents package is awesome and this bug should be addressed. –  egreg Oct 3 '11 at 12:53

this is certainly a bug, and has already been reported as such to tech-support@ams.org. it has been added to the list for the next overhaul of the amsmath package.

here are two additional small tests that provide useful information:

\documentclass[11pt]{article}
\usepackage[]{amsmath}
\begin{document}
$\breve{\breve{T} v}$
\end{document}


this results in everything being terribly misplaced.

\documentclass[11pt]{article}
\usepackage[]{amsmath}
\begin{document}
$\breve{\breve{T} \breve{v}}$
\end{document}


this crashes with

! Undefined control sequence.
l.6 \breve{\breve{T} \breve{v}}


thanks for the additional diagnosis and suggestions.

-
In some way the accent macro should decide whether its argument consists only of one Acc atom and, in this case, do its work as it does now; otherwise it should typeset the argument and then put the accent over it. –  egreg Oct 3 '11 at 13:22
Is that list accessible for the public? This could prevent such questions, if a bug is already known. –  Stefan Kottwitz Oct 3 '11 at 13:33
@Stefan -- good question. no, it's not. i'll discuss this with my group. however, the form of the list is very "telegraphic", with basically a one- or two-line description and a reference to sometimes quite extensive test files and documentation; it would not be a simple matter to make it easily digestible for someone who doesn't have access to the background material. –  barbara beeton Oct 3 '11 at 13:44

Another approach could be resetting \macc@nucleus at a suitable place, such as here:

\documentclass{article}
\usepackage{amsmath}
\let\amsmathaccentV\mathaccentV
\makeatletter
\def\mathaccentV#1#2#3#4#5{%
\amsmathaccentV{#1}{#2}{#3}{#4}{#5}%
\global\let\macc@nucleus\@empty
}
\makeatother
\begin{document}
$\breve{\tilde{X}}$
$\breve{\breve{{T}}\breve{{v}}}$
$\tilde{{\tilde{T}}{\tilde{v}}}$
\end{document}


Which gives:

However, there would still be the open problem that the complete accented expression might not have its natural width, which can be seen if we let characters or symbols follow.

Mentioning this on the way of finding a good fix for amsmath.

-
It won't work with the "more minimal" example. –  egreg Oct 3 '11 at 13:07
@egreg: Yes, I see, the other error regarding \macc@kerna still occurs in that MWE. Just adding a thought which may become a step in fixing. At least it fixes the question. –  Stefan Kottwitz Oct 3 '11 at 13:11

If you load the accents package (after loading amsmath) with the single option set, you won't get the problems you describe:

\documentclass{article}
\usepackage{amsmath}
\usepackage[single]{accents}
\begin{document}
$\breve{\breve{a}\breve{c}}$, $\tilde{\tilde{T}\tilde{v}}$
\end{document}


Observe that the positioning of the upper (second-storey) accent will be quite poor if the accented glyphs below it have significantly different heights and/or widths (as is the case with the second example above). If they're available, one should use the wide variants of the accent symbols for the upper (second-story) accents, e.g.,

$\widetilde{\tilde{T}\tilde{v}}$


Of course, there is (to my knowledge) no "wide" version of the breve accent, so this piece of advice can't be applied for all accents...

Addendum: A caveat to the generality of this solution: I've just discovered that if one uses the accents package for some of the additional accents it provides (such as \ddddot), you can't use these additional accents in the upper position in the examples above as, in that case, the first letter (but, curiously, not its accent) will be gobbled up. I've only tested this with the ddddot accent, to be sure, but this problem may be wider spread. :-(

-