# Background under a tikzpicture

There are a lot of questions about background but I haven't found the answer I want

I would like to use a picture (old paper) under the result of a tikzpicture.

Actually my code is

% !TEX TS-program = lualatex-dev
\documentclass{standalone}
\usepackage{tikz}
\usepackage{graphicx}
\newlength{\mywidth}
\newlength{\myheight}

\makeatletter
\newcommand{\pictsize}[2]{ % #1 = width, #2 = height
\pgfextractx{\@tempdima}{\pgfpointdiff{\pgfpointanchor{current bounding box}{south west}}
{\pgfpointanchor{current bounding box}{north east}}}
\global#1=\@tempdima
\pgfextracty{\@tempdima}{\pgfpointdiff{\pgfpointanchor{current bounding box}{south west}}
{\pgfpointanchor{current bounding box}{north east}}}
\global#2=\@tempdima
}
\makeatother
\pgfdeclarelayer{background layer}
\pgfsetlayers{background layer,main}
\tikzset{paper/.style={%
execute at end picture={%
\pictsize{\mywidth}{\myheight}%
\begin{pgfonlayer}{background layer}
\node[inner sep=0pt] (p) at (current bounding box.center)
{\includegraphics[width=\mywidth,height=\myheight]{paper.png}};
\end{pgfonlayer}%
}}}

\begin{document}

\begin{tikzpicture}[paper]
\coordinate (A) at (0,0);
\coordinate (B) at (6,6);
\draw[line width=1cm] (A) -- (B);
\end{tikzpicture}
\end{document}


My question : Is it possible to avoid \newlength{\mywidth} \newlength{\myheight} and the command \pictsize ? the tikzpicture and the background must have the same dimensions. With only TikZ !

• @JohnKormylo yes but it’s not a problem! – Alain Matthes Jan 18 at 19:46

You could clip the image using the bounding box, assuming the default size is large enough.

\documentclass{standalone}
\usepackage{tikz}
\usepackage{graphicx}

\pgfdeclarelayer{background layer}
\pgfsetlayers{background layer,main}
\tikzset{paper/.style={%
execute at end picture={%
\begin{pgfonlayer}{background layer}
\clip (current bounding box.south west) rectangle (current bounding box.north east);
\node[inner sep=0pt] (p) at (current bounding box.center)
{\includegraphics{example-image}};
\end{pgfonlayer}%
}}}

\begin{document}

\begin{tikzpicture}[paper]
\coordinate (A) at (0,0);
\coordinate (B) at (6,6);
\draw[line width=1cm] (A) -- (B);
\end{tikzpicture}
\end{document}


Yes, of course. pgf stores the dimensions in pgf@picmaxx, \pgf@picminx, pgf@picmaxy, and \pgf@picminy, so you could just use them.

\documentclass{standalone}
\usepackage{tikz}
\pgfdeclarelayer{background layer}
\pgfsetlayers{background layer,main}
\makeatletter
\tikzset{paper/.style={%
execute at end picture={%
\begin{pgfonlayer}{background layer}
\pgfmathsetmacro{\mywidth}{\pgf@picmaxx-\pgf@picminx}%
\pgfmathsetmacro{\myheight}{\pgf@picmaxy-\pgf@picminy}%
\node[inner sep=0pt] (p) at (current bounding box.center)
{\includegraphics[width=\mywidth pt,height=\myheight pt]{example-image-duck}};
\end{pgfonlayer}%
}}}
\makeatother

\begin{document}
\begin{tikzpicture}[paper]
\coordinate (A) at (0,0);
\coordinate (B) at (6,6);
\draw[line width=1cm] (A) -- (B);
\end{tikzpicture}
\end{document}


BTW, tikz automatically loads graphicx, so this is really "only tikz". ;-)

• One may use \dimexpr but I am not sure if that is really much better than defining two local macros (as opposed to introducing new lengths). – Schrödinger's cat Jan 18 at 18:20
• I forgot pgf@picmaxx ! I'm getting too old. Good answer and informations about graphicsand :dimexpr . Thanks but the clip solves my problems – Alain Matthes Jan 18 at 20:49