# Tikz - create rectangle with same width as the picture

I have the following MWE:

\documentclass[]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,fit,shapes.geometric,calc,backgrounds}
\begin{document}
\begin{tikzpicture}

% Text
\node[below=of header1.west, anchor=west] (text1) {Short text};
\node[below=of header2.west, anchor=west] (text2) {A long line of text. A long line of text. A long line of text. A long line of text.};

% Background
\begin{pgfonlayer}{background}
\node [fill=gray!20, draw=black, fit=(current bounding box.north west) (current bounding box.south east)] {};
\end{pgfonlayer}

\end{tikzpicture}
\end{document}


which produces this:

However, in addition, I would like to create a rectangle as background for HeaderOne and HeaderTwo, which also spans the width of the entire picture, a bit like this (please ignore the bad painting around the text itself; it should be drawn on the background layer):

I have tried using 'background top' from the pgf manual in conjunction with 'background rectangle', but this does not look good when you want to have drawn a black line (border) around everything as well (the border does not fit the entire picture then).

Thanks.

• Why don't you just use a table for that? – JouleV Apr 23 at 12:30
• I have further elements in my actual tikz picture (i.e. code which is inherent to tikz, and would not fit into a table). Sorry I didnt mention that. – PS. Apr 23 at 12:34
• My suggestion is to make a table and place this table inside a node – JouleV Apr 23 at 12:35

Just add another fit node and do not forget inner sep=0pt.

\documentclass[]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,fit,shapes.geometric,calc,backgrounds}
\begin{document}
\begin{tikzpicture}[every node/.style={minimum height=5ex}]

% Text
\node[below=0pt of header1.south west, anchor=north west] (text1) {Short text};
\node[below=0pt of header2.south west, anchor=north west] (text2) {A long line of text. A long line of text. A long line of text. A long line of text.};

% Background
\begin{pgfonlayer}{background}
\node [fill=gray!20, draw=black, fit=(current bounding box.north west) (current bounding box.south east), inner sep=0pt] {};
\end{pgfonlayer}

\end{tikzpicture}
\end{document}


• @JouleV That depends on below of, I'll improve soon. – CarLaTeX Apr 23 at 12:58
• @JouleV Yes, that's also a good solution, see my now. – CarLaTeX Apr 23 at 13:05
• @CarLaTeX: Thanks for your solution - exactly what I needed. – PS. Apr 23 at 13:59
• @PS. You're welcome! Thank you for accepting my answer! – CarLaTeX Apr 23 at 14:00

Why don't use a simple table for that?

\documentclass[margin=3]{standalone}
\usepackage{colortbl}
\usepackage{xcolor}
\begin{document}
\renewcommand{\arraystretch}{1.5}
\begin{tabular}{ll}
\hline
\rowcolor{gray!20} Short text & A long line of text. A long line of text. A long line of text. A long line of text.\\
\hline
\end{tabular}
\end{document}


If you want to use this table inside a TikZ picture, simply place it inside a node.

\documentclass[margin=3,tikz]{standalone}
\usepackage{colortbl}
\usepackage{xcolor}
\begin{document}
\tikz\node{\renewcommand{\arraystretch}{1.5}%
\begin{tabular}{ll}
\hline
\rowcolor{gray!20} Short text & A long line of text. A long line of text. A long line of text. A long line of text.\\
\hline
\end{tabular}};
\end{document}


With pure TikZ it is also not that complicated. No need of PGF layers. However, I don't recommend this way.

\documentclass[margin=3,tikz]{standalone}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[node distance=0pt,inner sep=7pt]