[Tex/LaTex] How to make an image for backgrounds with exact A4 dimensions

backgroundsgraphicspaper-sizetikz-pgf

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 21×29.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?

Best Answer

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