4

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:

with 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: enter image description here

Code:

\documentclass[9pt,a4paper]{article}
\usepackage[table]{xcolor}
\usepackage{graphicx}
\usepackage[margin=1.cm,top=1.2cm,bottom=1.3cm,headsep=14pt]{geometry}
\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
  {\addvspace{\mylength}\centering}% \begin{mycenter}[<len>]
  {\par\addvspace{\mylength}}% \end{mycenter}

\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}
      \textbf{#1}\qquad\BODY
  \end{minipage}};
\end{tikzpicture}
\end{mycenter}
}

% for adjustwidth environment
\usepackage[strict]{changepage}

% for definitions
\definecolor{defishade}{rgb}{0.97,0.97,.99}
\newenvironment{defi}{%
\vspace{-1.2mm} % UGLY MANUAL ADJUSTEMENT
\par\addvspace{\mylength}
\setlength{\topsep}{0pt}
  \def\FrameCommand{%
    \hspace{1pt}%
    {\color{black!70}\vrule width 2pt}%
    {\color{defishade}\vrule width 4pt}%
    \colorbox{defishade}%
  }%
  \MakeFramed{\advance\hsize-\width\FrameRestore}%
  \noindent\hspace{-4.55pt}% disable indenting first paragraph
    % \vspace{-8pt}  
  \begin{adjustwidth}{0pt}{7pt}%
  \vspace{1.5pt}\vspace{2pt}%
 \textcolor{black!70}{ \textbf{Definition}\qquad}
}
{%
  \vspace{2pt}
  \end{adjustwidth}\endMakeFramed%
  \vspace{.5mm}
  \addvspace{\mylength}
}




\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
\begin{equation}
1+1=2
\end{equation}
\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}%
\begin{equation}1-1=0\end{equation}
\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
\begin{equation}
1+1=2
\end{equation}
\end{theo}
\begin{equation}
2+3=5 \text{ when two environments are following eachother, the vertical spacing adds up.}  
\end{equation}
\begin{equation}
1+1=2
\end{equation}%
%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}
\begin{equation}
1+1=2%
\end{equation}%
%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
  • 3
    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
2
  • Redefining the environment mycenter which was causing some trouble, with:

    \newenvironment{mycenter}%
    {\parskip=0pt\par\addvspace{\mylength}\centering}%
    {\par\addvspace{\mylength}\noindent\ignorespacesafterend}%
    
  • 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:

enter image description here

Complete code:

\documentclass[9pt,a4paper]{article}
\usepackage[table]{xcolor}
\usepackage{graphicx}
\usepackage[margin=1.cm,top=1.2cm,bottom=1.3cm,headsep=14pt]{geometry}
\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
  {\parskip=0pt\par\addvspace{\mylength}\centering  }% \begin{mycenter}[<len>]
  {\par\addvspace{\mylength}\noindent\ignorespacesafterend}% \end{mycenter}

\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}
      \textbf{#1}\qquad\BODY
  \end{minipage}};
\end{tikzpicture}
\end{mycenter}
}

\definecolor{defishade}{rgb}{0.97,0.97,.99}
\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,
    fill=defishade, fill 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}
      \textcolor{black!70}{\textbf{Définition}}\qquad\BODY
  \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
\begin{equation}
1+1=2
\end{equation}
\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}%
\begin{equation}1-1=0\end{equation}
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
\begin{equation}
1+1=2
\end{equation}
\end{theo}
\begin{equation}
\cos \pi=-1
\end{equation}
\begin{equation}
1+1=2
\end{equation}%
\begin{defi} \LaTeX\  is your friend.
\end{defi}
\begin{theo}{Theorem 4} Let's all love each other.
\end{theo}%
\begin{defi}Someone nasty is not nice.\end{defi}
\begin{equation}
1+1=2%
\end{equation}%
\begin{defi}Kombucha is a bloh bloh bloh.\end{defi}
\end{document}
  • 1
    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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.