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 \def
s 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 aminipage
?
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
\advance\belowdisplayskip1sp
\advance\belowdisplayshortskip1sp
%\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}
\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.\par
from\RemoveSpaceAtEnd
is a problem. Perhaps need the\par
to be able to detect the last display skip.