3

I'm getting some strange behaviour when using the color package at paragraph and subparagraph level. See the code snippet below:

\usepackage[usenames,dvips]{color}

\section{A}
{\color{Green} \itshape Hello.}

\subsection{B}
{\color{Green} \itshape Hello.}

\subsubsection{C}
{\color{Green} \itshape Hello.}

\paragraph{D}
{\color{Green} \itshape Hello.}

\subparagraph{E}
{\color{Green} \itshape Hello.}

The color switch is inside a group following the heading title, but is being incorrectly used within the header title at paragraph and subparagraph level. That is, D and E are shown in green as well as the 'Hello' text (but A, B and C aren't).

Note that the \itshape switch does not have the same behaviour, so I suspect this is a bug/feature of the internals of the color package. Can anyone shed any light on this? Am I missing something?

I'm using the TeX Live 2009 version as in the standard Ubuntu 10.04 Lucid package.

EDIT: Just to be clear. I know that, in general, I can get round it using the non-switch equivalent \textcolor (but any alternatives as suggested by David Carlisle are still useful). I'm actually using this within a 'style' \newcommand definition, so I need the switch variant to colour multiple paragraphs. (There are lots of design-level ways I can avoid the problem of course, like not using styles with colour; the question isn't about that!)

I guess I'm primarily interested in whether such types of 'feature' are relatively common, limited to the specifics of using colours, etc. (and the general under-the-covers reasons why), so I can make informed decisions on what LaTeX constructs to use when, and which are likely to cause me more headaches than they're worth.

2
  • The package documentation does say: "Some drivers do not maintain a special 'colour stack'. These drivers are likely to get confused if you nest colour changes, or use colours in floating environments." I'm not doing either of these, but maybe it's a similar driver issue? Oct 8, 2012 at 11:12
  • 2
    Just tested and it does the same if using the xcolor package instead. Maybe that shares most of the internals of color anyway... Oct 8, 2012 at 11:20

1 Answer 1

4

Clearly it has to be a feature not a bug:-)

You can use

 \leavevmode{\color{Green} \itshape Hello.\par}

Making a colour switch in vertical mode is tricky (and just after a heading is one of the trickier places to be in LaTeX). \textcolor inserts \leavevmode itself to avoid these problems but for the general \color switch that caused more problems than it solved. So an alternative is to use

 \textcolor{Green}{\itshape Hello.}

If neither of those is convenient you need a command that delays making the color switch until it is safe:

\documentclass{article}


\usepackage[usenames,dvipsnames]{color}


\DeclareRobustCommand\zcolor{\ifvmode
 \expandafter\def\expandafter\tmp\expandafter{\the\everypar}%
 \ifx\tmp\empty
 \else
  \expandafter\expandafter\expandafter  \leavevmode
  \fi
 \fi
\color
 }

\begin{document}





\section{A}
{\zcolor{Green} \itshape Hello.}

\subsection{B}
{\zcolor{Green} \itshape Hello.}

\subsubsection{C}
{\zcolor{Green} \itshape Hello.}

\paragraph{D}
{\zcolor{Green} \itshape Hello.}

\subparagraph{E}
{\zcolor{Green} \itshape Hello.}

\end{document}

To address some of the comments:

LaTeX (or if you prefer, I) tried to make color act in the same way as font changes but it implemented completely font is a property of each character in a horizontal list, but color changes are not a property of the character the point that colour changes are marked by whatsit nodes in the list. This can affect spacing in several ways as noted in grfguide. But in particular

   \parbox[t]{\color{red} abc} 

will align on the first node in the vertical list which is the colour node above the first line whereas

 \parbox[t]{\mbox{}\color{red} abc}

now the color node is in the first line not in the vertical list so the top alignment aligns with the baseline of the first row of text as it would without the colour.

The other place that spacing is affected (and this affects section headings) is that LaTeX often uses \addvspace or similar macros that try to check whether any vertical space has just been added to the list and adjust the space to be added in that case (so two adjacent displays or two adjacent headings only get the larger of the two vertical spaces not the sum of two) but a whatsit node is an unremovable node type so once it has been added to a vertical list it is impossible for the list to be deconstructed past that point and for any vertical spaces or penalties already added to be detected from within TeX. Thus display \color... display typically gets anomalous extra spacing that is not there without the colour as the second display can not see the first.

5
  • Useful detail; thanks. I should have said in the question that I know how to work around it with \textcolor, but it's actually as part of a \newcommand (effectively a 'style'), where I need it to colour multiple paragraphs. (I'll edit the question now.) Oct 8, 2012 at 13:17
  • BTW, does your first sentence have an implicit smiley at the end, or is that meant seriously (along the lines of 'everything's working around the limitations of TeX and LaTeX under the covers, so there will always be some bug-like features')? Just out of interest! Oct 8, 2012 at 13:20
  • sorry there is an implicit smiley (although the second part of your question is a true statement) some of the regulars here claim that I'm only here to field bug reports but I claim my packages only ever have undocumented features, not bugs Oct 8, 2012 at 13:47
  • But now readers will be confused because the implicit is now explicit :-) Re 'features' and 'bugs': yes, it's amusing how many LaTeX package documents talk about 'This is a feature, not a bug!' (e.g. the marginnote one I just read). That's testament to the complexity of creating packages that avoid all side-effects I guess (and one of the main design hurdles LaTeX has to overcome to avoid, similarly to Linux, the contra position that its power sometimes sacrifices 'it just works'; a problem stuff like memoir and LaTeX 3 are trying to solve). Oct 8, 2012 at 16:07
  • Accepting this answer as the closest I think I'm going to get (and with a lot of effort put into it). @david-carlisle: I'd still like to understand a little more about what 'Making a colour switch in vertical mode is tricky (and just after a heading is one of the trickier places to be in LaTeX)' really means: why is this tricky and how much of the trickiness is because colours are being used? Oct 18, 2012 at 14:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .