15

For some reason space is added after my equation when I colour it. This does not happen if the colour is added to text.

Is there a simple way to prevent this from happening, like a global setting (I am not interested in manually adjusting the space with \vspace{-1cm})? Perhaps there is a better way to colour my equation (another package, another command, etc), or in other words, am I doing something wrong?

enter image description here

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

For reference, the closest question I found to this one doesn't seem to solve my problem, ie, adding \fboxsep0pt to my preamble did not change the output.

EDIT The comment With {\color{blue}a = b + c} there is no additional space ... solves part of the problem, but it doesn't work when using for example align (it gives an error):

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

If I try to fix this using the command twice leaving the & outside, the spacing around = is wrong:

\begin{align*}
{\color{blue}    a - b} &{\color{blue}= c}\\
    a-c &= b
\end{align*}
Mensch
  • 65,388
Vivi
  • 26,953
  • 31
  • 77
  • 79
  • Hmmm. Intersting question... Using \textcolor{blue}{a = b + c} there is no additional space, – Peter Grill Jan 13 '13 at 03:38
  • True, but then it creates problem if used with align. I will add an example to show that. – Vivi Jan 13 '13 at 03:40
  • 1
    Spacing can be fixed via {\color{blue}{}= c}. – Peter Grill Jan 13 '13 at 03:59
  • @PeterGrill It seems like too many manual corrections/workarounds, but it works. Cheers. – Vivi Jan 13 '13 at 04:05
  • 1
    Playing with @PeterGrill's advice, I find \begin{align*} \color{blue}{}a - b &\color{blue}{}= c\ a-c & b \end{align*} to work. There were previous posts about \color{} adding padding but I can't seem to find them. – hpesoj626 Jan 13 '13 at 04:10
  • @Vivi: I agree. I remember David Carlisle had commented about \color effecting spacing somewhere. – Peter Grill Jan 13 '13 at 04:10
  • 1
    @hpesoj626: Interesting that in align things are ok without the additional grouping, but not in equation. – Peter Grill Jan 13 '13 at 04:14
  • @PeterGrill Yes. I can verify that. – hpesoj626 Jan 13 '13 at 04:15
  • @PeterGrill it puzzles me, too... – Vivi Jan 13 '13 at 04:17
  • @Vivi Here's the dupe that I answered re tabularx http://tex.stackexchange.com/questions/78633/color-lines-tabularx-add-padding. this one was the duped post http://tex.stackexchange.com/questions/31547/color-changes-cell-height-in-tabular – hpesoj626 Jan 13 '13 at 04:18
  • @Vivi If you are interested you can look at a related question about coloring math symbols http://tex.stackexchange.com/questions/21598/how-to-color-math-symbols. And here is a question from latex-community.org regarding globally setting the color for equations: http://www.latex-community.org/forum/viewtopic.php?f=46&t=2834 – hpesoj626 Jan 13 '13 at 04:36
  • 2
    I'm not convinced this has anything to do with color. Two math environments should not be placed 'back to back' like this- you should use gather*. When you use gather*, the spacing is the same in both – cmhughes Jan 13 '13 at 04:45
  • @cmhughes: Yep, adding some text in between the two display mode equations seems to work just fine with the MWE as above. – Peter Grill Jan 13 '13 at 04:52
  • @cmhughes I hadn't heard of gather before. I will check it out. I am also happy for this question to be closed given the related questions and the many suggestions given. – Vivi Jan 13 '13 at 05:09
  • 1
    @hpesoj626 Great! I believe this then qualifies my question as a duplicate, so feel free to close this one. – Vivi Jan 13 '13 at 05:11
  • @cmhughes But gather doesn't provide alignment?! I want the first equation to be independent and not aligned with the set of equations that comes afterwards, which should in turn be aligned with each other. Gather does not allow one to put equation and align inside it, does it? – Vivi Jan 13 '13 at 05:43
  • 2
    @Vivi I won't vote to close this one yet. I think this one merits a definite answer since this deals with a more specific setting. Eventually though, it might get closed. Answering this in the linked post is not within context, I think. Although it was mentioned there that one needs to scope the effect of \color inside {} . – hpesoj626 Jan 13 '13 at 05:43
  • how about this: \begin{gather*} \color{blue} a = b + c\\begin{align*} a - b &= c\ a-c & b \end{align*} \end{gather*} – cmhughes Jan 13 '13 at 05:48
  • @cmhughes It seems to mess up with the alignment. I tried with the equations I am typesetting and look at the difference: https://dl.dropbox.com/u/1885087/cmhughes.png – Vivi Jan 13 '13 at 05:57
  • 2
    @cmhughes: You shouldn't use align* as the inner environment; I didn't even know that this is possible. With aligned as the inner environment, everything works nicely, Vivi. – Hendrik Vogt Jan 13 '13 at 08:39

2 Answers2

16

Oh. It's an AMS bug, the alignment is not color safe . Never noticed that before:-) In that case you need explicit grouping (use begingroup rather than bgroup or { as it's rather less intrusive into math spacing)

enter image description here

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

It's actually a bit harsh to call it an AMS bug, the same effect is seen with \[ in LaTeX however rather than grouping each case, this is probably the correct fix (anywhere in the preamble after loading amsmath. I'll 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}!!
David Carlisle
  • 757,742
  • Aren't \patchcmd{\mathdisplay}{$$}{$$\begingroup}{}{} and \patchcmd{\endmathdisplay}{$$}{\endgroup$$}{}{} easier? ;-) – egreg Aug 14 '15 at 20:49
5

having found david's inquiry about this while cleaning out old files, i have finally investigated.

while there is indeed extra (unwanted) space when two display environments are adjacent, and this space increases when color is added, the amsmath display environments are not designed to be used in this manner. all display material between two blocks of text should instead be placed into a single environment, which can then be subdivided into "subsidiary" nested environments as appropriate. (someone mentioned that in a comment, but it didn't make it into david's answer.) here is an expanded example showing the desired outcome in the last column. (i've changed blue to red because blue tends to disappear on my screen.)

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

output of example code

so is this a bug in amsmath? if this extra space appears when a "simple" one-environment instance comes between two text blocks, then it is definitely a bug. if there is no extra space in that context, then it's not a bug, since the behavior is what amsmath is designed to do. here's the example to decide the matter.

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

output of example code

no extra space. thus i interpret the problem as misuse of the environments, and not a bug.