11
  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.

Ahmed Musa
  • 11,742

2 Answers2

12

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}; }% }

\AtBeginShipout{\AtBeginShipoutAddToBox{\draftpage}}

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

Output

correct code

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.

Qrrbrbirlbel
  • 119,821
  • @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
  • @AhmedMusa I don't understand your comment about duplication. –  Sep 25 '12 at 21:20
  • @MarcvanDongen Take a look at the old versions of my answer. – Qrrbrbirlbel Sep 25 '12 at 22:22
2

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

\documentclass{article}
\usepackage{lipsum}
\usepackage{tikz}
\makeatletter
\def\AfterManifest{\g@addto@macro\@aftermanifest}
\def\@aftermanifest{}
\g@addto@macro\document{%
  \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}

enter image description here

Ahmed Musa
  • 11,742