5

Please look at the following code:

\documentclass{article}

\usepackage{amsmath}

\begin{document}

\Huge

\begin{equation}
\frac{x^{2}}{x^{2}}
\end{equation}

\begin{equation}
x^{2} / x^{2}
\end{equation}

\end{document}

Can you see the wrong placement of the exponent in the denominator? Compare it to the flat fraction.

output of example code

I do not really have an idea what to do to get the same spacing. Any suggestions?

Thanks.

9
  • Denominators use the “cramped” style, where exponents are lower than in the “uncramped” style used in the numerator. At \Huge size the effect is more evident than in \normalsize.
    – egreg
    Mar 11, 2017 at 18:39
  • 1
    potential duplicate: Superscripts appear in various weird places in fractions Mar 11, 2017 at 18:47
  • Is there a way to switch off the cramped style globaly? Mar 11, 2017 at 19:29
  • @user125730 - Cramped style for the denominator terms of fractions is "hard-wired", i.e., built into the algorithms of all TeX engines.
    – Mico
    Mar 11, 2017 at 20:19
  • 1
    @Mico: Thanks for your reply. It looks like I had understood exactly the opposite of what you meant. :-)
    – GuM
    Mar 13, 2017 at 18:58

3 Answers 3

7

Before reading on, please see this answer. What follows is about modifying TeX’s behavior so to obtain the desired output at the primitive level (in particular, not just for fractions, but for all occurences of a “cramped” style).

It is true what @Mico says in his comment, that is, that cramped style for denominators is a very low-level feature built into all TeX engines; but it is also true that it can be configured by means of the relevant \fontdimen parameters (so that I wouldn’t describe it as being “hard-wired”). The exact meaning of these parameters is defined in Appendix G of The TeXbook; in particular, what is relevant for this question is just the second sentence of Rule 18c (last two lines on p. 445), which dictates how TeX chooses the amount by which “ordinary” exponents (that is, not too deep) are to be raised when applied to “ordinary” base symbols (for example, single characters, as opposed to subformulas). It turns out that there are three possibilities:

  • in \displaystyle, the chosen amount is the value of the parameter that can be referred to as \fontdimen 13 \textfont 2;

  • in any “uncramped”, non-display style, the chosen amount is the value of \fontdimen 14 \<style>font 2

  • in any “cramped” (either display or non-display) style, the chosen amount is \fontdimen 15 \<style>font 2, except that <style> is substituted with text if it is display.

As it has already been pointed out by others, “cramped” styles are used for denominators of fractions; note, though, that they are used also in radicals, in \overline constructions, and still in other places.

As a consequence, it is possible to “disable”, virtually, the “cramped” styles simply by assigning to the 15th \fontdimen parameter the value of the 14th, thereby rising exponents in denominators by the same amount that TeX ordinarily uses for exponents in numerators; that is, exactly the opposite of what @Mico’s solution, which forces a “cramped” style in the numerators too, does.

However, there are some caveats.

  1. First of all, changes to \fontdimen parameters do not obey TeX grouping rules: their scope is always global (The TeXbook, p. 277). This means that, if you want to undo them, you need to save the original values “by hand”.

  2. In the second place, each \fontdimen change should be repeated thrice (once for each style, that is, for \textfont 2, \scriptfont 2, and \scriptscript 2), unless you want to disable “crampedness” only for some styles but not for others: for example, in order to disable “crampedness” only for \textstyle and \scriptstyle, but not for \scriptscriptstyle, you would change \fontdimen 15 \textfont 2 and \fontdimen 15 \scriptfont 2, but not \fontdimen 15 \scriptscriptfont 2.

  3. This triple setting, in turn, must be repeated for all the sizes (\normalsize, \footnotesize, …) at which you are typesetting formulas, since, in general, the font that corresponds to, say, \textfont 2 in \normalsize is not the same one that corresponds to \textfont 2 in \footnotesize.

  4. If you decide to suppress “crampedness”, it seems wise to suppress “displayedness” too, that is, to give the same value as \fontdimen 14 to \fontdimen 13 as well (see above).

The following code gives a sort of “proof of concept”. It defines two declarations, both of which are irreversible:

  • \LeveledExponentsForCurrentSize suppresses both “crampedness” and “displayedness” for the current font size only (but all the way to \scriptscriptstyle);

  • \LeveledExponentsForAllSizessuppresses both “crampedness” and “displayedness” for all the standard LaTeX sizes; note, however, that this will load a large number of math fonts, most of which you are never going to use, and which might cause you to hit the limit for the number of loadable fonts in certain engines (mainly, in pdfTeX).

As already hinted, the code makes no provision for saving the original values, and hence for reverting to the original setting. This isn’t difficult to accomplish, however.

% My standard header for TeX.SX answers:
\documentclass[a4paper]{article} % To avoid confusion, let us explicitly 
                                 % declare the paper format.

