1

I have the following MWE:

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

% Headers        
\node (header1) {\large HeaderOne};
\node[right=of header1] (header2) {\large HeaderTwo};

% 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: enter image description here

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): enter image description here

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? – user156344 Apr 23 at 12:30
  • 1
    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 – user156344 Apr 23 at 12:35
3

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}]

% Headers        
\node (header1) {\large HeaderOne};
\node[right=of header1] (header2) {\large HeaderTwo};

% 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] {};
\node [fill=red, draw=black, fit=(header1)(header2)(current bounding box.north east), inner sep=0pt] {};
\end{pgfonlayer}

\end{tikzpicture}
\end{document}

enter image description here

  • @JouleV That depends on below of, I'll improve soon. – CarLaTeX Apr 23 at 12:58
  • 1
    @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
3

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{red}\large HeaderOne & \large HeaderTwo\\\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}

enter image description here

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{red}\large HeaderOne & \large HeaderTwo\\\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]
\node[text width=3cm,font=\large\color{white},fill=red] (header1) {HeaderOne};
\node[text width=3cm,below=of header1,fill=gray!20] (text1) {Short text};
\node[text width=13cm,font=\large\color{white},fill=red,right=-\pgflinewidth of header1] (header2) {HeaderTwo};
\node[text width=13cm,below=of header2,fill=gray!20] (text2) {A long line of text. A long line of text. A long line of text. A long line of text.};
\draw (header1.north west) -- (header2.north east)
    (header1.south west) -- (header2.south east)
    (text1.south west) -- (text2.south east);
\end{tikzpicture}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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