2

I have been attempting to use \AtBeginShipout and tikz to draw header and footer art as suggested in solution for creating newsletter art by user Gonzalo Medina.

One problem that has been encounter is that an extra page containing only the artwork is always produced at the end of the document. In the example below I expect two pages of output, but there are three. Why is this? What is the best way to prevent it?

Example using \AtBeginShipout with \tikzpicture produces an extra page:

\documentclass{article}

\usepackage{tikz}
\usepackage{atbegshi}
\usepackage{lipsum}

\newcommand\DrawSquare{
\begin{tikzpicture}[overlay]
\draw[fill=pink]
    (0,0) rectangle (10,-10);
\end{tikzpicture}
}

\AtBeginShipout{\DrawSquare}

\begin{document}
\lipsum[1-9]
\end{document}

Thumbnail of Output:

Output

2

When you use

\AtBeginShipout{<stuff>}

<stuff> is inserted into the input stream. However, at that point, the entire "current page" has already been constructed and is contained inside the box \AtBeginShipoutBox. If you leave everything as-is, the "current page" is shipped out, after which <stuff> is processed.

In your instance, an overlay box is added to the input stream but doesn't form part of the "current page" to be shipped out.

In order to add to the "current page" that will be shipped out, you need to use the combination

\AtBeginShipout{\AtBeginShipoutAddToBox{<stuff>}}

or

\AtBeginShipout{\AtBeginShipoutUpperLeft{<stuff>}}
  • Can you provide any insight on why \AtBeginShipoutAddToBox, or something similar, is not also required for \AtBeginShipoutFirst{<stuff>} in order to get stuff added to the first page? – Brian Kubera Mar 2 '16 at 4:16
  • @BrianKubera: From the atbegshi documentation: "AtBeginShipoutFirst{<code>} ... The <code> is directly executed in a \vbox that is put at the beginning of the output page. Dealing with the output box \AtBeginShipoutBox is not necessary and not permitted here." – Werner Mar 2 '16 at 4:42
  • Thank you, I could not make sense of what was going on without your explanation. – Brian Kubera Mar 2 '16 at 5:19
1

Replacing:

\AtBeginShipout{\DrawSquare}

With:

\AtBeginShipout{\AtBeginShipoutAddToBox{\DrawSquare}}

Solves the problem. The output consists of two pages of text, both pages contain a pink square. However, I can't explain why the problem occurs or why this solution works.

This solution was found here.

  • If "this solution was found [elsewhere]", could this question be considered a duplicate? – Werner Mar 1 '16 at 6:32
  • Yes, I noted the similarity in a comment to the question. However, the why is not explained here or in the other question. – Brian Kubera Mar 1 '16 at 6:46
  • @BrianKubera You may read the documentation of atbegshi package (p.3). – Paul Gaborit Mar 1 '16 at 7:20
  • I am failing to understand the difference in behavior of \AtBeginShipout and \AtBeginShipoutFirst. The documentation describes them as both "a hook that executes for" appropriate page(s). If I use \AtBeginShipoutFirst{\DrawSquare} in TeX from the question , the first page will be drawn on, as I would expect. The same is not true of \AtBeginShipout{\DrawSquare}, the drawing won't be applied until the next page, skipping the first page and creating an additional page. This is not the behavior I expect from the documentation and seems inconsistent with the behavior of \AtBeginShipoutFirst. – Brian Kubera Mar 1 '16 at 7:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.