# Why does Latex make a small adjustment when I change section color [duplicate]

I want to change the color of a section of a document. The command to do so is

{\color{red} \section{Introduction}}


(Merely doing \section{\color{Introduction}} does not change the color of the section number).

So when I carry out the command above, it slightly adjusts the vertical space of all of the text in the entire document. This can be observed in the gif https://imgur.com/a/RLpPLU6

So, how do I carry out color changes without changing the vertical spacing, etc ?

Edit : A minimal code snippet and the effect of changing color can be seen here https://imgur.com/a/pI3NmuX (no other change except color)

\documentclass{article} % For LaTeX2e
\usepackage{xcolor}

\begin{document}

\section{Introduction}

The promise of deep learning is to discover rich, hierarchical models that represent
probability distributions over the kinds of data encountered in artificial intelligence applications,
such as natural images, audio waveforms containing speech, and symbols in natural language corpora.

{\color{red} \section{Next Section}}

This framework can yield specific training algorithms for many kinds of model and optimization
algorithm. In this article, we explore the special case when the generative model generates
samples by passing random noise through a multilayer perceptron, and the discriminative model
is also a multilayer perceptron. We refer to this special case as {\em adversarial nets}.
In this case, we can train both models using only
the highly successful backpropagation and dropout algorithms
and sample from the generative
model using only forward propagation. No approximate inference or Markov chains are necessary.

\end{document}



I previously thought the culprit was the nips14submit_e (which can be found here (https://pastebin.com/aw96SzXB)), but it doesn't seem to be

• Welcome to TeX.se. Please don't post a code fragment. Instead make a minimal compilable document that shows the problem (i.e., not your whole paper). This is not the correct way to make a section title coloured, but without knowing what document class you are using it's hard to say what's going on. – Alan Munn Aug 1 '19 at 16:01
• Is it a single section, or all sections that you want to have coloured? – Bernard Aug 1 '19 at 16:02
• texdoc color see footnote on page 6 of grfguide. – David Carlisle Aug 1 '19 at 16:28
• @crazydiamond yes but it would have been easier to answer if the posted document displayed the problem you are asking about. – David Carlisle Aug 1 '19 at 18:31
• tex would give you the exact coordinates of the text without needing to post process the pdf, if that is what you want (\pdfsavepos) – David Carlisle Aug 1 '19 at 18:33

As you can check with \showoutput there are no spacing differences in the supplied example in the question with and without \color{red} so it isn't an example of this problem...

However the issue can occur (and is shown in the first posted gif with the centred abstract).

A real example is

\documentclass{article} % For LaTeX2e
\usepackage{xcolor}
\showoutput
\showboxdepth3

\begin{document}

\begin{center}
zzzzz
\end{center}

{%\color{red}
\section{Next Section}}

This framework can yield specific training algorithms for many kinds of model and optimization
algorithm. In this article, we explore the special case when the generative model generates
samples by passing random noise through a multilayer perceptron, and the discriminative model
is also a multilayer perceptron. We refer to this special case as {\em adversarial nets}.
In this case, we can train both models using only
the highly successful backpropagation and dropout algorithms
and sample from the generative
model using only forward propagation. No approximate inference or Markov chains are necessary.

\end{document}


If you uncomment the colour command you get additional space before the heading as the colour whatsit node prevents the section command "seeing" the vertical space already added by the \end{center} and so it adds its full specified amount rather than merging the spaces specified by \end{center} and \section.

The trick is to do colour changes as far as possible in horizontal not vertical mode, so for example this works

\documentclass{article} % For LaTeX2e
\usepackage{xcolor}
\showoutput
\showboxdepth3

\begin{document}
\color{red}

\begin{center}
\textcolor{black}{zzzzz}
\end{center}

\section{Next Section}

\mbox{}\color{black}This framework can yield specific training algorithms for many kinds of model and optimization
algorithm. In this article, we explore the special case when the generative model generates
samples by passing random noise through a multilayer perceptron, and the discriminative model
is also a multilayer perceptron. We refer to this special case as {\em adversarial nets}.
In this case, we can train both models using only
the highly successful backpropagation and dropout algorithms
and sample from the generative
model using only forward propagation. No approximate inference or Markov chains are necessary.

\end{document}


Or perhaps nicer the following which just locally tacks the color on to the \Large used by the section heading

\documentclass{article} % For LaTeX2e
\usepackage{xcolor}
\showoutput
\showboxdepth3
\let\realLarge\Large

\begin{document}

\begin{center}
zzzzz
\end{center}

%\def\Large{\realLarge\color{red}}
\section{Next Section}
%\let\Large\realLarge

This framework can yield specific training algorithms for many kinds of model and optimization
algorithm. In this article, we explore the special case when the generative model generates
samples by passing random noise through a multilayer perceptron, and the discriminative model
is also a multilayer perceptron. We refer to this special case as {\em adversarial nets}.
In this case, we can train both models using only
the highly successful backpropagation and dropout algorithms
and sample from the generative
model using only forward propagation. No approximate inference or Markov chains are necessary.

\end{document}


I want to change the color of a section of a document ... one section at a time.

If you prefer using high-level commands and letting LaTeX perform most of the hard work for you "behind the scenes", while not having to be concerned with subtle positional shifts, I recommend loading the sectsty package and using its \sectionfont command. As the following example shows, one can employ \sectionfont{\color{...}} multiple times.

\documentclass{article}
\usepackage{sectsty} % for "\sectionfont" macro
\usepackage[dvipsnames]{xcolor}

\begin{document}

\sectionfont{\color{RubineRed}}
\section{Introduction}

\sectionfont{\color{Dandelion}}
\section{Literature}

\sectionfont{\color{Aquamarine}}
\section{Data}

\sectionfont{\color{PineGreen}}
\section{Methods}

\sectionfont{\color{NavyBlue}}
\section{Results}

\sectionfont{\color{Violet}}
\section{Conclusion}

\end{document}