# Make cramped style ignore non-sub- and superscripts

The mathtools package provides the wonderful \cramped command to force a cramped style for sub- and superscripts. Maybe it's a bad habit, but I tend to wrap entire inline formulas in that command when I recognize that exponents disturb the line spacing.

However, I just had to notice that this seems to affect the spacing of normal symbols in formulas and, in particular, might have highly undesired effects on line wrapping. In the example below, LaTeX allows to compress just enough to fit the uncramped content in a box of slightly less than the natural width. However, if the entire formula is wrapped in \cramped{}, it seems to have an impact on the compressability of the space between the symbols. Fitting the formula in the same space is no longer possible, despite the fact that the glyphs themselves are identical, AFAICS.

Is there any way to force \cramped to only affect sub- and superscripts and leave everything else alone?

\documentclass{article}
\usepackage{mathtools}
\pagestyle{empty}

\begin{document}
\setlength{\parskip}{1em}
\settowidth{\dimen0}{Test: $1 - 2^{-(\lambda-1)}$.}
\setlength{\dimen1}{0.985\dimen0}

nothing cramped:\\
\framebox{\parbox{\dimen0}{Test: $1 - 2^{-(\lambda-1)}$.}}
\framebox{\parbox{\dimen1}{Test: $1 - 2^{-(\lambda-1)}$.}}

everything cramped:\\
\framebox{\parbox{\dimen0}{Test: $\cramped{1 - 2^{-(\lambda-1)}}$.}}
\framebox{\parbox{\dimen1}{Test: $\cramped{1 - 2^{-(\lambda-1)}}$.}}

only second term cramped:\\
\framebox{\parbox{\dimen0}{Test: $1 - \cramped{2^{-(\lambda-1)}}$.}}
\framebox{\parbox{\dimen1}{Test: $1 - \cramped{2^{-(\lambda-1)}}$.}}

\settowidth{\dimen0}{Test: $1 - 2$.}
\setlength{\dimen1}{0.985\dimen0}

without su[b,p]scripts, nothing cramped:\\
\framebox{\parbox{\dimen0}{Test: $1 - 2$.}}
\framebox{\parbox{\dimen1}{Test: $1 - 2$.}}

without su[b,p]scripts, everything cramped:\\
\framebox{\parbox{\dimen0}{Test: $\cramped{1 - 2}$.}}
\framebox{\parbox{\dimen1}{Test: $\cramped{1 - 2}$.}}

\end{document}


• \cramped makes a subformula, so spaces are frozen Apr 19 '16 at 9:44
• Thanks. That explains the observation. I guess there is no way to "un-freeze" the spaces again? And since the mathtools doc states that "unfortunately there are no primitive commands that allows [sic!] you to detect crampedness or switch to it", that basically means that a) unconditionally wrapping everything in \cramped{} is not a good idea, and b) no matter what I do, at least the spacing in longish exponents will be fixed when they are set as cramped? Apr 19 '16 at 9:55

The argument to \cramped is typeset as a subformula, so spaces inside it are frozen; it's the same that happens with \left and \right.

Thus the spaces around the minus sign in \cramped{1-2} are not flexible any more and the result is guaranteed to look wrong. For the same reason, \left and \right should never be used in inline math.

Of course you get rigid spaces also in $\sqrt{a-1}$, but this is less of a concern because the radical isolates the subformula from the context. It is the same as \cramped, because this trick uses \radical0 for doing its business.

You are overthinking, in my opinion. Look at the following example, where \lineskip is made much bigger than the usual 1pt just to clearly see whether it comes into action. You can see it's used only in case 5 (where the dreaded \left and \right appear). Compare closely numbers 1 and 2; in the former \cramped is used, the latter has normal text style: I see no improvement (actually I think number 1 is worse than number 2). Lines have both ascenders and descenders.

\documentclass{article}
\usepackage{mathtools}

\begin{document}

\lineskip=20pt % for better seeing if it's used

1.\ Some silly text some silly text some silly text some silly text
some silly text some silly text some silly text some silly text
some silly text some silly text some silly text some silly text
$\cramped{2^{-(\lambda-1)}}$
some silly text some silly text some silly text some silly text
some silly text some silly text some silly text some silly text
some silly text some silly text some silly text some silly text

2.\ Some silly text some silly text some silly text some silly text
some silly text some silly text some silly text some silly text
some silly text some silly text some silly text some silly text
$2^{-(\lambda-1)}$
some silly text some silly text some silly text some silly text
some silly text some silly text some silly text some silly text
some silly text some silly text some silly text some silly text

