6

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

3

2 Answers 2

4

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>}}
5
  • 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? Mar 2, 2016 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, 2016 at 4:42
  • Thank you, I could not make sense of what was going on without your explanation. Mar 2, 2016 at 5:19
  • This solution is "partially" working since now the extra pages are gone but i am missing my header and footer data on even numbered pages (I am making even pages blank by using \cleardoublepage or \cleartorecto). I am using memoir class. Can somebody please help. Dec 3, 2019 at 11:22
  • @SatyendraRana: How about posting a minimal working example showing your problem? You could also consider posting a follow-up question, pointing back to this one as reference.
    – Werner
    Dec 3, 2019 at 18:02
2

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.

4
  • If "this solution was found [elsewhere]", could this question be considered a duplicate?
    – Werner
    Mar 1, 2016 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. Mar 1, 2016 at 6:46
  • @BrianKubera You may read the documentation of atbegshi package (p.3). Mar 1, 2016 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. Mar 1, 2016 at 7:44

You must log in to answer this question.

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