等式着色后,后面会添加额外的空格

等式着色后,后面会添加额外的空格

出于某种原因,当我为方程式上色时,方程式后面会添加空格。如果为文本添加颜色,则不会发生这种情况。

有没有一种简单的方法可以防止这种情况发生,比如全局设置(我不想用手动调整空间\vspace{-1cm})?也许有更好的方法来给我的方程式上色(另一个包、另一个命令等),或者换句话说,我做错了什么?

在此处输入图片描述

\documentclass[12pt]{standalone}

\usepackage{amsmath}
\usepackage{color}

\begin{document}

\begin{minipage}{4cm}
\begin{equation*}
    a = b + c
\end{equation*}
\begin{align*}
    a - b &= c\\
    a-c & b
\end{align*}
\end{minipage}

\vline

\begin{minipage}{4cm}
\begin{equation*}
\color{blue}    a = b + c
\end{equation*}
\begin{align*}
    a - b &= c\\
    a-c & b
\end{align*}
\end{minipage}

\end{document}

以供参考,我发现最接近这个问题的问题似乎没有解决我的问题,即添加\fboxsep0pt我的序言并没有改变输出。

编辑 该注释With {\color{blue}a = b + c} there is no additional space ...解决了部分问题,但在使用时不起作用,例如align(它给出错误):

\begin{align*}
{\color{blue}    a - b &= c}\\
    a-c &= b
\end{align*}

如果我尝试使用两次将 & 留在外面的命令来修复此问题,则周围的间距=是错误的:

\begin{align*}
{\color{blue}    a - b} &{\color{blue}= c}\\
    a-c &= b
\end{align*}

答案1

哦。这是 AMS 的一个错误,对齐不颜色安全。之前从未注意到这一点:-) 在这种情况下,您需要明确分组(使用 begingroup 而不是 bgroup,或者{因为它对数学间距的干扰较少)

在此处输入图片描述

\documentclass[12pt]{standalone}

\usepackage{amsmath}
\usepackage{color}

\begin{document}

\begin{minipage}{4cm}
\begin{equation*}
    a = b + c
\end{equation*}
\begin{align*}
    a - b &= c\\
    a-c & b
\end{align*}
\end{minipage}

\vline

\begin{minipage}{4cm}
\begin{equation*}
\begingroup\color{blue}    a = b + c\endgroup
\end{equation*}
\begin{align*}
    a - b &= c\\
    a-c & b
\end{align*}
\end{minipage}

\end{document}

其实,称其为自动化系统错误,在 LaTeX 中也能看到同样的效果\[,但是,这不是对每个案例进行分组,而是正确的修复方法(在加载后的序言中的任何地方)amsmath。我会 ping @barbarabeeton

\def\foo#1$$#2!!{\def\mathdisplay##1{#1$$\begingroup#2}}
\expandafter\foo\mathdisplay{#1}!!

\long\def\foo#1$$#2!!{\def\endmathdisplay##1{#1\endgroup$$#2}}
\expandafter\foo\endmathdisplay{#1}!!

答案2

在清理旧文件时发现大卫对此的询问,我最终进行了调查。

虽然两个显示环境相邻时确实存在额外的(不需要的)空间,并且添加颜色时该空间会增加,但amsmath 显示环境不是设计为以这种方式使用。 两个文本块之间的所有显示材料都应放置在单个环境中,然后可以根据需要将其细分为“子”嵌套环境。 (有人在评论中提到了这一点,但它并没有出现在大卫的回答中。)这里有一个扩展的例子,显示了最后一列中所需的结果。 (我已更改bluered因为蓝色往往会在我的屏幕上消失。)

\documentclass[12pt]{standalone}

\usepackage{amsmath}
\usepackage{color}

\begin{document}

\begin{minipage}{4cm}
\begin{equation*}
    a = b + c
\end{equation*}
\begin{align*}
    a - b &= c\\
    a-c &= b
\end{align*}
\end{minipage}

\vline

\begin{minipage}{4cm}
\begin{equation*}
\color{red}    a = b + c
\end{equation*}
\begin{align*}
    a - b &= c\\
    a-c & b
\end{align*}
\end{minipage}

\vline

\begin{minipage}{4cm}
\begin{equation*}
\begingroup\color{red}    a = b + c\endgroup
\end{equation*}
\begin{align*}
    a - b &= c\\
    a-c &= b
\end{align*}
\end{minipage}

\vline

\begin{minipage}{4cm}
\begin{gather*}
\begingroup\color{red}    a = b + c\endgroup\\
\begin{split}
    a - b &= c\\
    a-c &= b
\end{split}
\end{gather*}
\end{minipage}

\end{document}

示例代码的输出

那么这是 中的错误吗amsmath?如果当“简单”单环境实例位于两个文本块之间时出现此额外空格,那么这肯定是一个错误。如果该上下文中没有额外空格,那么它就是不是一个错误,因为该行为是amsmath被设计用来做的。这里有一个例子来解决这个问题。

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{color}

\begin{document}

start with some text.  make it a rather long line.
\begin{equation*}
    a = b + c
\end{equation*}
some more text.  make this also a rather long line.
\begin{equation*}
\color{red}    a = b + c
\end{equation*}
still more text.  make this also a rather long line.

\end{document}

示例代码的输出

没有多余的空间。因此,我认为这个问题是环境的误用,而不是一个错误。

相关内容