Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

In order to make a background image that fits the page perfectly, I have tried two things. The first is to use a document class where the dimensions of the page can be set to that of A4, which resulted in the following.

\documentclass[a4paper]{article}
\usepackage{graphicx,tikz}
\usepackage[margin=0cm]{geometry}
\begin{document}
\noindent\resizebox{\pdfpagewidth}{\pdfpageheight}{
    \begin{tikzpicture}
        \draw (0,0) -- (1,0) -- (1,1) -- (0,1) -- cycle;
    \end{tikzpicture}
    }
\end{document}

Another thing was to look up the dimensions of A4 in points, to which I found an answer on Wrong A4 measurements? (consistent with the 21x29.7cm), and then make a standalone with those lengths, with the following code.

\documentclass{standalone}
\usepackage{graphicx,tikz}
\begin{document}
\noindent\resizebox{595.276pt}{841.89pt}{
    \begin{tikzpicture}
        \draw (0,0) -- (1,0) -- (1,1) -- (0,1) -- cycle;
    \end{tikzpicture}
    }
\end{document}

Here I just drew a rectangle to make sure it covered the page, but unfortunately it didn't, as can be seen in the following image (changed the paper colour to grey to make it visible). Both tex files gave the same output, so there is just one image.

An attempt to make a background image

Now I suspected it was the tikz that made this error, and tried with picture instead:

\documentclass{standalone}
\usepackage{graphicx,tikz}
\begin{document}
\begin{picture}(595.276,841.89)
    \linethickness{10pt}
    \multiput(0,0)(595.276,0){2}{\line(0,1){841.89}}
    \multiput(0,0)(0,841.89){2}{\line(1,0){595.276}}
\end{picture}
\end{document}

This worked, resulting in the next image (grey just for consistency this time).

The second attempt

The image was used as a background using

\documentclass[a4paper]{article}
\usepackage{graphicx,lipsum}    % lipsum for the filling text
\usepackage{wallpaper}
\begin{document}
\CenterWallPaper{1}{img/bg2}
\lipsum[1-5]
\end{document}

Now there is one thing I would like an argumented opinion on, and two questions:

  1. Would it be better to make a standalone with the A4 dimenions -possibly not exact- or to use a documentclass that already has the right dimensions (such as [a4paper]{article})?
  2. How does one get tikz to fill an image, or to rescale an image to the width and height of the page? The picture environment just isn't enough for most purposes.
  3. When the line \setlength\unitlength{1pt} was added just above the picture environment, the lines shifted, and the resulting document had extra white at the top and bottom around the image. How could this happen, and how can this be solved?
share|improve this question
2  
TikZ provides a special node for this: It's called current page and has the same size and position as the current page, as long as you use \begin{tikzpicture}[overlay, remember picture] and compile your document twice. You can access the corners using the standard anchors (current page.north west for the top left corner, for example). –  Jake Apr 24 '13 at 14:31
    
There is also the tikzpagenodes package you would want to consider. –  azetina Apr 24 '13 at 15:04

2 Answers 2

up vote 10 down vote accepted

If you want to use wallpaper and insert the background as an image, you can use this code:

\documentclass[a4paper]{article}
\usepackage{graphicx,lipsum}    % lipsum for the filling text
\usepackage{wallpaper}
\usepackage{filecontents}
%%------------------- The background---------------
\begin{filecontents*}{bg2.tex}
\documentclass[a4paper]{standalone}
\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}[remember picture]
        \draw[magenta,fill=gray!30, line width = 4pt] (current page.south west) rectangle (current page.north east);
    \end{tikzpicture}
\end{document}
\end{filecontents*}
%%-------------------
% compile with bg2.tex pdflatex
\immediate\write18{pdflatex bg2}
\begin{document}
\CenterWallPaper{1}{bg2}
\lipsum[1-5]
\end{document}

You have to compile this with --shell escape. But with tikz, this can be done straight away using remember picture and overlay.

\documentclass[a4paper]{article}
\usepackage{tikz}
\usepackage{lipsum}    % lipsum for the filling text
\begin{document}
%%------------------- The background---------------
    \begin{tikzpicture}[remember picture,overlay]
        \draw[magenta,fill=gray!30, line width = 4pt] (current page.south west) rectangle (current page.north east);
    \end{tikzpicture}
%%-------------------
\lipsum[1-5]
\end{document}

Both the codes will need atleast two compilations to settle down.

enter image description here

share|improve this answer
    
It looks like the first example expects bg2.tex to be in a sub-directory, but you are \write-ing it to the current directory. –  jon Apr 24 '13 at 15:21
    
@jon: That was just for the example. The OP ddin't put those two things in the same files in his question. They will be two separate files instead of one as per his question. –  Harish Kumar Apr 24 '13 at 22:43
    
Sure -- but the lines \immediate\write18{pdflatex bg2} and \CenterWallPaper{1}{TeXAuX/bg2} will make the example not work as is unless you remove the TeXAuX (unless it does on Windows..?). I am sure that you know this, but others reading your answer might not. It makes sense to explain better what you are doing or remove the TeXAuX/. –  jon Apr 24 '13 at 23:14
    
@jon Oh! I missed it. It the folder winedt creates for me where all my other files (other than pdf) will be stored. I have removed it now. –  Harish Kumar Apr 24 '13 at 23:25
    
No worries! +1. –  jon Apr 24 '13 at 23:36

Just a note I needed the answer mode for as a comment wouldn't allow for multiline code. When applying this background that Harish so quickly came up with to all pages, the background package can be used. Somehow it is then not necessary to use [overlay, remember picture] as Jake suggested. Using exactly the same (and wonderfully pretty) pink style, a multipage example would look like this.

\documentclass{article}
\usepackage{background,lipsum,tikz}
\begin{document}
\backgroundsetup{angle=0,scale=1,contents=
    \tikz{
        \draw[magenta,fill=gray!30, line width = 4pt]
        (current page.south west) rectangle (current page.north east);
        }
    }
\lipsum[1-5]
\newpage
\lipsum[6-10]
\end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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