\usepackage[T1]{fontenc}         % Not always necessary, but recommended.
% End of standard header.  What follows pertains to the problem at hand.

\makeatletter

\newcommand*\@level@exponents@for@font[1]{%
    \fontdimen 13 #1\tw@ \fontdimen 14 #1\tw@
    \fontdimen 15 #1\tw@ \fontdimen 14 #1\tw@
}
\newcommand*\@level@exponents@for@size[1]{%
    \begingroup
        #1% set font size
        \setbox\z@ \hbox{$$}% activate math font for that size
        \@level@exponents@for@font \textfont
        \@level@exponents@for@font \scriptfont
        \@level@exponents@for@font \scriptscriptfont
    \endgroup
}
\newcommand*\LeveledExponentsForCurrentSize{%
    \@level@exponents@for@size \@currsize
}
\newcommand*\LeveledExponentsForAllSizes{%
    \@level@exponents@for@size \tiny
    \@level@exponents@for@size \scriptsize
    \@level@exponents@for@size \footnotesize
    \@level@exponents@for@size \small
    \@level@exponents@for@size \normalsize
    \@level@exponents@for@size \large
    \@level@exponents@for@size \Large
    \@level@exponents@for@size \LARGE
    \@level@exponents@for@size \huge
    \@level@exponents@for@size \Huge
}



\begin{document}

\Huge

Compare $x^{2}$ with $\overline{x^{2}}$.
In display, three placements are possible:
\[x^{2}|{\textstyle x^{2}}|\overline{x^{2}}\]

As @Mico suggests, let's show fractions too, both in display, as in
\[\frac{x^{2}}{x^{2}}\ne x^{2},\]
and inline: \( \frac{x^{2}}{x^{2}}\ne x^{2} \).

\LeveledExponentsForCurrentSize

Now compare them again: \( x^{2}|\overline{x^{2}} \).
Now in display:
\[x^{2}|{\textstyle x^{2}}|\overline{x^{2}}\]
And the fractions, displayed
\[\frac{x^{2}}{x^{2}}\ne x^{2},\]
and inline: \( \frac{x^{2}}{x^{2}}\ne x^{2} \).

\end{document}

For your convenience, here is the resulting output:

Output of the above code

3
  • +1. Thanks for pointing out that it's possible to change from cramped to uncramped style in denominators and hence that the default setting isn't truly hardwired (as I had incorrectly claimed in a comment). A minor suggestions: In order to address the OP's concern more directly, it might be useful to show how $\frac{x^2}{x^2}$ looks like before and after executing \LeveledExponentsForCurrentSize, with \frac occurring in both display and text style.
    – Mico
    Mar 12, 2017 at 8:21
  • @Gustavo Mezzetti: I am really impressed and your solution works fine but not for LuaLaTeX... Is ther a possibility to make it work in LuaLaTeX? Mar 12, 2017 at 11:34
  • @user125730: I’m sorry, but I can barely say that I have finally (almost) completed the study of the original TeX engine by Knuth—well, no, also with e-TeX extensions. With pdfTeX I’ve pretty worked my way up to an acceptable level of knowledge, and only recently I began tackling XeTeX; but a serious study of LuaTeX is still beyond the horizon of my foreseeable “to-do” list… ;-) However, Mico’s answer to the question that Barbara Beeton cites contains some hints for LuaTeX.
    – GuM
    Mar 13, 2017 at 17:48
6

To force the so-called "cramped" style on exponents in both the numerator and the denominator, use the \cramped macro -- which is provided by the mathtools package -- on the term(s) in the numerator. ("Cramped style is in force automatically for the denominator.)

enter image description here

\documentclass{article}
\usepackage{mathtools} % for "\cramped" macro
\begin{document}
$\displaystyle\frac{x^2+\cramped{x^2}}{x^2} $
\end{document}
3

my preference is to choose a method that has no side effects elsewhere. that eliminates the method described (most excellently!) by gustavo mezzetti. and since forcing the "cramped" style is not what was asked for (and would have the side effect that if the modified object is near a variable that has both sub- and superscript), the alignment would not be consistent.

a method that operates on only the position of the superscript is to insert a phantom subscript. this pushes up the superscript to almost the level of the superscript on a normal, in-line variable. this can be made into a command, as it is here, for convenience. the phantom object should have a good height and no depth; a digit or capital "X" is usually a good choice.

\documentclass{article}
\newcommand{\matchsupheight}{_{\vphantom{0}}}
\begin{document}

\begin{equation}
x^{2} / x^{2}
\end{equation}

\begin{equation}
\frac{x^{2}}{x^{2}} \quad \frac{x^{2}}{x\matchsupheight^{2}}
\end{equation}

\end{document}

output of example code

You must log in to answer this question.

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