# Add \par only if last paragraph did not end with displayed math

## Background:

I have named "sections" (sections for rest of this question) which are conditionally displayed or suppressed based on parameters. To simplify the test case below, these are controlled by the three \defs following \begin{document}.

Each of these sections should be able to control whether they are typeset in a new paragraph or are to continue from the last. This seems to be working for 3/4 of the cases. Where I run into a problem is if the last enabled section ended in a display math environment, and the following section wants to always be displayed as a new paragraph (that is not continue on as part of the previous paragraph as is the case for the IntroDetails paragraph.

## Problem:

The MWE below reproduces the problem case, and you can see that the Summary paragraph starts further down then it should. To see this more clearly, you can see the other cases that work, which is any combination of commenting out at least one of the following:

\def\EndWithDisplayMath{}%
\def\SupressIntroDetails{}%


So, it seems what I need is a macro that redefines \par if the last typeset content ended in display math. I would like to add the intelligence to the \NamedSection macro so that each of the named content sections don't need to take into consideration if the previous typeset content ended with displayed math.

## Notes:

• In case it is relevant, the sections are always typset in the order they appear in the document, but each section can be typeset or suppressed independently of the others.
• I attempted to reproduce the problem within a minipage environment to make it easier to see the problem spacing, but things seems to work just fine in a minipage?

## References:

• I am using a modified version of the solution from Remove excess space at end to eliminate the vertical space if the section ended with with a display math equation. The modification was to remove the \par which is most likely related to the \RemoveSpaceAtEnd not quite working.

## Code:

\def\EndWithDisplayMath{}% Problem ONLY if BOTH of these are uncommented.
\def\SupressIntroDetails{}%

\documentclass{article}
\usepackage{showframe}
\usepackage{parskip}
\usepackage{xparse}

% https://tex.stackexchange.com/questions/45943/remove-excess-space-at-end
\newcommand{\RemoveSpaceAtEnd}[1]{%
\begingroup
%\par% Removed this from solution from 45943
#1%
%\par% Removed this from solution from 45943
\ifdim\lastskip=\belowdisplayshortskip
%\typeout{display here (short)}%
\nobreak
\vskip-\belowdisplayshortskip
\else
\ifdim\lastskip=\belowdisplayskip
%\typeout{display here \noexpand#1}%
\nobreak
\vskip-\belowdisplayskip
\fi
\fi
\endgroup
}

\NewDocumentCommand{\NamedSection}{%
m% {#1} = name for sub-section
+m% {#2} = content ("+" can have para)
}{%
\ignorespaces%
\ifcsname#1\endcsname%
\RemoveSpaceAtEnd{#2}%
\fi%
\ignorespacesafterend%
}

\begin{document}

% Control which sections to display:
\def\Intro{}%
\ifdefined\SupressIntroDetails
\else
\def\IntroDetails{}%
\fi
\def\Summary{}%

\NamedSection{Intro}{%
First we introduce the topic.
\ifdefined\EndWithDisplayMath
$E = mc^2.$
\fi
}%
\NamedSection{IntroDetails}{%
Followed by more intro details.
}%
%
% Leaving a blank line here works fine, except if "IntroDetails" are supressed.
\NamedSection{Summary}{%
%  Want this to start on its own line.
\par%  Using \par works fine, except if "IntroDetails" are supressed.
And finally summarize the topic.
}%

\end{document}

• Just thinking... What about \everydisplay setting something, \everypar resetting that (so that you can detect whether a paragraph contained a display anywhere)? Then you only have to know whether it was the last thing. And maybe (maybe?) this can be detected by comparing \prevgraf at the beginning of the (last) display in the paragraph and at the end of the paragraph - if the difference is 3, the display was the last thing. Of course, this would need to tinker with \everypar, probably also \par - but something like this might work. – mbork Mar 21 '13 at 7:41
• @mbork: I think David Carlisle's solution from Remove excess space at end is the key, but not sure why removing the \par from \RemoveSpaceAtEnd is a problem. Perhaps need the \par to be able to detect the last display skip. – Peter Grill Mar 21 '13 at 7:56
• Related Questions: Excess vertical space in mdframed ending with display math environment, Remove excess space at end, and [How to check if last displayed content was displaymath / minipage ends on displaymath? ](tex.stackexchange.com/q/448856/4301). – Peter Grill Oct 4 '18 at 20:13

As far as I can see you just want to put my \par back and then

\NewDocumentCommand{\NamedSection}{%
m% {#1} = name for sub-section
+m% {#2} = content ("+" can have para)
}{%
\ignorespaces%
\ifcsname#1\endcsname%
\RemoveSpaceAtEnd{#2}%
\fi%
\ignorespaces%afterend%
}


If you need the sections sometimes to be mid-paragraph then you can restrict the use of \par to the beginning of a horizontal list, which means it's either after  or \noindent (or you are in an inner list where \par does nothing anyway) or you are doing something very sneaky.

\makeatletter

\newcommand{\RemoveSpaceAtEnd}[1]{%
%\par% Removed this from solution from 45943
#1%
\def\next{}%
\ifhmode\ifnum\lastnodetype=-1\par
\let\next\@doendpe
\fi% Removed this from solution from 45943
\ifdim\lastskip=\belowdisplayshortskip
%\typeout{display here (short)}%
\nobreak
\vskip-\belowdisplayshortskip
\else
\ifdim\lastskip=\belowdisplayskip
%\typeout{display here \noexpand#1}%
\nobreak
\vskip-\belowdisplayskip
\fi
\fi
\next
}

• The reason the \pars were removed was that I want each named section to determine if it starts a new paragraph or not. Each section is not necessarily a new paragraph. So, with the \par put back in \RemoveSpaceAtEnd, the version which has both \def\EndWithDisplayMath{} and \def\SupressIntroDetails{} commented is incorrect. Sorry for not making that explicit enough -- this is buried in the comments in the Summary section. – Peter Grill Mar 21 '13 at 14:23
• It is strange that the modified \RemoveSpaceAtEnd works fine in a minipage. I thought this was due to the modified \parskip, but that has no effect -- so, even restoring parkskip the results in a minipage are fine. – Peter Grill Mar 21 '13 at 14:25
• @PeterGrill Well not that strange as at the start of a minipage you are already in vmode so the first \par would do nothing anyway so removing it does no harm. – David Carlisle Mar 21 '13 at 14:35
• I don't see how the issue is with the first \par in \RemoveSpaceAtEnd if within a minipage. I belive it with the second one, at least in this MWE. But both \pars in \RemoveSpaceAtEnd are problematic for me, in the general case. – Peter Grill Mar 21 '13 at 17:06
• @PeterGrill You mean you want to omit things mid paragraph in general? – David Carlisle Mar 21 '13 at 17:13