# [Xy-pic] Really weird behavior

Daniel Müllner daniel at danifold.net
Wed Dec 11 13:19:39 CET 2013

Hi Michael,

the issue you mentioned is mostly independent of Xy-pic and is only
related to it as Xy-pic typesets everything in boxes.

Look at the following code:

{\setbox0\hbox{\color{red}Test1}\box0} Test2

This prints "Test1 Test2", all in red. The reason is that TeX's grouping
mechanism doesn't mix well with the graphics state model (which is a
stack) of Postscript and the PDF. Here is what happens:

TeX parses the \hbox{\color{red}Test}, sees the \color{red} inside the
\hbox (which also implies a local scope) and saves the content to box 0.
Upon leaving the \hbox, TeX leaves a group. The color package was
designed so that \color has local scope, so I suppose there is an
\aftergroup somewhere in the expansion of \color. This \aftergroup is
now triggered before anything is typeset. Only after that, the content
of the box is typeset with the \box0. As a result, TeX writes the
following into the DVI file, in this order:

* Change color to black
* Change color to red
* Typeset "Test1"
* Typeset "Test2"

And here you have the effect that puzzled you...

For the record:

1) pdfTeX has a color stack feature, see Section 7.19 of "The pdfTex
manual".

2) If you publish a package, you are very welcome to replace every
\hbox{...} and \vbox{...} by \hbox{{...}} resp. \vbox{{...}}, which
solves the color problem. As a user of an existing package, I don't have
a good solution for you. You can enclose every \color command in a TeX
group, of course, if that's feasible, but then you may prefer \textcolor
anyway.

Regards,

Daniel