tikz and atbegshi

1. The following doesn't show watermark on page 1. How can I get watermark on page 1?

2. \\ has no effect. How can I force line break?

\documentclass{article}
\usepackage{lipsum}
\usepackage{graphicx}
\usepackage{atbegshi}
\usepackage{tikz}
\AtBeginShipoutInit
\AtBeginShipout{%
\tikz[overlay,remember picture]{%
\path[use as bounding box](0,0);
\node [gray!55,inner sep=0pt,rotate=45] at
([shift={(.5\paperwidth,-.5\paperheight)}]current page.north west){%
\scalebox{3}{%
\fontseries{b}\fontfamily{bch}\fontsize{14.4}{17}\selectfont
DRAFT\\Page~\thepage
}%
};
}%
}

\begin{document}
\title{Test document}
\author{Ahmed Musa}
\maketitle
\lipsum[1-20]
\end{document}


Thanks to Gonzalo Medina for formatting the code.

-
There is also the draftwatermark package that provides macros to add a simple text watermark. – Qrrbrbirlbel Sep 24 '12 at 21:51
You might also be thinking of the \shortstack command for forcing a linebreak. – jon Sep 24 '12 at 21:54

Paul Gaborit found a solution based on my first attempt that uses \AtBeginShipout and \AtBeginShipoutAddToBox.

• \AtBeginShipout is used to add code before each "shipout" operation.
• \AtBeginShipoutAddToBox is used to put material (here \draftpage) in the background of the current \AtBeginShipoutBox.

With this method, the result always contains four pages (with or without draft mark). (The usage of \AtBeginShipoutFirst—can be used to add code before the first page—results in more vertical space so that the last line of page 4 ended up on a fifth page.)

Code

\documentclass{article}

\usepackage{atbegshi}
\usepackage{lipsum}
\usepackage{graphicx}
\usepackage{tikz}
\AtBeginShipoutInit
\newcommand*{\draftpage}{%
\tikz[overlay,remember picture]{%
\node [text=gray!55,inner sep=0pt,rotate=45,align=center,
font=\fontseries{b}\fontfamily{bch}\fontsize{43.2}{51}\selectfont]
at (current page) {DRAFT\\Page~\thepage};
}%
}

\title{Test document}
\author{Ahmed Musa}
\begin{document}
\maketitle
\lipsum[1-20]
\thepage
\end{document}


Output

The second part of the question (which I did oversee when I first answered it) is incorporated and uses percusse's approach using TikZ's scale and align key.

-
Thanks. Let's see if Heiko can save us the duplication. – Ahmed Musa Sep 24 '12 at 19:50
@AhmedMusa I've updated my answer. There is still both macros \AtBeginShipout and \AtBeginShipoutFirst but I hope the duplication is at its lowest. – Qrrbrbirlbel Sep 24 '12 at 20:09
@Qrrbrbirlbel The page counter is incremented after the page has been shipped out. The other counters are incremented just before usage. – egreg Sep 24 '12 at 21:03
@egreg Thanks for clarification. "after" was initially in the first version of my answer but I got confused while I tried to clarify that fact for myself. I hope that the revised version is now correct. – Qrrbrbirlbel Sep 24 '12 at 21:10
@Qrrbrbirlbel You can use \AtBeginShipoutFirst to add code before the first page. The added vertical space is a switch between vertical and horizontal mode: \tikz (or a tikzpicture) calls \leavevmode even with overlay and remember picture options! – Paul Gaborit Sep 24 '12 at 22:27

I have found an alternative to using atbegshi. I hacked into the output routine.

\documentclass{article}
\usepackage{lipsum}
\usepackage{tikz}
\makeatletter
\def\@aftermanifest{}
\let\AfterManifest\@iden
\@aftermanifest
\let\AfterManifest\@notprerr
\global\let\@aftermanifest\relax
\ignorespaces
}
\AfterManifest{%
\let\@savedoutputpage\@outputpage
\def\@outputpage{%
\let\@savedbegindvi\@begindvi
\def\@begindvi{%
\am@watermark
\@savedbegindvi
}%
\@savedoutputpage
\let\@begindvi\@savedbegindvi
}%
}
\def\am@watermark{%
\tikz[overlay,remember picture]{%
\node [text=gray!55,inner sep=0pt,rotate=45,align=center,scale=5,
font=\fontseries{b}\fontfamily{bch}\selectfont]
at (current page) {DRAFT\\[1ex]Page~\thepage};
}%
}
\makeatother

\title{Test document}
\author{Ahmed Musa}
\begin{document}
\maketitle
\lipsum[1-20]
\thepage
\end{document}


-
You must use an older version of MikTeX: the current version of atbegshi is 2011/10/05 v1.16! – Paul Gaborit Sep 24 '12 at 23:19
What advantages with your method? – Paul Gaborit Sep 25 '12 at 19:38