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

  • This question is very similar to: http://tex.stackexchange.com/q/73938/99584 – Brian Kubera Mar 01 '16 at 06:27
  • I prefere to use the everypage package for headers and footers. See http://tex.stackexchange.com/questions/276358/text-on-background-image-footer-and-header/276453?s=8|0.0651#276453 for example. – John Kormylo Mar 01 '16 at 22:37
  • @ John-Kormylo, Thank you for bringing up the everpage package and that question. I will look into the discussion there. – Brian Kubera Mar 02 '16 at 04:21

2 Answers2

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>}}
Werner
  • 603,163
  • Can you provide any insight on why \AtBeginShipoutAddToBox, or something similar, is not also required for \AtBeginShipoutFirst{} in order to get stuff added to the first page? – Brian Kubera Mar 02 '16 at 04: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 02 '16 at 04:42
  • Thank you, I could not make sense of what was going on without your explanation. – Brian Kubera Mar 02 '16 at 05: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. – Satyendra Rana Dec 03 '19 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 03 '19 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.

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