4

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.

2
  • 1
    your question is not clear about whether you want the sub/superscripts aligned one above the other, or offset. since the article you cite doesn't really deal with that either, it would be better to know what you want in that respect than to guess. (the default result of the example you give is to "stack" the scripts.) Commented Jun 12, 2018 at 15:04
  • @barbarabeeton I apologize, I updated the question.
    – atapaka
    Commented Jun 12, 2018 at 15:43

3 Answers 3

6

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

5
  • Does the point size of \fontdimenXX have to be adjusted in the case of different font size, say \footnotesize?
    – atapaka
    Commented Jun 12, 2018 at 15:45
  • @leosenko Save and restore them, as shown in your referenced document (\tmp they used, but you would need several unique names). Commented Jun 12, 2018 at 15:46
  • @leosenko To handle a variety of font sizes, you could define the \fontdimens in terms of ex units rather than pts Commented Jun 12, 2018 at 15:50
  • @Stephen, would you help me out with saving those values? I am trying to reproduce what the document did but with:\documentclass[10pt]{article} \begin{document} \font\tensy=cmsy10 \newcount\tmp \tmp=\the\fontdimen14\tensy \centerline{$\theta^{At}$\quad {\rm versus}\quad $\fontdimen14\tensy=7pt \theta^{At}$} \fontdimen14\tensy=\tmp \end{document}, first the results has .62892pt for some reason when \tmp is assigned the value and then it complains Illegal unit of measure (pt inserted). \let \end{document} once it reaches the part \fontdimen14\tensy=\tmp.
    – atapaka
    Commented Jun 12, 2018 at 16:54
  • @leosenko See my edit. I also discovered that for the dimension to take hold under a change in font size, math mode has to be entered, which I did inside of a box: \setbox0=\hbox{\(\)} Commented Jun 12, 2018 at 17:04
7

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

4

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

1
  • 2
    This works if font size is changed, too. Nothing special to be done then.
    – user4686
    Commented Jun 12, 2018 at 17:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .