3

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:

enter image description here Can anyone help?

  • \widebar redefines \mathaccent, which \widehat relies upon. Also, nesting accents is a dark corner in amsmath, see tex.stackexchange.com/questions/30327/… – egreg Apr 16 '14 at 20:53
  • @ egreg: So there's probably nothing to be done? \overline as a replacement isn't just as fine, even adding some math kerning… – Bernard Apr 16 '14 at 21:03
  • By the way, the hack suggested in my answer to the linked question (\compositeaccents) works, but the bar is not correctly computed as in \widebar{f\circ x}. – egreg Apr 16 '14 at 21:04
  • @I'll look at it. I also have the possibility to define a varwidebar for that kind of special case, using the \widebar command from mathabx (it supports \widehat in its argument, but may have other problems). – Bernard Apr 16 '14 at 21:18
8

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.

\documentclass{article}
\usepackage{scalerel}[2014/03/10]
\usepackage{stackengine}

\newcommand\reallywidetilde[1]{\ThisStyle{%
  \setbox0=\hbox{$\SavedStyle#1$}%
  \stackengine{1pt-\LMpt}{$\SavedStyle#1$}{%
    \stretchto{\scaleto{\SavedStyle\mkern.2mu\sim}{.5467\wd0}}{.5\ht0}%
%    .2mu is the kern imbalance when clipping white space
%    .5467++++ is \ht/[kerned \wd] aspect ratio for \sim glyph
  }{O}{c}{F}{T}{S}%
}}

\newcommand\reallywidehat[1]{\ThisStyle{%
  \setbox0=\hbox{$\SavedStyle#1$}%
  \stackengine{-1.0\ht0+.5pt}{$\SavedStyle#1$}{%
    \stretchto{\scaleto{\SavedStyle\mkern.15mu\char'136}{2.6\wd0}}{1.4\ht0}%
  }{O}{c}{F}{T}{S}%
}}

\newcommand\reallywidebar[1]{\ThisStyle{%
  \setbox0=\hbox{$\SavedStyle#1$}%
  \stackengine{.5pt+\LMpt}{$\SavedStyle#1$}{%
    \rule{\wd0}{\dimexpr.3\LMpt+.3pt}%
  }{O}{c}{F}{T}{S}%
}}

\def\test#1{$%
  \reallywidetilde{#1}\,
  \scriptstyle\reallywidetilde{#1}\,
  \scriptscriptstyle\reallywidetilde{#1}$~$
  \reallywidehat{#1}\,
  \scriptstyle\reallywidehat{#1}\,
  \scriptscriptstyle\reallywidehat{#1}$~$
  \reallywidebar{#1}\,
  \scriptstyle\reallywidebar{#1}\,
  \scriptscriptstyle\reallywidebar{#1}
$\par}

\parskip 1ex
\begin{document}

Normal math (different styles):

\test{abcde}
\test{abc}
\test{a}

Composites:

$\reallywidebar{D + \reallywidehat{C +\reallywidetilde{ag}}}$

$\reallywidebar{f \circ \reallywidehat{x}}$

\end{document}

enter image description here


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 the scalerel 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:

\def\fixDS#1{{\renewcommand\mathchoice[4]{##1}#1}}% FORCE \displaystyle
\def\fixTS#1{{\renewcommand\mathchoice[4]{##2}#1}}% FORCE \textstyle
\def\fixSS#1{{\renewcommand\mathchoice[4]{##3}#1}}% FORCE \scriptstyle
\def\fixsS#1{{\renewcommand\mathchoice[4]{##4}#1}}% FORCE \scriptscriptstyle

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 as

$\fixDS{\reallywidebar{D + \reallywidehat{C +\reallywidetilde{ag}}}}$

A similar approach can be used if the \mathchoices 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.

$\reallywidebar{A_{\reallywidebar{A_{\reallywidebar{A}}}}}$

$\fixDS{\reallywidebar{A_{\fixSS{\reallywidebar{A_\fixsS{\reallywidebar{A}}}}}}}$

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 for newtxmath, I reworked a version for that font. Also tweaked \reallywidetilde to match the font characteristics:

\documentclass{article}
\usepackage{scalerel}[2014/03/10]
\usepackage{stackengine}
\usepackage{newtxtext,newtxmath}

\newcommand\reallywidetilde[1]{\ThisStyle{%
  \setbox0=\hbox{$\SavedStyle#1$}%
  \stackengine{1pt-\LMpt}{$\SavedStyle#1$}{%
    \stretchto{\scaleto{\SavedStyle\mkern.2mu\sim}{.65\wd0}}{.5\ht0}%
%    .2mu is the kern imbalance when clipping white space
%    .5467++++ is \ht/[kerned \wd] aspect ratio for \sim glyph
  }{O}{c}{F}{T}{S}%
}}

\newcommand\reallywidehat[1]{\ThisStyle{%
  \setbox0=\hbox{$\SavedStyle#1$}%
  \stackengine{-1.0\ht0+1.8pt}{$\SavedStyle#1$}{%
    \stretchto{\scaleto{\SavedStyle\mkern.15mu\mathchar"0362}{1.3\wd0}}{1.15\ht0}%
  }{O}{c}{F}{T}{S}%
}}

\newcommand\reallywidebar[1]{\ThisStyle{%
  \setbox0=\hbox{$\SavedStyle#1$}%
  \stackengine{.5pt+\LMpt}{$\SavedStyle#1$}{%
    \rule{\wd0}{\dimexpr.3\LMpt+.3pt}%
  }{O}{c}{F}{T}{S}%
}}

\def\test#1{$%
  \reallywidetilde{#1}\,
  \scriptstyle\reallywidetilde{#1}\,
  \scriptscriptstyle\reallywidetilde{#1}$~$
  \reallywidehat{#1}\,
  \scriptstyle\reallywidehat{#1}\,
  \scriptscriptstyle\reallywidehat{#1}$~$
  \reallywidebar{#1}\,
  \scriptstyle\reallywidebar{#1}\,
  \scriptscriptstyle\reallywidebar{#1}
$\par}

\parskip 1ex
\begin{document}

Normal math (different styles):

\test{abcde}
\test{abc}
\test{a}

Composites:

$\reallywidebar{D + \reallywidehat{C +\reallywidetilde{ag}}}$

$\reallywidebar{f \circ \reallywidehat{x}}$

\end{document}

enter image description here

  • 1
    @user49915 Just tweaked .5467 to .65 and all is well. – Steven B. Segletes Feb 1 at 20:53
  • @user0 I am sorry, I do not have access to that font, so I can't check. But I would start with my answers at tex.stackexchange.com/questions/100574/really-wide-hat-symbol/… to see if they work for you. – Steven B. Segletes Feb 22 at 19:02
  • @user0 The first of the two solutions at the cited answer seems to work with unicode-math. – Steven B. Segletes Feb 22 at 19:05
  • @user0 The first -.6pt affects the left edge, the 2nd -.6pt affects the right edge, and the 0.5ex affects the hat height, and the [1pt] affects the gap between text and hat. Have at it. – Steven B. Segletes Feb 22 at 19:22
1

I fixed the code for \widebar so that nesting of accents is possible now. I didn't do any real testing, but it works nicely in your example:

output

$\widebar{f \circ \widehat{x}}$

  • Thank you so much! It was really lacking in latex. I'll test it in real conditions as soon as possible. – Bernard Apr 26 '14 at 9:55
  • @Hendtik Vogt: I began testing. Unfortunately there's a problem with the \mathbf command: when I type $\widebar{\mathbf{F}\circ x}, x is also in boldface. Similarly with mathrm, mathtt and probably any change of math alphabet, but not with \bm. A work around, for the moment, consists in writing $\widebar{\mathbf F}\circ x}$, which is not really satisfying. – Bernard Apr 26 '14 at 11:59
  • @Bernard: Oh, right. The old version already had that problem, too, so it appears that the new version is still better. I'll see if I can fix it. – Hendrik Vogt Apr 27 '14 at 6:44
  • Thankk you dor delving into this ˆproblem. Of course there was a typo in my previous comment: I meant $\widebar{{\mathbf F}\circ x}$. – Bernard Apr 27 '14 at 9:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.