mathtools icon indicating copy to clipboard operation
mathtools copied to clipboard

Incorrect alignment with \prescript

Open quark67 opened this issue 3 years ago • 2 comments

The bug where described here by other user at 2013: https://tex.stackexchange.com/questions/109694/the-prescript-command-from-the-mathtools-package-gives-incorrect-alignment

The code (taken from the previous site):

\documentclass{article}
\usepackage{amsmath,mathtools}
\begin{document}
$\prescript{14}{2}{\mathbf{C}}^{5+}_{2}$
\end{document}

produce this (red line added to make the difference clearer):

But I found also a bug for the sup argument.

Here is a code:

\documentclass{article}
\usepackage{mathtools}
\begin{document}
$\prescript{14}{2}{\mathbf{C}}^{4+}_{2}$
\end{document}

The result (compare the left and right "4": at left, the "4" is slightly too high): image

More interesting: if we drop the "+", the sub part (the "2") are correctly aligned, but the left "4" is too hight (or the right "4" too low, but the position of the right part is not controlled by mathtools).

The code:

\documentclass{article}
\usepackage{mathtools}
\begin{document}
$\prescript{14}{2}{\mathbf{C}}^{4}_{2}$
\end{document}

The result: image

quark67 avatar Sep 03 '21 10:09 quark67

This would need some refinement, but it should give the idea

\documentclass{article}
\usepackage{amsmath}

\NewDocumentCommand{\prescript}{mmme{^_}}{%
  \vphantom{#3}% the base
  ^{\prescriptalign{{#1}{#4}{#2}{#5}}}% the superscript
  _{\prescriptalign{{#2}{#5}{#1}{#4}}}%  the subscript
  \kern-\scriptspace
  #3% the base
  ^{\IfValueT{#4}{#4}\vphantom{#1}}% the superscript
  _{\IfValueT{#5}{#5}\vphantom{#2}}% the subscript
}

\NewDocumentCommand{\prescriptalign}{m}{%
  \mathpalette\prescriptalignaux{#1}%
}
\NewDocumentCommand{\prescriptalignaux}{mm}{%
  \prescriptalignauxi{#1}#2%
}
\ExplSyntaxOn
\NewDocumentCommand{\prescriptalignauxi}{mmmmm}
 {
  \hbox_set:Nn \l_tmpa_box { \mathsurround0pt $#1#2\IfValueT{#3}{\vphantom{#3}}$ }
  \hbox_set:Nn \l_tmpb_box { \mathsurround0pt $#1#4\IfValueT{#5}{\vphantom{#5}}$ }
  \makebox
    [\dim_max:nn { \box_wd:N \l_tmpa_box } { \box_wd:N \l_tmpb_box } ]
    [r]
    { \box_use:N \l_tmpa_box }
 }
\ExplSyntaxOff

\def\baserule{\makebox[0pt]{\vrule height 0pt depth 0.1pt width 10cm}}

\begin{document}

\begin{gather*}
\prescript{14\baserule}{2\baserule}{\mathbf{C}}^{5+}_{2}
\\
\prescript{14\baserule}{2\baserule}{\mathbf{C}}^{5}_{2}
\\
\prescript{14\baserule}{2\baserule}{\mathbf{C}}_{2}
\\
\prescript{14\baserule}{2\baserule}{\mathbf{C}}^{5+}
\end{gather*}

\end{document}
image

eg9 avatar Sep 03 '21 12:09 eg9

But is it now considered save to start using latex3 syntax in mathtools? It would be nice, but we have no idea how many people are updating these by hand. I all for drawing a line at some point and declaring that from now on we expect users to use an kernel newer than X

daleif avatar Sep 06 '21 08:09 daleif