4

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?

  • did you try the background package – touhami Mar 17 '15 at 21:53
  • Or the wallpaper package. (Google latex wallpaper showed the most amazing pictures!) – John Kormylo Mar 17 '15 at 21:55
  • @touhami No, does it do what I want? – Delta t Mar 17 '15 at 21:58
  • @JohnKormylo Where can I find some examples of the wallpaper package in use? Also, is it capable of doing something like my example (with rectangles of color and opacity, only for chosen pages, etc)? – Delta t Mar 17 '15 at 22:00
  • 1
    tex.stackexchange.com/questions/110502/… The foreground of partially opaque filler might be more difficult, although you could just edit it onto the picture. – John Kormylo Mar 18 '15 at 0:32
5

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

  • How do I make sure I have such an up to date version of KOMA-script? When might I not have? I want to make a setup that is easy to use for several people, other than myself, who use different setups and LaTeX distributions. Could this cause problems? – Delta t Mar 19 '15 at 11:55
  • Other than that, let me just add, this solution looks absolutely perfect for my needs. – Delta t Mar 19 '15 at 11:56
  • 2
    2015/02/07 v3.16 is the current KOMA-Scriptversion on CTAN, in TeX Live 2014 and MiKTeX2.9. Additionally you can get this version from the KOMA-Script website – esdd Mar 19 '15 at 12:42
3

Here is a solution

\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}

\makeatletter
\def \@begindvi{%
  \unvbox \@begindvibox
\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}
  \global\let \@begindvi \@empty
}
\makeatother

\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}

Edit: use this by command \mtbg{bak}

    \def\mtbak#1{%
\begin{tikzpicture}[remember picture,overlay]
\node (background) at (0.5\textwidth,-0.5\textheight+15pt) {\includegraphics[height=\paperheight+20pt]{#1}};
\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}}
\makeatletter
\def\mtbg#1{%
\def \@begindvi{%
  \unvbox \@begindvibox
  \mtbak{#1}
  \global\let \@begindvi \@empty
}}
\makeatother
  • That seems to work really well! I only notice a slight problem with the positioning of the tikz-picture. It seems to be indented. In my original code I simply added a \noindent to prevent this, but this doesn't seem to work here. Any ideas? – Delta t Mar 17 '15 at 22:24
  • Also, I can't seem to make this solution into a macro, which is something I would like to do to make things easier. A command \background{bak} that takes the picture name. – Delta t Mar 18 '15 at 0:19
  • @Deltat for the position you maybe need to move your objects to the left for example replace (-15pt,-\topmargin+5pt) by (-15pt-.5cm,-\topmargin+5pt) – touhami Mar 18 '15 at 6:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.