Vertical positioning of superscripts and subscripts in math mode

leosenko 06/12/2018. 3 answers, 189 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


HighResolutionMusic.com - Download Hi-Res Songs

1 Alan Walker

Diamond Heart flac

Alan Walker. 2018. Writer: Alan Walker;Sophia Somajo;Mood Melodies;James Njie;Thomas Troelsen;Kristoffer Haugan;Edvard Normann;Anders Froen;Gunnar Greve;Yann Bargain;Victor Verpillat;Fredrik Borch Olsen.
2 Sia

I'm Still Here flac

Sia. 2018. Writer: Sia.
3 Little Mix

Woman Like Me flac

Little Mix. 2018. Writer: Nicki Minaj;Steve Mac;Ed Sheeran;Jess Glynne.
4 Cardi B

Taki Taki flac

Cardi B. 2018. Writer: Bava;Juan Vasquez;Vicente Saavedra;Jordan Thorpe;DJ Snake;Ozuna;Cardi B;Selena Gomez.
5 Eminem

Venom flac

Eminem. 2018. Writer: Eminem.
6 Halsey

Without Me flac

Halsey. 2018. Writer: Halsey;Delacey;Louis Bell;Amy Allen;Justin Timberlake;Timbaland;Scott Storch.
7 Lady Gaga

I'll Never Love Again flac

Lady Gaga. 2018. Writer: Benjamin Rice;Lady Gaga.
8 Kelsea Ballerini

This Feeling flac

Kelsea Ballerini. 2018. Writer: Andrew Taggart;Alex Pall;Emily Warren.
9 Bradley Cooper

Shallow flac

Bradley Cooper. 2018. Writer: Andrew Wyatt;Anthony Rossomando;Mark Ronson;Lady Gaga.
10 Dewain Whitmore

Burn Out flac

Dewain Whitmore. 2018. Writer: Dewain Whitmore;Ilsey Juber;Emilio Behr;Martijn Garritsen.
11 Mako

Rise flac

Mako. 2018. Writer: Riot Music Team;Mako;Justin Tranter.
12 Bradley Cooper

Always Remember Us This Way flac

Bradley Cooper. 2018. Writer: Lady Gaga;Dave Cobb.
13 Lukas Graham

Love Someone flac

Lukas Graham. 2018. Writer: Don Stefano;Morten "Rissi" Ristorp;Morten "Pilo" Pilegaard;Jaramye Daniels;James Alan;David LaBrel;Lukas Forchhammer Graham.
14 Avril Lavigne

Head Above Water flac

Avril Lavigne. 2018. Writer: Stephan Moccio;Travis Clark;Avril Lavigne.
15 Deep Chills

Run Free flac

Deep Chills. 2018.
16 Khalid

Better flac

Khalid. 2018. Writer: Charlie Handsome;Jamil Chammas;Denis Kosiak;Tor Erik Hermansen;Mikkel Stoleer Eriksen;Khalid.
17 Charli XCX

1999 flac

Charli XCX. 2018. Writer: Charli XCX;Troye Sivan;Leland;Oscar Holter;Noonie Bao.
18 NCT 127

Regular (English Version) flac

NCT 127. 2018.
19 Camila Cabello

Consequences (Orchestra) flac

Camila Cabello. 2018. Writer: Emily Lynn Weisband;Nicolle Galyon;Amy Wadge;Bart Schoudel;Frank Dukes;Camila Cabello.
20 Lady Gaga

Look What I Found flac

Lady Gaga. 2018. Writer: DJ White Shadow;Nick Monson;Mark Nilan Jr;Lady Gaga.

Related questions

Hot questions

Language

Popular Tags