I met across a serious problem using the widebar
math command defined by Hendrik Vogt as an answer to question 16337. It worked fine until I used \(wide)hat
in its argument. Here is a M(non)WE:
\documentclass[12pt, a4paper]{article}
\usepackage{amsmath}%
\makeatletter
\newcommand*\if@single[3]{%
\setbox0\hbox{${\mathaccent"0362{#1}}^H$}%
\setbox2\hbox{${\mathaccent"0362{\kern0pt#1}}^H$}%
\ifdim\ht0=\ht2 #3\else #2\fi
}
%The bar will be moved to the right by a half of \macc@kerna, which is computed by amsmath:
\newcommand*\rel@kern[1]{\kern#1\dimexpr\macc@kerna}
%If there's a superscript following the bar, then no negative kern may follow the bar;
%an additional {} makes sure that the superscript is high enough in this case:
\newcommand*\widebar[1]{\@ifnextchar^{{\wide@bar{#1}{0}}}{\wide@bar{#1}{1}}}
%Use a separate algorithm for single symbols:
\newcommand*\wide@bar[2]{\if@single{#1}{\wide@bar@{#1}{#2}{1}}{\wide@bar@{#1}{#2}{2}}}
\newcommand*\wide@bar@[3]{%
\begingroup
\def\mathaccent##1##2{%
%If there's more than a single symbol, use the first character instead (see below):
\if#32 \let\macc@nucleus\first@char \fi
%Determine the italic correction:
\setbox\z@\hbox{$\macc@style{\macc@nucleus}_{}$}%
\setbox\tw@\hbox{$\macc@style{\macc@nucleus}{}_{}$}%
\dimen@\wd\tw@
\advance\dimen@-\wd\z@
%Now \dimen@ is the italic correction of the symbol.
\divide\dimen@ 3
\@tempdima\wd\tw@
\advance\@tempdima-\scriptspace
%Now \@tempdima is the width of the symbol.
\divide\@tempdima 10
\advance\dimen@-\@tempdima
%Now \dimen@ = (italic correction / 3) - (Breite / 10)
\ifdim\dimen@>\z@ \dimen@0pt\fi
%The bar will be shortened in the case \dimen@<0 !
\rel@kern{0.6}\kern-\dimen@
\if#31
\overline{\rel@kern{-0.6}\kern\dimen@\macc@nucleus\rel@kern{0.4}\kern\dimen@}%
\advance\dimen@0.4\dimexpr\macc@kerna
%Place the combined final kern (-\dimen@) if it is >0 or if a superscript follows:
\let\final@kern#2%
\ifdim\dimen@<\z@ \let\final@kern1\fi
\if\final@kern1 \kern-\dimen@\fi
\else
\overline{\rel@kern{-0.6}\kern\dimen@#1}%
\fi
}%
\macc@depth\@ne
\let\math@bgroup\@empty \let\math@egroup\macc@set@skewchar
\mathsurround\z@ \frozen@everymath{\mathgroup\macc@group\relax}%
\macc@set@skewchar\relax
\let\mathaccentV\macc@nested@a
%The following initialises \macc@kerna and calls \mathaccent:
\if#31
\macc@nested@a\relax111{#1}%
\else
%If the argument consists of more than one symbol, and if the first token is
%a letter, use that letter for the computations:
\def\gobble@till@marker##1\endmarker{}%
\futurelet\first@char\gobble@till@marker#1\endmarker
\ifcat\noexpand\first@char A\else
\def\first@char{}%
\fi
\macc@nested@a\relax111{\first@char}%
\fi
\endgroup
}
\makeatother
\begin{document}
$ \widebar{f \circ \widehat{x}} $
\end{document}
The .log file delivers this:
! TeX capacity exceeded, sorry [grouping levels=255].
<to be read again>
\macc@style
l.195 ...i(f)^*} = \widebar{f \circ \widehat{x}} =
\widebar f \circ \widehat...
If you really absolutely need more capacity, …
Without the \widehat
command, it's perfect:
Can anyone help?
Best Answer
REVISED to add important note at end:
Based on my answer at Big tilde in math mode, I adapted similar approaches for a wide hat and bar. After showing them in various math styles, I show composites of them, including the function of interest to you.
IMPORTANT NOTE: (further revised based on better understanding)
I would make one interesting observation. The way TeX handles different mathstyles automatically is with
\mathchoice
, in which four boxes are constructed, and only one of them used, depending on the local mathstyle. The\ThisStyle{...\SavedStyle...}
construct of thescalerel
package used in this MWE is a glorified version of\mathchoice
that helps save on typing.But regardless of whether
\mathchoice
or\ThisStyle
is used to achieve the result, the nesting of\mathchoices
can become very computationally inefficient.For cases like this, it might make sense to override the
\mathchoice
and tell it which of the boxes to use. One can achieve that by defining these macros:In the case of my 2nd to last example, because all
\mathchoice
symbols occur in\displaystyle
, one could save a lot of compilation time by invoking the call asA similar approach can be used if the
\mathchoice
s occur in different mathstyles. For example, The first of the following forms constructs many boxes before choosing one, while the second form bypasses the\mathchoice
branching.This hardwiring approach need only to be considered if the computational cost of
\mathchoice
nesting presents a notable slowdown in compilation.SUPPLEMENT
After having been informed that the above approach for
\reallywidehat
did not look so pretty fornewtxmath
, I reworked a version for that font. Also tweaked\reallywidetilde
to match the font characteristics: