When I try to make either \intertext or \shortintertext conditional I get excess vertical spacing prior to the conditional text as per the MWE below. Note the excess vertical spacing prior to "Albert insisted that" and "but we know".
The minipage on the right represents the correct spacing as it does not have the \ConditionalText{} macro applied.

However, when the conditional is disabled, via the \toggletrue{}, the text is suppressed and the spacing is fine:

Notes:
It seems that with
\NewDocumentCommand{\ConditionalText}{O{red} +m}{#2}%the spacing problem is still there. However, the problem is resolved if I use the
\iftoggledirectly within thealignenvironment as:\newcommand{\TextWithConditional}{% \hfill\textbf{Intertext with Conditionals}\hfill% Newton said: \begin{align*} F &= ma \\ \iftoggle{SupressConditionalText}{}{\intertext{Albert insisted that}} E &=mc^2 \\ \iftoggle{SupressConditionalText}{}{\shortintertext{but we know}} E &=\frac{1}{2}mv^2 \end{align*} Text after a conditional display math formula.% }%
Failed Solution:
Well, I had what I thought was a brilliant fix, but it seems that all I can do is to quote Edison and say "I have discovered a method that does not work". Not sure why it does not work though.
\NewDocumentCommand{\ConditionalText}{O{red} +m}{% \ignorespaces% \iftoggle{SupressConditionalText}{}{% \ifmmode% \let\OldIntertext\intertext% \let\OldShortIntertext\shortintertext% \def\intertext{\vspace*{\baselineskip}\OldIntertext}% \def\shortintertext{\vspace*{-\baselineskip}\OldShortIntertext}% #2% Don't underline if in math mode \else% %\UnderlineText[#1]{#2}% This case not used in this MWE. \fi% }% \ignorespacesafterend% }%
Code:
\documentclass{article}
\usepackage{xparse}% Not essential for this problem.
\usepackage{xcolor}
\usepackage{etoolbox}
\usepackage{mathtools}
\usepackage{soul}
\usepackage{parskip}% Eliminate need for \noindent
%\usepackage{showframe}
\newtoggle{SupressConditionalText}%
\togglefalse{SupressConditionalText}% This has too much space BEFORE the conditional \intertext.
\NewDocumentCommand{\UnderlineText}{O{red} +m}{%
\setulcolor{#1}\ul{#2}%
}%
\NewDocumentCommand{\ConditionalText}{O{red} +m}{%
\ignorespaces%
\iftoggle{SupressConditionalText}{}{%
\ifmmode%
#2% Don't underline if in math mode
\else%
\UnderlineText[#1]{#2}%
\fi%
}%
\ignorespacesafterend%
}%
\newcommand{\TextWithConditional}{%
\hfill\textbf{Intertext with Conditionals}\hfill%
Newton \ConditionalText{often} said:
\begin{align*}
F &= ma \\
\intertext{Albert \ConditionalText{awkwardly} insisted that}
E &=mc^2 \\
\ConditionalText{\shortintertext{but we know}}
E &=\frac{1}{2}mv^2 \\
\ConditionalText{\intertext{But, equating the two we obtain.}}
mc^2 &= \frac{1}{2}mv^2
\shortintertext{from which \ConditionalText{we obtain}}
v &= \sqrt{2}c
\end{align*}
Text after a conditional display math formula.%
}%
\newcommand{\TextWithoutConditional}{%
\hfill\textbf{Intertext without Conditionals}\hfill%
Newton said:
\begin{align*}
F &= ma \\
\intertext{Albert awkwardly insisted that}
E &=mc^2 \\
\shortintertext{but we know}
E &=\frac{1}{2}mv^2
\end{align*}
Text after a conditional display math formula.%
}%
\newcommand{\TextWithoutConditionalWithoutInterText}{%
\hfill\textbf{No Intertext, No Conditionals}\hfill%
Newton said:
\begin{align*}
F &= ma \\
E &=mc^2 \\
E &=\frac{1}{2}mv^2
\end{align*}
Text after a conditional display math formula.%
}%
\begin{document}
\begin{minipage}[t]{0.45\linewidth}
\TextWithConditional
\end{minipage}
\hfill\vrule\hfill%
\begin{minipage}[t]{0.45\linewidth}
\TextWithoutConditional
\end{minipage}
\bigskip\hrule
\toggletrue{SupressConditionalText}% Disable conditional text
%
With conditional text supressed, things work fine:
\begin{minipage}[t]{0.45\linewidth}
\TextWithConditional
\end{minipage}%
\hfill\vrule\hfill%
\begin{minipage}[t]{0.45\linewidth}
\TextWithoutConditionalWithoutInterText
\end{minipage}%
\end{document}

\intertextuses the TeX primitive\noalignto insert text which is not aligned with the rest. This primitive may only appear just after\\(well, after the underlying\crprimitive). More precisely, TeX expands what follows (and ignores spaces) until finding a non-expandable token. If that token is\noalign, then it prepares to put stuff out of the alignment. Now,\intertextis clever, and does some (expandable) tests to make sure that it is preceeded by\\, adding it if needed. Since your condition is non-expandable,\\gets inserted. – Bruno Le Floch Feb 23 '13 at 03:22\DeclareExpandableDocumentCommand(make sure that all arguments are either long or short) and remove\ignorespacesfrom the beginning of the definition of\ConditionalText– cgnieder Mar 11 '13 at 11:30\DeclareExpandableDocumentCommand{\ConditionalText}{O{red} m}and comment out\ignorespacesit seems as if the value of thetoggleis completely ignored, which I don't get at all. This has to be something obvious, but I am not seeing it at the moment. – Peter Grill Mar 11 '13 at 18:55\ifmmodeconditional... – cgnieder Mar 11 '13 at 18:58\UnderlineText, but that yieldsMisplaced \noalign(if I un-comment the related code). Can't wait until the day that expansion makes sense to me... – Peter Grill Mar 11 '13 at 19:25\ConditionalTextthat is not expandable before\intertext. Since you supply\intertextas argument you get\setulcolor{red}\ul{\intertext{<text>}}while it should be\intertext{\setulcolor{red}\ul{<text>}}. Every assignment (like\setulcolor{red}) is not expandable. – cgnieder Mar 11 '13 at 20:27\ifmmodeis expanded at that point, but then if it doesn't find\noalignor\omitit inserts thehalignpreamble which in this case is from align so that starts math mode and then TeX re-processes the tokens. Normal way to stop that is to make your command robust so that it doesn't expand in the initial scan, but you can't do that as then\intertextwon't be seen. – David Carlisle Mar 13 '13 at 21:54\intertext? That seems very unlikely, must have misunderstood. – Peter Grill Mar 13 '13 at 21:56ConditionalTextto be used? If it is only inalignthen it's easy: just don't test for\ifmmodeIf it is always used at the start of some kind of alignment row you can hide teh non expandable tests in\noalignif it needs to work both in alignments and normal texts it's a bit harder but not impossible – David Carlisle Mar 13 '13 at 22:09