Controlling Space Between Paragraph and Example Environment in Amsthm

amsthmspacingtheorems

Which parameter sets the spacing before and after of the example environment from amsthm library?

MWE:

\documentclass{article}
\usepackage{lipsum}
\usepackage{amsthm}

\newtheorem{example}{Exam}[section]

\begin{document}

  \section{My section}
    \lipsum[2] % Dummy text
    \begin{example}
       \lipsum[1] % Dummy text
       \begin{equation}
         x^2 + y^2 = 1
       \end{equation}
    \end{example}  
    \lipsum[2] % Dummy text  

\end{document}

Best Answer

The amsthm package offers three predefined styles for the theorem-like structures: plain (the default style), definition and remark; the remark style leaves less space before and after the structure than the other two styles, as can be seen in the following example:

\documentclass{article}
\usepackage{amsthm}
\usepackage[hmargin=2cm]{geometry}% just for the example
\usepackage{lipsum}% just to generate text for the example

\newtheorem{exai}{Plain}
\theoremstyle{definition}
\newtheorem{exaii}{Definition}
\theoremstyle{remark}
\newtheorem{exaiii}{Remark}

\begin{document}

\lipsum[4]
\begin{exai}
\lipsum[4]
\end{exai}
\lipsum[4]
\begin{exaii}
\lipsum[4]
\end{exaii}
\lipsum[4]
\begin{exaiii}
\lipsum[4]
\end{exaiii}
\lipsum[4]

\end{document}

The internal lengths controlling the spacing before and after the structures are \thm@preskip (for the space before) and \thm@postskip (for the space after); in amsthm.sty one finds:

\def\thm@space@setup{%
  \thm@preskip=\topsep \thm@postskip=\thm@preskip

So the value of \topsep will be used for the structures built with the plain and definition styles; on the other hand, for the remark style, amsthm.sty has:

\def\th@remark{%
  \thm@headfont{\itshape}%
  \normalfont % body font
  \thm@preskip\topsep \divide\thm@preskip\tw@
  \thm@postskip\thm@preskip
}

so, for the remark style, half the value of \topsep will be used for the structures built with this style.

If you want to change the default value for the plain and definition styles, you could then use in the preamble something like

\makeatletter
\def\thm@space@setup{%
  \thm@preskip=.5\topsep \thm@postskip=\thm@preskip
}
\makeatother

A complete example:

\documentclass{article}
\usepackage{lipsum}
\usepackage{amsthm}

\makeatletter
\def\thm@space@setup{%
  \thm@preskip=.5\topsep \thm@postskip=\thm@preskip
}
\makeatother

\newtheorem{example}{Exam}[section]

\begin{document}

  \section{My section}
    \lipsum[2] % Dummy text
    \begin{example}
       \lipsum[1] % Dummy text
       \begin{equation}
         x^2 + y^2 = 1
       \end{equation}
    \end{example}  
    \lipsum[2] % Dummy text  

\end{document}

Of course, this will affect all theorem-like structures for the two styles plain and definition, and it will also affect the spacing after the structure, since \thm@postskip=\thm@preskip; if you want the change only for a particular structure, you can use \newtheoremstyle (refer to the documentation for amsthm) to define the settings appropriately. A little example (using a space that is too small. but it is just for example purposes):

\documentclass{article}
\usepackage{lipsum}
\usepackage{amsthm}

\newtheoremstyle{example}% ⟨name⟩
{3pt}%⟨Space above⟩
{3pt}%⟨Space below⟩
{\itshape}%⟨Body font⟩
{}%⟨Indent amount⟩
{\bfseries}% ⟨Theorem head font⟩
{.}%⟨Punctuation after theorem head⟩
{.5em}%⟨Space after theorem head⟩2
{}%⟨Theorem head spec (can be left empty, meaning ‘normal’)⟩
\theoremstyle{example}
\newtheorem{example}{Exam}[section]

\begin{document}

  \section{My section}
    \lipsum[2] % Dummy text
    \begin{example}
       \lipsum[1] % Dummy text
       \begin{equation}
         x^2 + y^2 = 1
       \end{equation}
    \end{example}  
    \lipsum[2] % Dummy text  

\end{document}