[Tex/LaTex] How to place a background image on page (using tikz?) that goes behind everything

backgroundsheader-footertikz-pgf

I want to place an image behind everything on a specific page in my document to liven it up a bit. The first I tried to do was to do was put in a tikz-picture as background, and it worked pretty well, but infortunately the header gets drawn over.

Here is the essential code I have used so far, and its output:

\documentclass[11pt,a4paper,twoside]{scrbook}
\RequirePackage{microtype}
\RequirePackage{pdfpages}

\usepackage{lipsum}
\usepackage{lettrine}
\usepackage{tikz}
\usepackage{multicol}

% Dimensions:
\setlength\topmargin{-58pt}                 
\setlength\headheight{20pt}                 
\setlength\headsep{25pt}                 
\setlength\marginparwidth{-20pt}            
\setlength\textwidth{\paperwidth - 82pt}    
\setlength\textheight{\paperheight - 112pt} 
\setlength\oddsidemargin{-30pt}
\setlength\evensidemargin{-30pt}

% Header and Footer:
\usepackage{fancyhdr}
% Header
\lhead[\thepage]{Edition}
\rhead[Name]{\thepage}
\chead{\sffamily HEADER}
% Headercolour
\renewcommand{\headrulewidth}{1pt}% 2pt header rule
\renewcommand{\headrule}{\hbox to\headwidth{%
\color{black}\leaders\hrule height \headrulewidth\hfill}}
% Footer
\lfoot{}
\rfoot{}
\cfoot{\vspace{-20pt}\textcolor{black}{\rule{\textwidth}{1pt}}}

% Fonts:
\usepackage{mathptmx}   % Serif
\usepackage{DejaVuSans} % Sans-serif
\usepackage[T1]{fontenc}
\usepackage{xstring}

\begin{document}

\noindent
\begin{tikzpicture}[remember picture,overlay]
\node (background) at (0.5\textwidth,-0.5\textheight+15pt) {\includegraphics[height=\paperheight+20pt]{bak}};
\draw [white , fill=white, opacity=0.7](-15pt,-\topmargin+5pt) rectangle (\textwidth+10pt,-\topmargin-\headheight-10pt);
\draw [white , fill=white, opacity=0.7](-15pt,15pt) rectangle (\textwidth+10pt,-\textheight);
\draw [white , fill=white, opacity=0.7](-15pt,-\textheight-22pt) rectangle (\textwidth+10pt,-\textheight-30pt);
\end{tikzpicture}
\pagestyle{fancy}

\begin{multicols}{2}
\lettrine[loversize=0.1]{A}{S} I said, this is a test. \lipsum
\end{multicols}
\clearpage

\begin{multicols}{2}
\lipsum
\end{multicols}

\end{document}

The output

Notice that of the two pages, only the second has a header. In the other the tikz-picture has drawn on top of it. If there is some way to make tikz draw underneath the header, or even to force a re-draw of the header, that would solve my problems. But I do not know how to do any of these.

Notice also that the footer does show up, whereas the header disappears behind the tikz drawing, for some reason. I don't know what to make of this.

Any ideas?

Best Answer

Here is a suggestion that needs an uptodate KOMA-Script: version 3.16 or newer. Then you can use scrlayer-scrpage instead fancyhdr and define a new pagestyles for the pages with a background image.

\documentclass[11pt,a4paper,twoside]{scrbook}
\usepackage{microtype}
\usepackage{pdfpages}

\usepackage{lipsum}
\usepackage{lettrine}
\usepackage{tikz}
\usepackage{multicol}

% Dimensions:
\usepackage{geometry}
\geometry{
  top=59pt,
  headheight=20pt,
  headsep=20pt,
  bottom=53pt,
  footskip=50pt,
  %marginparwidth=-20pt,
  hmargin=41pt
  }

\addtokomafont{pagehead}{\normalfont}
\usepackage[headsepline=1pt,footsepline=1pt]{scrlayer-scrpage}%<- activates pagestyle scrheadings
\clearpairofpagestyles
\newcommand*{\headcontents}[1]{%
  \raisebox{0pt}[\ht\strutbox][\dimexpr\headheight-\ht\strutbox\relax]{#1}}
\lehead{\headcontents{\pagemark}}
\lohead{\headcontents{Edition}}
\rehead{\headcontents{Name}}
\rohead{\headcontents{\pagemark}}
\chead{\sffamily\headcontents{HEADER}}

\newpairofpagestyles[scrheadings]{backgroundimage}{}
\makeatletter
\newcommand*\@backgroundimage{}
\newcommand*\backgroundimage[1]{%
  \ifstr{#1}{}{\gdef\@backgroundimage{}}{%
    \gdef\@backgroundimage{\includegraphics[height=\dimexpr\paperheight+20pt\relax]{#1}}%
}}

\colorlet{backgroundcolor}{white}
\newcommand*\coloredbg{%
  \tikz\fill[backgroundcolor,opacity=.7](0,0)rectangle({\layerwidth},{\layerheight});}

\DeclareNewLayer[background,textarea,
  addvoffset=-5pt,addhoffset=-5pt,addwidth=10pt,addheight=10pt,
  contents=\coloredbg
]{text.bg}
\DeclareNewLayer[background,foot,align=b,
  addvoffset=9pt,addhoffset=-5pt,addwidth=10pt,height=12pt,
  contents=\coloredbg
]{foot.bg}
\DeclareNewLayer[background,head,
  addvoffset=11pt,addhoffset=-5pt,addwidth=10pt,addheight=13pt,
  contents=\coloredbg
]{head.bg}
\DeclareNewLayer[background,
  area={.5\paperwidth}{.5\paperheight}{0pt}{0pt},
  contents={\makebox[0pt]{%
    \raisebox{0pt}[.5\height][.5\height]{\@backgroundimage}%
  }}
]{image.bg}
\AddLayersAtBeginOfPageStyle{backgroundimage}{text.bg,foot.bg,head.bg,image.bg}
\makeatother

\usepackage{mathptmx}
\usepackage{DejaVuSans}
\usepackage[T1]{fontenc}
\usepackage{xstring}

\usepackage{mwe}% for the example images

\begin{document}
\backgroundimage{example-image}%<- set the background image
\thispagestyle{backgroundimage}%<- use the background image only on the current page
\begin{multicols}{2}
\lettrine[loversize=0.1]{A}{S} I said, this is a test. \lipsum
\lipsum
\end{multicols}
\clearpage

\backgroundimage{example-image-A}%<- change the background image
\pagestyle{backgroundimage}%<- change the pagestyle
\begin{multicols}{2}
\lipsum\lipsum
\end{multicols}
\end{document}

enter image description here

Related Question