4

I want to change the all the equation color, so I redefined the equation environment code, but there is one blank line after the equation, my codes as following:

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

Codes without \color{red} as following:

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

As the following image shows, when \color{red} is added there is extra space under the red equation. I hope someone can tell me why.

enter image description here

  • 1
    Remove the blank line following \end{equation} in case 1. Or at least replace it with \hspace{\parindent} if a new par is wanted. – Steven B. Segletes Oct 17 '17 at 14:17
  • @ Steven B. Segletes These two cases both have blank line following \end{equation}, so I want to know why different results. – zongxian Oct 17 '17 at 14:22
  • @Andrew I see and understand that there is a discrepancy and that is the topic of the question. I answered, perhaps, the alternative question...once you note the discrepancy, how to remedy it. – Steven B. Segletes Oct 17 '17 at 16:57
  • Related (perhaps even a duplicate): How does changing colour affect spacing? – Ian Thompson Oct 17 '17 at 19:34
  • Hi @StevenB.Segletes, sorry for being a pedant, but the whole point of my comment was to highlight that (a) your proposed fix does not solve the problem because your output is different, and (b) that your comment does not explain why the \color{red} command is causing the difference in spacing. – Andrew Oct 18 '17 at 8:21
5

If you simplify your example it is easier to see where the space is coming from (and the fact that it is a line of a paragraph, not a space)

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

produces

enter image description here

Looking after the log you see that the first equation and the following paragraph "2" is

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

so you just have the normal space before a paragraph plus \belowdisplayshortskip space.

Compare with the the second equation and the paragraph "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

Here you see that the paragraph that started "3" and was interrupted by the math display has a final (white) line that just contains the colour restore.

The colour restore is not just a switch of state like restoring a font at the end of a group it is a real node in the list (much like an empty box, technically a whatsit node) and as there is another node in the list display math returns first to horizontal mode, and then the blank line ends the paragraph so you get a final all-white line of paragraph.

Note this is not vertical space and would not for example be dropped at a page break.

If you insert the colour nodes in safer places then

enter image description here

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

Seems that an \egroup or something similar during \endmathdisplay causes the problem.

To confirm test the code (that coloring using \textcolor without the unwanted space):

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

If you use the same code starting end ending in the commented start and end you will see the problem coming back.

So, there (in \endmathdisplay) is hidden the reason of your problem.

  • colouring in this way doesn't really work though, it does not colour the equation number (and it's colouring the equation number that is causing the problem) – David Carlisle Oct 18 '17 at 8:49
  • \textcolor{blue}\bgroup is \textcolor{blue}{\bgroup} the fact that that does anything at all rather than generate an error is an accident of the implementation and not guaranteed to work if the implementation ever changes. – David Carlisle Oct 18 '17 at 8:50
  • @DavidCarlisle, 1:My answer is just a debugging way and a step for finding the solution. I could not debug with your way due to less experience on understanding log files. 2: At the moment if you test the text: Here is a text \textcolor{blue}\bgroup here it is blue \egroup and black again you will see that it works. I know it is not a suggested way and I have already exclude such possibilities in my way (tex.stackexchange.com/questions/383631/…). The comments below the question made me to look for a way to answer "why". – koleygr Oct 18 '17 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.