4

Below is a very simplified example to illustrate a problem with spacing before and after environments:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsthm}
\usepackage{amsmath}
\usepackage{xcolor}

% Define the theorem environment
\newenvironment{question}[1][]
  {\vspace{1cm}
  \begin{theorem}
    \color{blue}
    \if\relax\detokenize{#1}\relax
    \else
      \textbf{#1.}\hspace{3pt}
    \fi}
  {\end{theorem}\vspace{1cm}}

% Define the theorem style
\theoremstyle{definition}
\newtheorem{theorem}{Question}

\begin{document}

\section{Custom Theorem Environment}

\begin{question}[My Custom Question 1]
  What is the meaning of life, the universe, and everything?
  \end{question}

This is working really nicely

\begin{question}[My Custom Question 2]
  How can we ensure world peace?
  \end{question}

%\vspace{-1cm} % - manual fix to the problem

\begin{question}[My Custom Question 3]
  But if we have two questions one after each other we get a double up of spacing
  \end{question}

\end{document}

Here, we have a custom environment with 1cm padding before and after it. This works well when we have text in between when the environments are invoked. However, when environments are invoked one after each other, there is a doubling up of text. Is there a way to avoid this with an automatic solution? I have manually commented in what will suppress the effect, but I am looking for a solution which deals with this problem automatically.

With the problem:

enter image description here

Ideal output:

enter image description here

2

1 Answer 1

7

Consider using \addvspace{1cm} instead of \vspace{1cm}. The former only adds space up to the maximum length specified.

enter image description here

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsthm}
\usepackage{amsmath}
\usepackage{xcolor}

% Define the theorem environment
\newenvironment{question}[1][]{%
  \par% Ensure you're in vertical mode
  \addvspace{1cm}
  \begin{theorem}
    \color{blue}%
    \if\relax\detokenize{#1}\relax
    \else
      \textbf{#1.}\hspace{3pt}
    \fi}
  {\end{theorem}\addvspace{1cm}}

% Define the theorem style
\theoremstyle{definition}
\newtheorem{theorem}{Question}

\begin{document}

\section{Custom Theorem Environment}

\begin{question}[My Custom Question 1]
What is the meaning of life, the universe, and everything?
\end{question}

This is working really nicely

\begin{question}[My Custom Question 2]
How can we ensure world peace?
\end{question}

\begin{question}[My Custom Question 3]
But if we have two questions one after each other we get a double up of spacing
\end{question}

\end{document}
1

You must log in to answer this question.

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