4

I want to put a background image on a file which uses the scrartcl class. The target pdf output size is A4 which is exactly the size of the background image (210mm x 297mm). The image should therefore cover the whole page.

This is what I've tried so far:

\documentclass[11pt,ngerman,a4paper]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[printwatermark]{xwatermark}
\newwatermark[allpages,picscale=1,picfile=./bg1,picfileext=png,picwidth=210mm,picheight=297mm,xpos=0,ypos=0]{}   
\begin{document}
    Hello World!
\end{document}

Unfortunately I get an error message

Package atveryend Info: Empty hook `BeforeClearDocument' on input line 11. <./bg1.png, id=4, 597.432pt x 845.0772pt> File: ./bg1.png Graphic file (type png)

Package pdftex.def Info: ./bg1.png used on input line 11. (pdftex.def) Requested size: 597.50662pt x 845.0455pt.

! Undefined control sequence. \xwm@makedraftbox ...@begingroup \ttfamily \color {red}\Large (draft mode)\c... l.11 \end{document}

and the result does not look very promising

screenshot

Do you have any idea what the problem might be?

This is the test-background I've used:

background

3

I would recommend the background package for this. Admittedly, this is mostly because I know it mostly works. (However, it turns out, there is also a good reason to use something other than xwatermark if using a KOMA class. See below.)

Solution with background package

For example:

\documentclass[11pt,ngerman,a4paper]{scrartcl}
\usepackage{background}
\backgroundsetup{
  contents={\includegraphics{bg1}},
  scale=1,
  angle=0
}
\begin{document}
    Hello World!
\end{document}

background background image

Diagnosis and solution with xwatermark

If you want to keep using xwatermark, things are a bit more complicated. The first point is that the package seems to require, but not load, the color package. At least, I got errors without it so I added

\usepackage{xcolor}

According to the manual (page 8):

For graphics/picture watermarks, you need the picfile (the graphics filename, with its full path but without its extension), picfileext (the picture filename extension without the dot), picbb (the picture bounding box), and picscale (the picture scale).

So I figured that I would see what happened if I set the watermark using just those keys. Not having much idea what the boundinb box should be, I took the default value xwatermark uses and made it explicit.

\usepackage[printwatermark]{xwatermark}
\newwatermark[allpages,picscale=1,picfile=./bg1,picfileext=png,picbb=0 0 100 100]{}

This starts to give a sense of where the problem might lie:

output with default bb and minimal settings

Exactly the same result is obtained, unsurprisingly, if we use

\newwatermark[allpages,picscale=1,picfile=./bg1,picfileext=png,]{}

and let the package use the default bounding box.

So what is happening is that the package uses a bounding box which captures only a small part of the picture. Although picscale=1 tells it not to scale the image, the later options setting the image's width and height override this and xwatermark scales the image accordingly. That is, it magnifies the small part of the picture inside the bounding box 0 0 100 100 so that it fills the requested area.

So, to get the desired output, it is necessary to determine the correct bounding box.

The output from running pdflatex tells us that the image dimensions are 597.432pt x 845.0772pt. Hence, a good bet would seem to be

\newwatermark[allpages,picscale=1,picfile=./bg1,picfileext=png,picbb=0 0 597.432pt 845.0772pt]{}

and, indeed, this gives the desired result:

output with corrected bounding box

Complete code:

\documentclass[11pt,ngerman,a4paper]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage[printwatermark]{xwatermark}
\newwatermark[allpages,picscale=1,picfile=./bg1,picfileext=png,picbb=0 0 597.432pt 845.0772pt]{}
\begin{document}
    Hello World!\newpage
    Bye-bye!
\end{document}

Note that use of xwatermark will break some functionality when using a KOMA class because the package loads fancyhdr. KOMA issues a warning about this on the console.

Unless you need xwatermark specifically, therefore, it would probably be better to use the background solution or similar. Not only is that strategy simpler, but it should also be fully compatible with KOMA as far as I know.

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.