[texhax] Problem with color package

jfbu jfbu at free.fr
Fri Dec 13 09:19:50 CET 2013


Le 13 déc. 2013 à 02:08, Michael Barr <barr at math.mcgill.ca> a écrit :

> It is not surprising that using the color package, the word Test2 prints in red in the example:
> \setbox0\hbox{\color{red} Test1}\box0 Test2
> 
> But it seems like a failure of the tex grouping mechanism when the same happens in this example:
> 
> {\setbox0\hbox{\color{red} Test1}\box0} Test2
> 
> Adding a group in another place does not improve things:
> 
> \setbox0\hbox{{\color{red} Test1}}\box0 Test2
> 
> but adding both sets of groupings does:
> 
> {\setbox0\hbox{{\color{red} Test1}}\box0} Test2
> 
> I got a partial (but not entirely convincing) explanation of this from Daniel Müllner.  But is there any way to force \color to confine itself to being inside a group.  And why does it need two levels of grouping to get it to work?
> 
> The following also work:
> \setbox0\hbox{{{\color{red} Test1}}}\box0 Test2 {{{\setbox0\hbox{\color{red} Test1}\box0\relax}}} Test2
> 
> Michael
> 

Hi, color is driver dependent, but for example in the case of
compilation by latex, the automatically chosen driver is `dvips'
and one gets:

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

> \box0=
\hbox(6.83331+0.0)x23.66669
.\special{color push rgb 1 0 0}
.\OT1/cmr/m/n/10 T
.\kern-0.83334
.\OT1/cmr/m/n/10 e
.\OT1/cmr/m/n/10 s
.\OT1/cmr/m/n/10 t
.\OT1/cmr/m/n/10 1

[no \special{color pop}]

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

> \box0=
\hbox(6.83331+0.0)x23.66669
.\special{color push rgb 1 0 0}
.\OT1/cmr/m/n/10 T
.\kern-0.83334
.\OT1/cmr/m/n/10 e
.\OT1/cmr/m/n/10 s
.\OT1/cmr/m/n/10 t
.\OT1/cmr/m/n/10 1
.\special{color pop}

What happens is that \color issues internally \set at color
which itself is defined in the driver file but normally
should issue a command to set the color and then
another one to unset it and the position where this
unset will happen is after the current group closes
(uses \aftergroup)

in the first case above there was no closing brace
in the constructed box, so no group closing was found
hence the \special{color pop} was not issued, hence
the color leak

in the second case it was.

And actually in my test with latex
\setbox0\hbox{{\color{red} Test1}}\showbox0\box0 Test2
has no color leak.

Then there are other issues related to how the "whatsit"'s
as defined by the \special commands are managed

LaTeX has \sbox which has additional opening and closing of group 
for the mechanism above to bring color safety (for drivers
having a color stack)

Regards
Jean-Francois




More information about the texhax mailing list