3.\ Some silly text some silly text some silly text some silly text
some silly text some silly text some silly text some silly text
some silly text some silly text some silly text some silly text
$2^{-(\lambda^2-1)}$
some silly text some silly text some silly text some silly text
some silly text some silly text some silly text some silly text
some silly text some silly text some silly text some silly text

4.\ Some silly text some silly text some silly text some silly text
some silly text some silly text some silly text some silly text
some silly text some silly text some silly text some silly text
$2^{-(\sum_i\lambda_i^2-1)}$
some silly text some silly text some silly text some silly text
some silly text some silly text some silly text some silly text
some silly text some silly text some silly text some silly text

5.\ Some silly text some silly text some silly text some silly text
some silly text some silly text some silly text some silly text
some silly text some silly text some silly text some silly text
$2^{-\left(\sum_i\lambda_i^2-1\right)}$
some silly text some silly text some silly text some silly text
some silly text some silly text some silly text some silly text
some silly text some silly text some silly text some silly text

\end{document}


I'm being lucky, to be honest: if in number 4 I change the “some” above the formula into “sygge” just to have consecutive descenders, I get

which one could disagree with. If I add \cramped, the picture becomes

I'd worry about this only in the final revision of the document, applying \cramped only where really needed because of clashes like the one above.

If you're willing to switch to LuaLaTeX, you can define a better \cramped macro that doesn't freeze spaces, by using math style changes.

\documentclass{article}
\usepackage{amsmath}

\newcommand{\changemathstyle}[1]{%
\ifcase#1\relax
\displaystyle\or
\crampeddisplaystyle\or
\textstyle\or
\crampedtextstyle\or
\scriptstyle\or
\crampedscriptstyle\or
\scriptscriptstyle\or
\crampedscriptscriptstyle
\fi
}

\newcommand\cramped[1]{%
\ifodd\mathstyle\relax
% we're already in a cramped style
#1%
\else
\edef\currentstyle{\mathstyle}%
\changemathstyle{\numexpr\mathstyle+1}%
#1%
\changemathstyle{\currentstyle}%
\fi
}

\begin{document}

\setlength{\parskip}{1em}
\settowidth{\dimen0}{Test: $1 - 2^{-(\lambda-1)}$.}
\setlength{\dimen1}{0.985\dimen0}

nothing cramped:\\
\framebox{\parbox{\dimen0}{Test: $1 - 2^{-(\lambda-1)}$.}}
\framebox{\parbox{\dimen1}{Test: $1 - 2^{-(\lambda-1)}$.}}

everything cramped:\\
\framebox{\parbox{\dimen0}{Test: $\cramped{1 - 2^{-(\lambda-1)}}$.}}
\framebox{\parbox{\dimen1}{Test: $\cramped{1 - 2^{-(\lambda-1)}}$.}}

only second term cramped:\\
\framebox{\parbox{\dimen0}{Test: $1 - \cramped{2^{-(\lambda-1)}}$.}}
\framebox{\parbox{\dimen1}{Test: $1 - \cramped{2^{-(\lambda-1)}}$.}}

\settowidth{\dimen0}{Test: $1 - 2$.}
\setlength{\dimen1}{0.985\dimen0}

without su[b,p]scripts, nothing cramped:\\
\framebox{\parbox{\dimen0}{Test: $1 - 2$.}}
\framebox{\parbox{\dimen1}{Test: $1 - 2$.}}

without su[b,p]scripts, everything cramped:\\
\framebox{\parbox{\dimen0}{Test: $\cramped{1 - 2}$.}}
\framebox{\parbox{\dimen1}{Test: $\cramped{1 - 2}$.}}

\end{document}


• Thanks, in particular for mentioning that the same issue occurs with \left and \right; I wasn't aware of that. You are probably right that I'm giving the issue too much attention; however, more often than not I'm not in charge of the final layout, because I have to deal with ancient, ugly styles. Notable mention: sig-alternate.cls, where already a simple exponent leads to increased line spacing (the formula above is actually a real-world example where I need cramped style, and the line and page break situation was so awkward that I had to rephrase an entire paragraph). Apr 19 '16 at 10:39
• @akobel You have my sympathy, if you really have to cope with sig-alternate. 😉 Apr 19 '16 at 10:52
• There's a missing % at end of line, after #1 :) Apr 19 '16 at 11:50
• @Manuel I should say it's irrelevant, because the macro is to be used in math mode. ;-) But coding style should not be influenced by such considerations. Apr 19 '16 at 11:51