1

Consider the following LaTeX manuscript, which sets the document color to red in the preamble, and then proceeds to create a text box, use it, and then ship it out.

\documentclass{article}
\usepackage{xcolor}
\color{red}
\begin{document}
    \newbox\mybox%
    \setbox\mybox\hbox{Hello, world!}%
    \copy\mybox%
    \shipout\box\mybox%
\end{document}

The resulting pdf document contains two pages. In the first one the box's text is rendered in black:

Text in black

whereas in the second one it is rendered in red:

Text in red

I presume that the first page was generated by the \shipout command.

  1. Why isn't the text color red in the shipped-out page?
  2. Why is the shipped-out page the first page of the document, when the \shipout command was given after the box was used? Shouldn't the shipped-out page appear after the page on which the box was used?
7
  • latex goes to a lot of effort to make colour work consistently across its documented commands, if you use primitive assignments such as \setbox then colour won't work, or you could crash your printer or ... Commented Aug 7, 2017 at 17:09
  • @DavidCarlisle: Point taken. Nonetheless, I would like to know the reason for the observed behavior in this particular case.
    – Evan Aad
    Commented Aug 7, 2017 at 17:27
  • as Heiko says in his answer you just have text in the box nothing storing colour state, compare \showbox\mybox if set with \setbox or \sbox and the difference will be clear Commented Aug 7, 2017 at 17:37
  • @DavidCarlisle: But the box has no color state when it is \copy-ed either, and yet it displays as red.
    – Evan Aad
    Commented Aug 7, 2017 at 17:47
  • if you copy a box it has the same same contents as the box you copy. Commented Aug 7, 2017 at 18:09

1 Answer 1

4

Ad 1.

\setbox\mybox\hbox{Hello, world!}%
\shipout\box\mybox

The box does not contain any color specials. The default color at the start of a PDF page is black, thus the result is black.

As a thumb or rule, use LaTeX commands to set boxes:

\documentclass{article}
\usepackage{xcolor}
\color{red}
\begin{document}
    \newbox\mybox%
    \sbox\mybox{Hello, world!}%
    \usebox\mybox%
    \shipout\box\mybox%
\end{document}

Then, \sbox sets the current color at the start of the box and the result is red in both cases.

Ad 2.

\shipout acts immediately. Regular pages are output by the output routine, that is called later, when the page is full, for example.

3
  • Thanks. I appreciate your explanation of the situation described in my 2nd question, as well as your workaround for the situation described in my 1st question. Could you please also explain the situation described in my 1st question?
    – Evan Aad
    Commented Aug 7, 2017 at 5:10
  • Why is the default color black, if \color{red} was specified in the preamble?
    – Evan Aad
    Commented Aug 7, 2017 at 17:49
  • @EvanAad It's the default of the PDF format that does not know anything about TeX. Commented Aug 7, 2017 at 18:39

You must log in to answer this question.

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