当我更改公式颜色时,会出现一个空白行

当我更改公式颜色时,会出现一个空白行

我想改变所有方程的颜色,所以我重新定义了方程环境​​代码,但是方程后面有一个空行,我的代码如下:

情况1:

\documentclass[]{article}
\usepackage{lipsum}
\usepackage{xcolor}
\usepackage{amsmath}

\makeatletter

\renewenvironment{equation}{%
\incr@eqnum
\mathdisplay@push\color{red}
\st@rredfalse \global\@eqnswtrue
\mathdisplay{equation}%
}{%
\endmathdisplay{equation}%
\mathdisplay@pop
\ignorespacesafterend
}
\makeatother

\begin{document}
\lipsum[2]
\begin{equation}\label{eq1}
    {x_{n + 1}} = F({x_n},\lambda ),{x_n} \in [a,b]. 
\end{equation}

\lipsum[2]

\end{document}

不包含的代码\color{red}如下:

情况 2:

\documentclass[]{article}
\usepackage{lipsum}
\usepackage{xcolor}
\usepackage{amsmath}

\makeatletter

\renewenvironment{equation}{%
\incr@eqnum
\mathdisplay@push
\st@rredfalse \global\@eqnswtrue
\mathdisplay{equation}%
}{%
\endmathdisplay{equation}%
\mathdisplay@pop
\ignorespacesafterend
}
\makeatother

\begin{document}
\lipsum[2]
\begin{equation}\label{eq1}
    {x_{n + 1}} = F({x_n},\lambda ),{x_n} \in [a,b]. 
\end{equation}

\lipsum[2]

\end{document}

如下图所示,当\color{red}添加后额外的红色方程式下方有空格。希望有人能告诉我原因。

在此处输入图片描述

答案1

如果你简化你的例子,就更容易看出空格来自哪里(以及它是段落的一行,而不是空格)

\documentclass[]{article}

\usepackage{xcolor}
\usepackage{amsmath}

\showoutput
\showboxdepth4
\begin{document}


1
\begin{equation}\label{eq1}
   xx
\end{equation}


2


\makeatletter

\renewenvironment{equation}{%
\incr@eqnum
\mathdisplay@push\color{red}%%%%
\st@rredfalse \global\@eqnswtrue
\mathdisplay{equation}%
}{%
\endmathdisplay{equation}%
\mathdisplay@pop
\ignorespacesafterend
}
\makeatother

3
\begin{equation}\label{eq2}
   xx
\end{equation}


4



\end{document}

生产

在此处输入图片描述

查看对数后,您会看到第一个等式和下面的段落“2”是

...\hbox(7.5+2.5)x178.21527, shifted 166.78473
....\hbox(4.30554+0.0)x11.43054, display []
....\kern154.00691
....\hbox(7.5+2.5)x12.77782, display []
...\penalty 0
...\glue(\belowdisplayshortskip) 6.0 plus 3.0 minus 3.0
...\glue(\parskip) 0.0 plus 1.0
...\glue(\baselineskip) 3.05556
...\hbox(6.44444+0.0)x345.0, glue set 324.99998fil
....\hbox(0.0+0.0)x15.0
....\OT1/cmr/m/n/10 2

因此,您只需在段落前留出正常空格加上\belowdisplayshortskip空格。

与第二个等式和段落“4”进行比较

...\hbox(7.5+2.5)x178.21527, shifted 166.78473
....\hbox(4.30554+0.0)x11.43054, display []
....\kern154.00691
....\hbox(7.5+2.5)x12.77782, display []
...\penalty 0
...\glue(\belowdisplayshortskip) 6.0 plus 3.0 minus 3.0
...\glue(\baselineskip) 9.5
...\hbox(0.0+0.0)x345.0, glue set 345.0fil
....\pdfcolorstack 0 pop
....\penalty 10000
....\glue(\parfillskip) 0.0 plus 1.0fil
....\glue(\rightskip) 0.0
...\glue(\parskip) 0.0 plus 1.0
...\glue(\baselineskip) 5.55556
...\hbox(6.44444+0.0)x345.0, glue set 324.99998fil
....\hbox(0.0+0.0)x15.0
....\OT1/cmr/m/n/10 4

在这里您可以看到以“3”开头并被数学显示打断的段落有一个最后(白色)行,其中仅包含颜色恢复。

颜色恢复不仅仅是像在组末尾恢复字体那样的状态切换,它是列表中的真实节点(很像一个空盒子,技术上是一个那是什么节点)并且由于列表中还有另一个节点,显示数学首先返回到水平模式,然后空白行结束段落,因此您得到最后全白的段落行。

请注意,这不是垂直空间,例如不会在分页符处被丢弃。

如果你将颜色节点插入更安全的地方,那么

在此处输入图片描述

\documentclass[]{article}

\usepackage{xcolor}
\usepackage{amsmath}

\showoutput
\showboxdepth4
\begin{document}


1
\begin{equation}\label{eq1}
   xx
\end{equation}


2


\makeatletter

\renewenvironment{equation}{%
\incr@eqnum
\mathdisplay@push
\st@rredfalse \global\@eqnswtrue
\let\zzz\tagform@
\def\tagform@##1{\textcolor{red}{\zzz{##1}}}%%%
\mathdisplay{equation}%
\begingroup\color{red}%%%%%
}{%
\endgroup\endmathdisplay{equation}%
\mathdisplay@pop
\ignorespacesafterend
}
\makeatother

3
\begin{equation}\label{eq2}
   xx
\end{equation}


4



\end{document}

答案2

似乎是\egroup\endmathdisplay 期间的某个或某些类似的东西导致了这个问题。

确认测试代码(使用\textcolor不带不需要的空间的着色):

\documentclass[]{article}
\usepackage{lipsum}
\usepackage{xcolor}
\usepackage{amsmath}

\makeatletter

\renewenvironment{equation}{%
\incr@eqnum
\mathdisplay@push
%\textcolor{blue}\bgroup
\st@rredfalse \global\@eqnswtrue
\mathdisplay{equation}%
\textcolor{blue}\bgroup
}{%
\egroup
\endmathdisplay{equation}%
%\egroup
\mathdisplay@pop
\ignorespacesafterend
}
\makeatother

\begin{document}
\lipsum[2]
\begin{equation}\label{eq1}
    {x_{n + 1}} = F({x_n},\lambda ),{x_n} \in [a,b]. 
\end{equation}

\lipsum[2]

\end{document}

如果您在注释的开始和结束时使用相同的代码开始和结束,您将看到问题再次出现。

因此,这里\endmathdisplay隐藏着您问题的原因。

相关内容