[Tex/LaTex] Background image from tikzpicture does not fit the whole page

backgroundsdimensionsgraphicstikz-pgf

For a poster, I would like to have an image fill the whole page. This image is supposed to be drawn within a tikz environment as my other content. However, graphicx seems to fail at rescaling when the image aspect ratio is much different fom the paper aspect ratio. My code reads:

\documentclass[]{extarticle}
\usepackage[a0paper,margin=0cm]{geometry}
\usepackage{tikz}
\pagestyle{empty}
\setlength{\parindent}{0pt}

\begin{document}
\begin{tikzpicture}
% draw generic background
\draw[white,line width=0pt] (-.5\textwidth, -.5\textheight) rectangle (.5\textwidth, .5\textheight);
% draw image
\pgftext[center,at={\pgfpoint{0}{0}}]{\includegraphics[width=\textwidth,height=\textheight]{test.jpg}}
\end{tikzpicture}
\end{document}

I use the background to fill the whole page with a tikz environment so that (0,0) is at the center of the page and I can position other tikz objects as needed. As long as I use images which closely fit the page or if I use the command includegraphics[width=\textwidth,height=\textheight,keepaspectratio], the image gets scaled correctly. Otherwise, I get a blank first page and my content is on the second page. In other words, the image does not fit the page size, it is too large! How can I prevent this error from happening? Or is there even a better way to draw the image from within the tikz environment?

Best Answer

Here is a simple solution without \pgftext (don't mix pgf and TikZ) and margin=0cm, and using the remember picture and overlay option, the special current page node and \paperwidth x\paperheight:

\documentclass[]{extarticle}
\usepackage[a0paper]{geometry}
\usepackage{tikz}
\pagestyle{empty}
\begin{document}
\begin{tikzpicture}[remember picture,overlay]
% draw image
\node[inner sep=0] at (current page.center)
{\includegraphics[width=\paperwidth,height=\paperheight]{example-image}};
\end{tikzpicture}
\end{document}