# Adjust vertical spacing for any combination of text, equations and custom environments

Edit I completely rewrote the question thanks to @pluton's comment.

I have a couples of documents which have to fit on two pages. Some of them are short, so nothing needs to be done. But for some others, I need to squeeze vertical spacing.

To do so, I created a variable \mylength which I manually adjust for each document. I would like \mylength to be the spacing above and below any environment, particularly equation and the custom environments defi and theo.

Following @pluton comment, I integrated \addvspace. I still have some problems.

This is the result for \mylength equals to 0pt. It is meant to check the minimal spacing. The output is almost fine, except:

• the vertical space surrounding the equation which, for some reason I don't understand, behaves as if there were text above and below (see above equation (2), above (4), below (5) [I am aware that two equations should not written as so], above (6).

With \mylength equals to 0pt:

Now, if I change \mylength from 0pt to 8pt, the result has several problems:

• with no suprise, the space around equation is still wrong
• the spacing between two defi is wrong
• theo is not centered when surrounded by defi (see Theorem 4)

With \mylength equals to 8pt:

Code:

\documentclass[9pt,a4paper]{article}
\usepackage[table]{xcolor}
\usepackage{graphicx}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{environ}
\usepackage{tikz}
\usepackage{framed}
\usepackage{amsmath,amsfonts,amssymb}

%%%%%%%%% ENVIRONMENTS %%%%%%%%%%

\newenvironment{mycenter}% to control space after and before environment

\NewEnviron{theo}[1]{%

\begin{mycenter}
\begin{tikzpicture}
\node[rectangle, rounded corners, inner sep=5pt,minimum width=\textwidth,
text=black, text opacity=1,
draw=orange, ultra thick, draw opacity=1,
fill=orange, fill opacity=.3]
(box){%
\begin{minipage}{.95\textwidth}
\end{minipage}};
\end{tikzpicture}
\end{mycenter}
}

\usepackage[strict]{changepage}

% for definitions
\newenvironment{defi}{%
\setlength{\topsep}{0pt}
\def\FrameCommand{%
\hspace{1pt}%
{\color{black!70}\vrule width 2pt}%
}%
\noindent\hspace{-4.55pt}% disable indenting first paragraph
% \vspace{-8pt}
\vspace{1.5pt}\vspace{2pt}%
}
{%
\vspace{2pt}
\vspace{.5mm}
}

\begin{document}\thispagestyle{empty}
\def\mylength{0pt}
\setlength{\belowdisplayskip}{\mylength} \setlength{\belowdisplayshortskip}{\mylength}
\setlength{\abovedisplayskip}{\mylength} \setlength{\abovedisplayshortskip}{\mylength}

\paragraph{Environment theo}  First, let's check this environment.

\begin{theo}{Theorem} If blah blah blah then
$$1+1=2$$
\end{theo}
Everything's fine here.
\begin{theo}{Theorem} If blah blah blah then nothing.
\end{theo}%
\begin{theo}{Theorem} If blah blah blah then nothing.
\end{theo}%
Everything's fine here.
\begin{theo}{Theorem} If blah blah blah then nothing.
\end{theo}%
$$1-1=0$$
\textcolor{red}{\bf Why is the distance between the above equation and the theorem so large?}

\paragraph{Environment defi}  Now, let's check this second environment.
\begin{defi}Below, it's OK for \verb?0pt? and \textcolor{red}{too large} for \verb?8pt?.\end{defi}
\begin{defi}Nothing special.\end{defi}
Spacing is OK for 0pt but not centered (vertically) as between the two first theorems for 10pt.\begin{defi}Here, it's OK.\end{defi}
\begin{theo}{Theorem 2} If blah blah blah then
$$1+1=2$$
\end{theo}
$$2+3=5 \text{ when two environments are following eachother, the vertical spacing adds up.}$$
$$1+1=2$$%
%qsdfqsdf
\begin{defi}\textcolor{red}{\bf Why is the distance between the above equation and the present, and previous definitions, so large?}
\end{defi}
\begin{theo}{Theorem 4} \textcolor{red}{Theorem is not centered between two definitions for 8pt.}
\end{theo}%
\begin{defi}\textcolor{red}{Space too large below for 10pt.}\end{defi}
$$1+1=2%$$%
%sdfqssddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
\begin{defi}Kombucha is a bloh bloh bloh.\end{defi}
\end{document}

