Vertical positioning of superscripts and subscripts in math mode

leosenko 06/12/2018. 3 answers, 188 views
math-mode positioning subscripts superscripts

The question can be found around but with no satisfactory answer. I want to position the subscript more down and superscript more up.

The code to test it on is simple:

$1.23_{-22.22}^{-1.23221}$

The major requirement, no phantom boxes and other ugly tricks should be used. The sub- and superscripts have to be written as suggested above. Further, I want to enable the "improved positioning" and then return to the standard positioning (I have a large table with the numbers as suggested above). There is a suggestion here. But the author does it only for subscripts and superscripts separately. For the case when both super and subscripts are used, he refers to the tex book appendix G, I tried to look at it, but I do not really have no idea how to connect the ideas, I am the latex user not developer. The mentioned text also provides a good example how I would like the super and sub scripts to look like. Also, I would like the superscript and subscript to be left aligned with each other if that is possible.

3 Answers


Steven B. Segletes 06/12/2018.

Your referenced article alters \fontdimen14 for superscripts and \fontdimen16 for subscripts.

You need to alter \fontdimen17 as well, for the case of simultaneous super and subscripts (note: \textfont2 works for display and text math styles only. For the script and scriptscript styles, use \scriptfont2 and \scriptscriptfont2, respectively).

Furthermore, if you wish to retain the higher superscript in cramped modes like under a radical, you need to also alter \fontdimen15.

\documentclass[10pt]{article}
\begin{document}
$1.23_{-22.22}^{-1.23221}$
$
\fontdimen14\textfont2=6pt
\fontdimen15\textfont2=6pt
\fontdimen16\textfont2=5pt
\fontdimen17\textfont2=5pt
1.23_{-22.22}^{-1.23221}
1.23_{-22.22}
1.23^{-1.23221}
\sqrt{1.23^{-1.23221}}$
\end{document}

enter image description here

To have these \fontdimens set temporarily, one should save the original values and restore them as desired.


Here is how it might be done, across font sizes, with switchback to original dimensions:

\documentclass{article}
\setbox0=\hbox{$%
\xdef\fdfourteen{\the\fontdimen14\textfont2}
\xdef\fdfifteen{\the\fontdimen15\textfont2}
\xdef\fdsixteen{\the\fontdimen16\textfont2}
\xdef\fdseventeen{\the\fontdimen17\textfont2}
$}
\newcommand\newss{\setbox0=\hbox{\(\)}%
\fontdimen14\textfont2=1.5ex
\fontdimen15\textfont2=1.5ex
\fontdimen16\textfont2=1.1ex
\fontdimen17\textfont2=1.1ex
}
\newcommand\origss{\setbox0=\hbox{\(\)}%
\fontdimen14\textfont2=\fdfourteen%
\fontdimen15\textfont2=\fdfifteen%
\fontdimen16\textfont2=\fdsixteen%
\fontdimen17\textfont2=\fdseventeen%
}
\parskip=1em% FOR THIS MWE ONLY
\def\test{$
  1.23_{-22.22}^{-1.23221}\quad
  1.23_{-22.22}\quad
  1.23^{-1.23221}\quad
  \sqrt{1.23^{-1.23221}}
$}
\begin{document}
\fdfourteen, \fdfifteen, \fdsixteen, \fdseventeen

\origss Orig: \test\par{\footnotesize Orig(footnotesize): \test}

\newss New: \test\par{\footnotesize\newss New(footnotesize): \test}

\origss Orig: \test\par{\footnotesize\origss Orig(footnotesize): \test}
\end{document}

enter image description here


egreg 06/12/2018.

Since you seem to also want normal position for superscripts and subscripts, it's necessary to mark somehow the places where you want the different positioning.

\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}

\NewDocumentCommand{\dn}{e{_^}}{%
  _{\IfValueT{#1}{#1}\vphantom{\smash[b]{|}}}
  ^{\IfValueT{#2}{#2}\vphantom{\smash[t]{\big|}}}
}

\begin{document}

\[
1.23\dn_{-22.22}^{-1.23221}
\qquad
1.23_{-22.22}^{-1.23221}
\]

\end{document}

Use a more meaningful name than \dn.

enter image description here


jfbu 06/12/2018.

A game with catcodes and mathcodes.

\documentclass{article}
\usepackage{amsmath}

\begingroup
\catcode`_ \active
\catcode`^ \active
\gdef\newactiveunderscore
    {\def_##1{\sb{##1\vphantom{\smash[b]{|}}}}}
% default definition is \_
\gdef\restoreoldactiveunderscore{\def_{\_}}
\gdef^#1{\sp{#1\vphantom{\smash[t]{\big|}}}}
% (the definitions contents copied from @egreg's \dn)
\endgroup

\newcommand{\modifiedsubsuper}{%
    % _ is already math active
    \newactiveunderscore % give it new definition
    \catcode`_ 12 % don't forget to remove its special catcode
    % store current mathcode of ^
    \ifnum\mathcode`^="8000 \else\edef\currentspmathcode{\the\mathcode`^}\fi
    \mathcode`^ "8000
    \catcode`^ 12 % don't forget to remove its special catcode
}%

\newcommand{\normalsubsuper}{%
    \restoreoldactiveunderscore
    \catcode`_ 8 % don't forget its special catcode
    \mathcode`^ \currentspmathcode
    \catcode`^ 7 % don't forget its special catcode
}%

\AtBeginDocument{\xdef\currentspmathcode{\the\mathcode`^}}

\begin{document}

\[
1.23_{-22.22}^{-1.23221}
\]
\[\modifiedsubsuper
1.23_{-22.22}^{-1.23221}
\]
\[
1.23_{-22.22}^{-1.23221}
\]
\modifiedsubsuper
\[
1.23_{-22.22}^{-1.23221}
\]
\normalsubsuper
\[
1.23_{-22.22}^{-1.23221}
\]
\end{document}

enter image description here

Related questions

Hot questions

Language

Popular Tags