3

This is a question that arose from using Hendrik Vogt's \widebar. I noticed that superscripting a widebar letter raises the superscript. Is there any way to make the superscript stay in the original height? (And is it bad syntax to do so?)

\widebar{\mathbb R}^2 \quad \mathbb R^2 
\quad 
\widebar R^2 \quad R^2

Superscripted R

\documentclass[preview]{standalone}
\usepackage{amsmath,amssymb}

%%%%%%%%%%%%%%%%%%%%%%
%% Hendrik's answer %%
%%%%%%%%%%%%%%%%%%%%%%
\makeatletter
\let\save@mathaccent\mathaccent
\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{%
        %Enable nesting of accents:
        \let\mathaccent\save@mathaccent
        %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
%%%%%%%%%%%%%%%%%
%% End Hendrik %%
%%%%%%%%%%%%%%%%%

\begin{document}
$
\widebar{\mathbb R}^2 \quad \mathbb R^2 
\quad 
\widebar R^2 \quad R^2
$
\end{document}
  • 1
    make a new macro that does \widebar{#1}\vphantom{#1} – daleif Sep 13 '17 at 11:04
3

The problem is, that the superscript is set dependent on the height of the prior token. You could tell LaTeX that the token has the height 0pt using \smash or use \vphantom to let the token prior to the superscript have the height of the content without \widebar:

\documentclass[preview,border=4mm]{standalone}
\usepackage{amsmath,amssymb}

%%%%%%%%%%%%%%%%%%%%%%
%% Hendrik's answer %%
%%%%%%%%%%%%%%%%%%%%%%
\makeatletter
\let\save@mathaccent\mathaccent
\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{%
        %Enable nesting of accents:
        \let\mathaccent\save@mathaccent
        %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
%%%%%%%%%%%%%%%%%
%% End Hendrik %%
%%%%%%%%%%%%%%%%%

\newcommand*{\widebarpot}[2]{%
  \smash{\widebar{#1}}^#2}% or \widebar{#1}\vphantom{#1}^#2

\begin{document}
$
\widebar{\mathbb R}^2 \quad \mathbb R^2 
\quad 
\widebar R^2 \quad R^2
$\\
$
\widebarpot{\mathbb R}{2} \quad \mathbb R^2 
\quad 
\widebarpot{R}{2} \quad R^2
$
\end{document}

Using \smash: enter image description here

Using \vphantom: enter image description here

  • Thanks! It is really funny to me that I did not consider using \vphantom as I actually fiddled with it to increase the border height of the example (but ended up using rule). I did not know border was an optional argument to standalone. Two things learned from one question! – Therkel Sep 13 '17 at 12:25
2

You can do that with \smash[t]{...}, or adding a pair of braces before the exponent:

\documentclass[preview, border = 5pt]{standalone}
\usepackage{amsmath,amssymb}

%%%%%%%%%%%%%%%%%%%%%%
%% Hendrik's answer %%
%%%%%%%%%%%%%%%%%%%%%%
\makeatletter
\let\save@mathaccent\mathaccent
\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{%
        %Enable nesting of accents:
        \let\mathaccent\save@mathaccent
        %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
%%%%%%%%%%%%%%%%%
%% End Hendrik %%
%%%%%%%%%%%%%%%%%

\begin{document}

$ \smash[t]{\widebar{\mathbb R}}^2\quad\widebar{\mathbb R}{}^2\quad
\widebar{R}{}^2 \quad \mathbb R^2\quad R^2 $

\end{document} 

enter image description here

1

Acc (for accented) atoms will be considered, as far as their height is concerned, equal to their base. The construction of the wide bar “by hand” does not make an Acc atom, so the whole box is used for determining the height.

If you're not in shortage of math families, I suggest using \widebar from mathabx (see https://tex.stackexchange.com/a/364929/4427 for the source and for another application).

\documentclass{article}
\usepackage{amsmath,amssymb}

\DeclareFontFamily{U}{mathx}{\hyphenchar\font45}
\DeclareFontShape{U}{mathx}{m}{n}{ <-> mathx10 }{}
\DeclareSymbolFont{mathx}{U}{mathx}{m}{n}
\DeclareFontSubstitution{U}{mathx}{m}{n}
\DeclareMathAccent{\widebar}{\mathalpha}{mathx}{"73}

\begin{document}

$
\bar{\mathbb{R}}^2
\quad
\mathbb{R}^2
\quad
\bar{R}^2
\quad
 R^2
$

$
\widebar{\mathbb{R}}^2
\quad
\mathbb{R}^2
\quad
\widebar{R}^2
\quad
 R^2
$

\end{document}

enter image description here

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.