• And if, for some reason, you think doing so is a bad practice, feel free to share the reasons. – anderstood Jun 7 '15 at 2:55
• It would be better if you use a stretchable size for \def\mylength{10pt}. 10pt is constant. – Sigur Jun 7 '15 at 5:24
• you might want to use the \addvspace command: forkosh.com/latex/ltx-198.html – pluton Jun 7 '15 at 5:56
• @pluton your comment looks like an answer to me... – erreka Jun 8 '15 at 19:01
• @Sigur: why would it be better? Having a single parameter to tune is fine for me. I recall my documents have to fit on two pages each. – anderstood Jun 9 '15 at 4:00

• Redefining the environment mycenter which was causing some trouble, with:

\newenvironment{mycenter}%

• Redefining the custom environment defi using tikz as for the theo environment, so that I could understand what the code,

• Changing \mylength to an extendable length as suggested by Sigur and elford with \mylength{8pt plus 4pt minus 6pt},
• And using \addvspace to avoid adding a vertical space twice,

I was able to produce this, which fit my needs:

Complete code:

\documentclass[9pt,a4paper]{article}
\usepackage[table]{xcolor}
\usepackage{graphicx}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{environ}
\usepackage{tikz}
\usepackage{framed}
\usepackage{amsmath,amsfonts,amssymb}

%%%%%%%%% ENVIRONMENTS %%%%%%%%%%

\newenvironment{mycenter}% to control space after and before environment

\NewEnviron{theo}[1]{%

\begin{mycenter}
\begin{tikzpicture}
\node[rectangle, rounded corners, inner sep=5pt,minimum width=\textwidth,
text=black, text opacity=1,
draw=orange, ultra thick, draw opacity=1,
fill=orange, fill opacity=.3]
(box){%
\begin{minipage}{.95\textwidth}
\end{minipage}};
\end{tikzpicture}
\end{mycenter}
}

\NewEnviron{defi}{%
\begin{mycenter}
\begin{tikzpicture}
\node[rectangle, inner sep=5pt,minimum width=\textwidth,
text=black, text opacity=1,
draw=none, ultra thick, draw opacity=1,
line width=0pt,
append after command={
(\tikzlastnode.north west) edge [line width=.8mm,color=black!70]
(\tikzlastnode.south west)}]
(box){%
\begin{minipage}{.95\textwidth}
\end{minipage}};
\end{tikzpicture}
\end{mycenter}
}

\begin{document}\thispagestyle{empty}
\def\mylength{8pt plus 4pt minus 6pt}
\setlength{\belowdisplayskip}{\mylength} \setlength{\belowdisplayshortskip}{\mylength}
\setlength{\abovedisplayskip}{\mylength} \setlength{\abovedisplayshortskip}{\mylength}

First, let's check this environment.

\begin{theo}{Theorem} If blah blah blah then
$$1+1=2$$
\end{theo}
Everything's fine here.
\begin{theo}{Theorem} If blah blah blah then nothing.
\end{theo}%
\begin{theo}{Theorem} If blah blah blah then nothing.
\end{theo}%
Everything's fine here.
\begin{theo}{Theorem} If blah blah blah then nothing.
\end{theo}%
$$1-1=0$$
text text text

Now, let's check this second environment.
\begin{defi}A fool is a pool with an \textit{f}.\end{defi}
\begin{defi}Nothing special.\end{defi}
I can write whatever I want, who cares.
\begin{defi}You need to learn all definitions.\end{defi}
\begin{theo}{Theorem 2} If blah blah blah then
$$1+1=2$$
\end{theo}
$$\cos \pi=-1$$
$$1+1=2$$%
$$1+1=2%$$%

• afaik, to specify stretchable space it is only necessary to state it in the appropriate place. So here you simply need to state \mylength{2pt plus 4pt minus 6pt}, or something like that; the \setlength macro will assign it to the below or above display skip parameters. – erreka Jun 10 '15 at 23:44