TransWikia.com

How to make a superscript of arbitrary width?

TeX - LaTeX Asked on August 16, 2021

I’m trying to make the following two commands of the same width:

newcommand{Bsh}{ensuremath{B^{H}_{s}}xspace}
newcommand{Bsl}{ensuremath{B^{L}_{s}}xspace}

in order to avoid these ugly alignment issues:

enter image description here

I have tried without any success to use a combination of smash, llap, rlap, clap, phantom etc.
It seems that smash does not work as intended in superscripts.

Can someone help me? Thanks.

3 Answers

This might do. I allocate the same left-aligned width for each of the superscripts.

ORIGINAL Answer:

Good if one does not need the script styles.

documentclass{article}
usepackage{mathtools,xspace,eulervm}
newcommand{Bsh}{ensuremath{B^{mathmakebox[.6em][l]{H}}_{s}}xspace}
newcommand{Bsl}{ensuremath{B^{mathmakebox[.6em][l]{L}}_{s}}xspace}
begin{document}
begin{align*}
  vertBshrangle &= 0
  vertBslrangle &= 1
end{align*}
end{document}

enter image description here

REVISED ANSWER:

EDITED to better handle scalable space in smaller math styles.

documentclass{article}
usepackage{mathtools,xspace,eulervm,scalerel}
newcommand{Bsh}{ensuremath{B^{ThisStyle{%
  mathmakebox[2.05LMex][l]{H}}}_{s}}xspace}
newcommand{Bsl}{ensuremath{B^{ThisStyle{%
  mathmakebox[2.05LMex][l]{L}}}_{s}}xspace}
begin{document}
begin{align*}
scriptscriptstyle
  vertBshrangle~
scriptstyle
  vertBshrangle~
textstyle
  vertBshrangle &= 0
scriptscriptstyle
  vertBslrangle~
scriptstyle
  vertBslrangle~
textstyle
  vertBslrangle &= 1
end{align*}
end{document}

enter image description here

Correct answer by Steven B. Segletes on August 16, 2021

Similar to Steven's answer but with a slightly different approach: I measure the width of H in the current math style and set the L in a box with the same width

documentclass{article}

usepackage{mathtools}
DeclarePairedDelimiter{ket}{vert}{rangle}

newcommand*{Bsh}{B^{H}_{s}}

makeatletter
newcommand*{Bsl}{B_s^{mathpalette@Bslrelax}}
newcommand*{@Bsl}[2]{mathmakebox[widthof{m@th$#1H$}][l]{L}}
makeatother

usepackage{array} % for the test

begin{document}

begin{tabular}{ >{$}l<{$} >{$scriptstyle}l<{$} >{$scriptscriptstyle}l<{$} }
ket{Bsl} & ket{Bsl} & ket{Bsl} 
ket{Bsh} & ket{Bsh} & ket{Bsh} 
end{tabular}

end{document}

enter image description here

(I'm a fierce opponent of ensuremath and xspace so I'll let you fill them in :-).)

Answered by campa on August 16, 2021

Let TeX do the box-measuring for you with the help of eqmakebox[<tag>][<align>]{<stuff>} (from eqparbox). Different elements/<stuff> with the same <tag> are set in a (text) box with maximum width. You can manually adjust the <align>ment on a per-box case, with the default being centred.

enter image description here

documentclass{article}

usepackage{eqparbox,amsmath}

newcommand{Bsh}{ensuremath{B^{H}_{s}}}
newcommand{Bsl}{ensuremath{B^{L}_{s}}}

begin{document}

begin{align*}
                 lvert Bsh rangle &= x 
                 lvert Bsl rangle &= y 
  lvert eqmakebox[B]{Bsh} rangle &= x 
  lvert eqmakebox[B]{Bsl} rangle &= y
end{align*}

end{document}

This requires two compilations with any change in the maximum width associated with any specific <tag>.

Answered by Werner on August 16, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP