我有以下 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}
不过,除此之外,我还想创建一个矩形作为 HeaderOne 和 HeaderTwo 的背景,它也横跨整个图片的宽度,有点像这样(请忽略文本本身周围的糟糕绘画;它应该绘制在背景层上):
我曾尝试将 pgf 手册中的“背景顶部”与“背景矩形”结合使用,但当您想在所有内容周围画一条黑线(边框)时,效果并不好(边框不适合整个图片)。
谢谢。
答案1
只需添加另一个fit
节点,不要忘记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}
答案2
为什么不使用一个简单的表格呢?
\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}
如果你想在 Ti 中使用此表钾Z 图片,只需将其放在节点内即可。
\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}
采用纯 Ti钾Z 也不是很复杂。不需要 PGF 层。不过,我不推荐这种方式。
\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}