# [texhax] Changing font color of a saved box of text - is it possible?

Ivan Griffin ivan at skynet.ie
Tue Sep 4 13:42:26 CEST 2012


On Tue, 4 Sep 2012, Heiko Oberdiek wrote:

>> I suspect not (as the typesetting/macro expansion is already
>> complete within the box),
>
> LaTeX puts the current color inside the box, thus that the color
> does not change if used in different color contexts. It is a feature
> that the color of a box does not change.
>

That's what I figured.

>>
>> What I see is that "This is test 1" is typeset in black, whereas
>> "This is test 2" is typeset in red.
>
> Why can't you use a simple macro?
>
>  \newcommand*{\macroFoo}{This is test1.}
>  \color{red}
>  ...
>  \macroFoo
>  ...
>

What I'm actually doing is fiddling with a bibtex .bst file and some
custom fields to highlight certain entries in the bibliography.

So, rather than output the formatting code for the bibtex entry (@book
usually) immediately, I was storing it in a box, and then applying some
formatting to it based on the value of a custom field, and outputting it
at the end.

It is difficult to use a macro as my formatting code is spread across a
number of different formatting elements in the .bst file.

> If you need boxes, this can be done using the low level TeX interface:
>
> \setbox\boxFoo=\hbox{%
>  \begingroup
>    This is test 1.%
>  \endgroup
> }
>
> The group with \begingroup and \endgroup is a protection for the case that
> color commands are used inside the text. Color resetting is done outside
> the current group and would slip out of the box without the additional
> group.
>

Thanks, this avenue of attack is intriguing.

> Yours sincerely
>  Heiko Oberdiek
> _______________________________________________

Best Regards,
Ivan