# Wide equations have too much vertical space above them

It is the same question as How can I prevent wide equation having too large vertical space?, but the answers there don't address my case:

\documentclass[9pt]{beamer}
\usepackage[utf8]{inputenc}

\usefonttheme{serif}

\usepackage{tcolorbox}
\tcbuselibrary{magazine}

\tcbuselibrary{theorems}
\colorlet{greendark}{green!60!black}
\colorlet{bluelight}{blue!35!white}
\newtcbtheorem[number within=section]{mycomp}{Computation}%
{colframe=bluelight!60!greendark,enlarge top by=0.15cm,before
skip=3pt,after skip=6.5pt,fonttitle=\slshape,breakable}{comp}

\begin{document}

\begin{mycomp}{Mobius cancellation $G=\log \implies g = \frac 1t$}{mobiuscancellog}
$$\frac{1}{1/x} = x\sum_{d\leq x} \mu(d) \frac {-\log(\frac dx)}d + O\left(\sum_{d\leq x} \mu(d) (\tfrac{1}{d/x}+1))\right).$$
\end{mycomp}

\begin{mycomp}{Mobius cancellation $G=\log^2 \implies g = \frac {2\log t}{t}$}{mobiuscancellog2}
$$\frac{2 \log(\frac 1x)}{1/x} = x\sum_{d\leq x} \mu(d) \frac {-\log^2(\frac dx)}d + O\Big(\sum_{d\leq x} \mu(d) \left[\tfrac{\log(\frac dx)}{d/x}+1\right]\Big)$$
\end{mycomp}

\end{document}


Notice that the 2nd equation has more vertical space above the equation than below (compared distance from top of box to top of summation sign, and bottom of summation sign to bottom of box in both pictures).

I have determined that it must be solely based on width: by deleting enough of the 2nd equation, and it becomes shorter, it suddenly looks right again.

Can anyone suggest a fix?

• Off-topic: Never use $$ in LaTeX document. No way, no how. Please read the posting Why is $...$ preferable to $$ ... $$? for further information. – Mico Commented Mar 5 at 5:51 • It appears to be font-related. If you use a different font, the problem disappears. (At least, it looks OK to me.) This is true if you use the default Beamer fonts, but it is also true if you use professionalfonts with a custom font package, as long as it is not Computer Modern-based. So e.g. newtx doesn't show the problem, but lmodern does. If you put an inner frame around the equation, you get more space, but it is balanced. Similarly if you insert an empty box after. (I used $...$ for all tests. Using \centering\displaystyle ... also works, but obviously changes spacing. – cfr Commented Mar 5 at 6:02 • @cfr do you know anyway to make it display the bounding box? I'm interested in seeing if maybe the issue is because the inner bound of the tcolorbox is somehow intersecting with the bounding box of the equation and that's what's causing the issue – D.R Commented Mar 5 at 7:09 ## 2 Answers Vertical space before an equation depends on its length and the length of the previous line. The corresponding skips are \abovedisplayskip and \abovedisplayshortskip. In your case, setting \setlength\abovedisplayshortskip{\abovedisplayskip}  gives the same spacing for the two equations. The problems arises because you start a paragraph with a formula (first material in your boxes), and LaTeX doesn't support it. You can solve it with \noindent before your equations. \documentclass[9pt]{beamer} \usepackage[utf8]{inputenc} \usefonttheme{serif} \usepackage{tcolorbox} \tcbuselibrary{magazine} \tcbuselibrary{theorems} \colorlet{greendark}{green!60!black} \colorlet{bluelight}{blue!35!white} \newtcbtheorem[number within=section]{mycomp}{Computation}% {colframe=bluelight!60!greendark,enlarge top by=0.15cm,before skip=3pt,after skip=6.5pt,fonttitle=\slshape,breakable}{comp} \begin{document} \begin{mycomp}{Mobius cancellation G=\log \implies g = \frac 1t}{mobiuscancellog} \noindent $\frac{1}{1/x} = x\sum_{d\leq x} \mu(d) \frac {-\log(\frac dx)}d + O\left(\sum_{d\leq x} \mu(d) (\tfrac{1}{d/x}+1))\right).$ \end{mycomp} \begin{mycomp}{Mobius cancellation G=\log^2 \implies g = \frac {2\log t}{t}}{mobiuscancellog2} \noindent $\frac{2 \log(\frac 1x)}{1/x} = x\sum_{d\leq x} \mu(d) \frac {-\log^2(\frac dx)}d + O\Big(\sum_{d\leq x} \mu(d) \left[\tfrac{\log(\frac dx)}{d/x}+1\right]\Big)$ \end{mycomp} \end{document}  I replaced $$ with $...$ as suggested by Mico.

(La)TeX is not really at ease when a single equation out of context has to be displayed.

I suggest to define a specific environment for such cases, so you can force the “line before the display” (that doesn't really exist) to be short, by setting \predisplaysize=0pt.

\documentclass[9pt]{beamer}

\usefonttheme{serif}

\usepackage{tcolorbox}
\tcbuselibrary{magazine}
\tcbuselibrary{theorems}

\colorlet{greendark}{green!60!black}
\colorlet{bluelight}{blue!35!white}
\newtcbtheorem[number within=section]{mycomp}{Computation}{
colframe=bluelight!60!greendark,
enlarge top by=0.15cm,
before skip=3pt,
after skip=6.5pt,
fonttitle=\slshape,
breakable
}{comp}

\newenvironment{equation-}{$}{\predisplaysize=0pt$}

\begin{document}

\begin{mycomp}{Mobius cancellation $G=\log \implies g = \frac 1t$}{mobiuscancellog}
\begin{equation-}
\frac{1}{1/x} = x\sum_{d\leq x} \mu(d) \frac {-\log(\frac dx)}d
+ O\biggl(\,\sum_{d\leq x} \mu(d) (\tfrac{1}{d/x}+1))\biggr).
\end{equation-}
\end{mycomp}

\begin{mycomp}{Mobius cancellation $G=\log^2 \implies g = \frac {2\log t}{t}$}{mobiuscancellog2}
\begin{equation-}
\frac{2 \log(\frac 1x)}{1/x} = x\sum_{d\leq x} \mu(d) \frac {-\log^2(\frac dx)}d
+ O\biggl(\,\sum_{d\leq x} \mu(d) \Bigl[\tfrac{\log(\frac dx)}{d/x}+1\Bigr]\biggr)
\end{equation-}
\end{mycomp}

\end{document}


Note the small changes I made in order to avoid badly sized delimiters and clashes (they're not relevant as far as top and bottom spacing are concerned).

If you need to do an alignment as an out of context display, use aligned inside equation-.

• Is spacing always the same with \noindent before equation and \predisplaysize=0pt inside ? I mean, I read here: tex.stackexchange.com/a/107727/316068 that \predisplaysize has a minimum width of 2em. If the equation is almost as long as the line, is \abovedisplayskip used in place of \abovedisplayshortskip ? [I would likes to cite @egreg, but it doesn't work]
– jlab
Commented Mar 5 at 10:42
• @jlab The poster always gets informed of comments under their post, no need for @<name>. Anyway, the rules for deciding whether to use normal or short skips are quite complex. The minimum \predisplaysize automatically set by TeX is indeed 2em (in the symbol font, IIRC), but here we're setting it manually, so any value can be given. Commented Mar 5 at 11:25
• I did a quick test. With \predisplaysize=0pt, if the equation is too long \abovedisplayskip is used in place of \abovedisplayshortskip (it's not the case with \noindent before the equation). Another solution would be to set \abovedisplayskip=\abovedisplayshortskip.
– jlab
Commented Mar 5 at 11:41