7

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://i.stack.imgur.com/DrWpz.jpg

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://i.stack.imgur.com/GNHjm.jpg (no other change except color)

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

\title{Generative Adversarial Nets}

\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

  • 2
    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 01 '19 at 16:01
  • 2
    Is it a single section, or all sections that you want to have coloured? – Bernard Aug 01 '19 at 16:02
  • 3
    texdoc color see footnote on page 6 of grfguide. – David Carlisle Aug 01 '19 at 16:28
  • it is best to treat colour like a font change and set the colour for the heading as you would set the font, either modifying your class setup or using a package like titlesec – David Carlisle Aug 01 '19 at 16:31
  • @Bernard, I want to do one section at a time. By coloring different elements of a latex file and taking the diff of the rendered pdf with the original pdf, I get the ground truth locations of those elements. This can help me structurally annotate a pdf. – crazydiamond Aug 01 '19 at 16:48
  • The issue should be a small vertical offset but your image shows the first paragraph gaining and losing indent which is presumably a different issue entirely – David Carlisle Aug 01 '19 at 18:11
  • \showoutput confirms there are no spacing differences if I delete \color{red} from this example. – David Carlisle Aug 01 '19 at 18:14
  • @DavidCarlisle, the purpose of my experiment is to obtain the location of the various text items (like section, item, etc). So, can I change some aspect of the text (I was thinking color but it could be anything), so that when I take the diff of the changed pdf and the original pdf, it gives me the exact location of the text. – crazydiamond Aug 01 '19 at 18:22
  • 1
    @crazydiamond yes but it would have been easier to answer if the posted document displayed the problem you are asking about. – David Carlisle Aug 01 '19 at 18:31
  • 1
    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 01 '19 at 18:33
  • This is fascinating. So could you try this, but wrap the \section{Introduction} instead of \section{Next Section} – crazydiamond Aug 01 '19 at 18:40
  • It'd be helpful if you could upload your images to SE's image server. – Nat Aug 02 '19 at 10:51
  • @Nat, it's a gif and I wasn't able to – crazydiamond Aug 02 '19 at 14:07

2 Answers2

12

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.

enter image description here

\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}
Mico
  • 506,678
8

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
\title{Generative Adversarial Nets}

\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
\title{Generative Adversarial Nets}

\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
\title{Generative Adversarial Nets}

\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}
Sigur
  • 37,330
David Carlisle
  • 757,742