[texhax] Problem with color package

jfbu jfbu at free.fr
Fri Dec 13 11:53:58 CET 2013


le 13/12/2013 09:19 selon jfbu:
> 
> 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
> 

It is interesting to investigate more exactly what happens
without the added braces. 

Consider

\documentclass{article}
\usepackage{color}
\begin{document}
\showoutput
\setbox0\hbox{\color{red} Test1}\box0 Test2
\end{document}

processed by latex (not pdflatex) to use the dvips driver
Here is from the log:

the color pop comes before the color push.

The reason is that \color{red} issued an \aftergroup
but this \aftergroup comes from the closing brace of
the box. Hence the color pop command is issued
*before* the actual insertion of \box0 in the main
vertical list (here we are in vertical mode)

At the bottom we find some push/pop from the \normalcolor
I guess which accompany with the page number typesetting

..\vbox(550.0+0.0)x345.0, glue set 527.9436fil
...\write-{}
...\special{color pop}     <----- this comes from \color{red} in the box *definition*!!!
...\glue(\topskip) 3.16669
...\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
...\glue(\parskip) 0.0 plus 1.0
...\glue(\baselineskip) 5.16669
...\hbox(6.83331+0.0)x345.0, glue set 306.33331fil
....\hbox(0.0+0.0)x15.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 2
....\penalty 10000
....\glue(\parfillskip) 0.0 plus 1.0fil
....\glue(\rightskip) 0.0
...\glue 0.0 plus 1.0fil
...\glue 0.0
...\glue 0.0 plus 0.0001fil
..\glue(\baselineskip) 23.55556
..\hbox(6.44444+0.0)x345.0
...\special{color push  Black}
...\hbox(6.44444+0.0)x345.0, glue set 170.0fil
....\glue 0.0 plus 1.0fil
....\OT1/cmr/m/n/10 1
....\glue 0.0 plus 1.0fil
...\special{color pop}


On the other hand, from 

\documentclass{article}
\usepackage{color}
\begin{document}
\showoutput
\setbox0\hbox{{\color{red} Test1}}\box0 Test2
\end{document}

we get the color pop in a correct position, as
explained earlier

..\vbox(550.0+0.0)x345.0, glue set 527.9436fil
...\write-{}
...\glue(\topskip) 3.16669
...\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} <--- thanks to the added brace pair, this is together with the box contents.
...\glue(\parskip) 0.0 plus 1.0
...\glue(\baselineskip) 5.16669
...\hbox(6.83331+0.0)x345.0, glue set 306.33331fil
....\hbox(0.0+0.0)x15.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 2
....\penalty 10000
....\glue(\parfillskip) 0.0 plus 1.0fil
....\glue(\rightskip) 0.0
...\glue 0.0 plus 1.0fil
...\glue 0.0
...\glue 0.0 plus 0.0001fil
..\glue(\baselineskip) 23.55556
..\hbox(6.44444+0.0)x345.0
...\special{color push  Black}
...\hbox(6.44444+0.0)x345.0, glue set 170.0fil
....\glue 0.0 plus 1.0fil
....\OT1/cmr/m/n/10 1
....\glue 0.0 plus 1.0fil
...\special{color pop}

> 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
> 
> 
> _______________________________________________
> TeX FAQ: http://www.tex.ac.uk/faq
> Mailing list archives: http://tug.org/pipermail/texhax/
> More links: http://tug.org/begin.html
> 
> Automated subscription management: http://tug.org/mailman/listinfo/texhax
> Human mailing list managers: postmaster at tug.org





More information about the texhax mailing list