[Tex/LaTex] Watermark on selected pages or xwatermark and scrlayer-scrpage

koma-scriptscrlayer-scrpagexwatermark

maybe it is a duplicated question, but I want to put a watermark on pages I define at the beginning of the document. So far, the only package providing this is xwatermark. But I use scrlayer-scrpage and they do not work together. Is there a workaround or any other packages that provide this functionality?

\documentclass{scrartcl}
\usepackage{xcolor}
\usepackage{blindtext}
\usepackage[printwatermark]{xwatermark}
%\usepackage{scrlayer-scrpage}  % uncomment and compilation fails
\newwatermark[pagex={1,2,4,6},angle=45,scale=3]{Only selected Pages}
\begin{document}
\Blinddocument
\end{document}

Best Answer

Package xwatermark loads package fancyhdr which can't be used together with package scrlayer-scrpage.

With package scrlayer-scrpage you could define an additional layer for the watermark and add this to every layer pagestyle including page style empty.

Example:

\documentclass{scrartcl}
\usepackage{xcolor}
\usepackage{graphicx}
\usepackage[automark]{scrlayer-scrpage}

\usepackage{expl3}
\ExplSyntaxOn
\clist_const:Nn \l_my_watermarkpages {1,2,4,8}% list with the selected page numbers
\newcommand\watermark[1]
  {\makebox[0pt][c]
    {\scalebox{3.1}% scaling
      {\rotatebox[origin=bc]{45}% rotating
        {\Huge\bfseries\textcolor{lightgray}% font settings
          {\clist_if_in:NVTF \l_my_watermarkpages {#1}
            {\watermarktext}{}
  }}}}}
\ExplSyntaxOff
\newcommand\watermarktext{Only selected Pages}

\DeclareNewLayer[
  background,
  %textarea,
  mode=picture,
  contents={\putC{\watermark{\the\value{page}}}}
]{watermark}
\AddLayersToPageStyle{@everystyle@}{watermark}

\usepackage{blindtext}% only for dummy text
\begin{document}
\Blinddocument
\end{document}

Result:

enter image description here


If you don't remove package xwatermark from your code then you have to use package fancyhdr for your header and footer.

Example:

\documentclass{scrartcl}
\usepackage{xcolor}
\usepackage{blindtext}
\usepackage[printwatermark]{xwatermark}
\newwatermark[pagex={1,2,4,6},angle=45,scale=3]{Only selected Pages}

\pagestyle{fancy}
\usepackage{blindtext}% only for dummy text
\begin{document}
\Blinddocument
\end{document}

Result:

enter image description here

Note that this breaks several KOMA-Script features: options headsepline and footsepline, \addtokomafont and setkomafont for elements pageheadfoot and pagenumber etc.