# Extra vertical space if custom environment ends with displaymath

I have defined the following custom nub environment in my preamble:

\makeatletter
\newenvironment{nub}[1][\@nil]{%
\def\tmp{#1}%
\ifx\tmp\@nnil
\subsection{}
\else
\subsection{#1.}
\fi}
\makeatother


This works perfectly for my needs, except when a nub ends with an equation, or any other maths in display mode, because then it adds a bunch more vertical space before the next nub.

Here is a screenshot where you can see the extra spacing, before (6.4.4):

I'm sure there must be some answer for this problem on the site already, but I couldn't find anything!

You're forgetting the “end part” in the definition of nub, which should be \ignorespacesafterend. With the code you show, \makeatother is taken instead.

\documentclass{book}
\usepackage{amsmath}

\makeatletter
\newenvironment{nub}[1][\@nil]{%
\def\tmp{#1}%
\ifx\tmp\@nnil
\subsection{}
\else
\subsection{#1.}
\fi}{\ignorespacesafterend}
\makeatother

\begin{document}

\setcounter{chapter}{6}\setcounter{section}{4}\setcounter{subsection}{2}

\begin{nub}
text text text text text text text text text text
text text text text text text text text text text
text text text text text text text text text text
\begin{equation*}
F(x)=G(x)
\end{equation*}
\end{nub}

\begin{nub}[Lemma]
text text text text text text text text text text
text text text text text text text text text text
text text text text text text text text text text
\end{nub}

\end{document}


A simpler definition with xparse:

\documentclass{book}
\usepackage{amsmath}
\usepackage{xparse}

\NewDocumentEnvironment{nub}{o}
{\subsection{\IfValueT{#1}{#1.}}}
{\ignorespacesafterend}


### How I would implement it

\documentclass{book}
\usepackage{amsmath}
\usepackage{amsthm,xparse}

\newtheoremstyle{nub}
{\topsep}     % ABOVESPACE
{\topsep}     % BELOWSPACE
{\normalfont} % BODYFONT
{0pt}         % INDENT (empty value is the same as 0pt)
{5pt plus 1pt minus 1pt} % HEADSPACE

\theoremstyle{nub}
\newtheorem{nubinner}[subsection]{\nubname}
\newcommand{\nubname}{}

\NewDocumentEnvironment{nub}{o}
{\IfValueT{#1}{\renewcommand{\nubname}{ #1.}}\begin{nubinner}}
{\end{nubinner}}

\begin{document}

\setcounter{chapter}{6}\setcounter{section}{4}\setcounter{subsection}{2}

\begin{nub}
text text text text text text text text text text
text text text text text text text text text text
text text text text text text text text text text
\begin{equation*}
F(x)=G(x)
\end{equation*}
\end{nub}

\begin{nub}[Lemma]
text text text text text text text text text text
text text text text text text text text text text
text text text text text text text text text text
\end{nub}

\end{document}


• just adding the \ignorespacesafterend fixed everything, thank you! is there any reason to use your second solution over this fix (besides the code looking much more understandable)? – Tim Jun 4 '19 at 21:25
• @Tim The second solution, the one with \newtheorem, is not a hack. Yours is. – egreg Jun 4 '19 at 21:49
• haha, fair enough, thanks! – Tim Jun 4 '19 at 22:07
• for some reason the optional argument (e.g. Lemma) doesn't seem to do anything for me in your second solution... – Tim Jun 5 '19 at 15:43
• @Tim Without seeing your general setup it’s difficult to make a diagnosis – egreg Jun 5 '19 at 16:02