在 \intertext 和 \shortintertext 周围应用条件会留下多余的垂直空间

在 \intertext 和 \shortintertext 周围应用条件会留下多余的垂直空间

当我尝试将“\intertext或”设置\shortintertext为条件时,我会在条件文本之前获得多余的垂直间距,如下面的 MWE 所示。请注意“艾伯特坚持认为”“但我们知道”

右侧minipage代表正确的间距,因为它没有\ConditionalText{}应用宏。

在此处输入图片描述

但是,当通过 禁用条件时,\toggletrue{}文本将被抑制并且间距正常:

在此处输入图片描述

笔记:

  • 似乎

    \NewDocumentCommand{\ConditionalText}{O{red} +m}{#2}%
    

    \iftoggle间距问题仍然存在。但是,如果我直接在环境中使用,问题就会得到解决align

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

失败的解决方案:

  • 好吧,我找到了一个我认为非常出色的解决办法,但似乎我所能做的就是引用爱迪生的话:“我发现了一个方法,不是工作”。但不确定为什么它不起作用。

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

代码:

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

答案1

我认为这会满足您的要求,并使测试具有可扩展性,如果您处于align*ifmmode为真,它将采用数学分支。它需要扩展到其他环境。

在此处输入图片描述

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

\makeatletter
\newtoggle{SupressConditionalText}%

\togglefalse{SupressConditionalText}% This has too much space BEFORE the conditional \intertext.

% Not used in this paritcular example
\NewDocumentCommand{\UnderlineText}{O{red} +m}{%
    \setulcolor{#1}\ul{#2}%
}%

\def\xaligns{align*}
\def\ConditionalText#1{%
    \iftoggle{SupressConditionalText}{}{%
        \ifx\@currenvir\xaligns
          #1%
        \else\ifmmode
           #1% Don't underline if in math mode
        \else%
           \UnderlineText[red]{#1}% This case not used in this MWE.
        \fi
       \fi
    }%
    \ignorespacesafterend%
}%

\newcommand{\TextWithConditional}{%
    \hfill\textbf{Intertext with Conditionals}\hfill%
    Newton said:
    \begin{align*}
        F &= ma \\
        \ConditionalText{\intertext{Albert insisted that}}
        E &=mc^2 \\
        \ConditionalText{\shortintertext{but we know}}
        E &=\frac{1}{2}mv^2
    \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 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}

相关内容