5

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 \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}

Peter Grill
  • 223,288

1 Answers1

1

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%
}

enter image description here


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

\advance\belowdisplayskip1sp
\advance\belowdisplayshortskip1sp
\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
}
David Carlisle
  • 757,742
  • 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
  • Not sure I understand. I want each named section to determine if it should have a \par before it or not. – Peter Grill Mar 21 '13 at 18:57
  • @PeterGrill maybe I'm misled by the word "section" because I wouldn't expect a section to start I horizontal mode, which is why I asked if you need to omit parts of a paragraph eg individual words or phrases. Trouble is I can't think off hand of a reliable way (in one pass) of detecting that you are just after display math without leaving hmode (or redefining every display math construct to leave a marker, but that would have other problems) – David Carlisle Mar 21 '13 at 19:38
  • I defined at the top of the question that section was a "named section" not the LaTeX \section -- I should have come up with a different name. – Peter Grill Mar 21 '13 at 20:00