我需要灰色背景还包括标签 T1 和 T2
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,decorations.pathmorphing,backgrounds,positioning,fit,petri}
\begin{document}
\begin{tikzpicture}
\node [rectangle,draw] (set a) [label=right:$T_1$] {\textit{SET A}};
\node [rectangle,draw] (set b) [below=of set a, label=right:$T_2$] {\textit{SET B}};
\draw [->] (set a) to node [auto] {\textit{q}} (set b);
\begin{pgfonlayer}{background}
\node [fill=black!30,fit=(set a) (set b)] {};
\end{pgfonlayer}
\end{tikzpicture}
\end{document}
答案1
杰克的回答是正确的,但如果您想要浅灰色的包装器和不同的背景,就会遇到问题。
然后如果你在图片中添加一些东西;你需要使用嵌套的 tikzpictures(我不怕这个)。有时你需要谨慎使用嵌套图片。
\begin{tikzpicture}
\node at (5,0){extra};
\node {\begin{tikzpicture}[
show background rectangle,
background rectangle/.style={fill=lightgray},
box/.style={draw, font=\itshape}
]
\node [box] (set a) [label=right:$T_1$] {SET A};
\node [box] (set b) [below=of set a, label=right:$T_2$] {SET B};
\draw [-latex] (set a) to node [auto] {\textit{q}} (set b);
\end{tikzpicture}};
\end{tikzpicture}
Gonzalo 的回答也很好,但是如果你添加一个额外的节点,你需要按顺序对图片进行编程,在这种情况下,我认为最好使用范围,local bounding box
如下所示:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,backgrounds,positioning,fit}
\begin{document}
\begin{tikzpicture}[typea/.style={draw,rectangle,font=\itshape}]
\node at (5,0){extra};
\begin{scope}[local bounding box=bb]
\node [typea] (set a) [label=right:$T_1$] {SET A};
\node [typea] (set b) [below=of set a, label=right:$T_2$] {SET B};
\draw [->] (set a) to node [auto,font=\itshape] {q} (set b);
\end{scope}
\begin{pgfonlayer}{background}
\node [fill=black!30,fit=(bb)] {};
% Jake's remark is fine, (bb) avoids (bb.north west) (bb.south east)
\end{pgfonlayer}
\end{tikzpicture}
\end{document}
在使用此选项之前label
(针对 TikZ 的老用户),我们使用简单节点,在这种情况下,您可以避免使用虚拟节点
\begin{tikzpicture}[
box/.style={draw, font=\itshape}
]
\node [box] (set a) {SET A};
\node [box] (set b) [below=of set a] {SET B};
\node[anchor=west,right=6pt] (T1) at (set a.east){$T_1$};
\node[anchor=west,right=6pt] (T2) at (set b.east){$T_2$};
\draw [-latex] (set a) to node [auto] {\textit{q}} (set b);
\begin{pgfonlayer}{background}
\node [fill=black!30,fit=(set a) (T2)] {};
\end{pgfonlayer}
\end{tikzpicture}
最后,您可以使用label
没有虚拟节点的选项name
,我使用 pgf 2.1 CVS,也许这个选项是新的(我不知道)
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,backgrounds,positioning,fit}
\begin{document}
\begin{tikzpicture}
\node [rectangle,draw] (set a) [label={[name=T1]right:$T_1$}] {\textit{SET A}};
\node [rectangle,draw] (set b) [below=of set a, label={[name=T2]right:$T_2$}] {\textit{SET B}};
\begin{pgfonlayer}{background}
\node [fill=black!30,fit=(set a) (T2)] {};
\end{pgfonlayer}
\end{tikzpicture}
\end{document}
答案2
您只需提供show background rectangle, background rectangle/.style={fill=lightgray}
以下选项即可tikzpicture
:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,backgrounds,positioning}
\begin{document}
\begin{tikzpicture}[
show background rectangle,
background rectangle/.style={fill=lightgray},
box/.style={draw, font=\itshape}
]
\node [box] (set a) [label=right:$T_1$] {SET A};
\node [box] (set b) [below=of set a, label=right:$T_2$] {SET B};
\draw [-latex] (set a) to node [auto] {\textit{q}} (set b);
\end{tikzpicture}
\end{document}
您可以使用选项关闭背景矩形的填充tight background
。
答案3
您可以使用current bounding box
:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,decorations.pathmorphing,backgrounds,positioning,fit,petri}
\begin{document}
\begin{tikzpicture}[typea/.style={draw,rectangle,font=\itshape}]
\node [typea] (set a) [label=right:$T_1$] {SET A};
\node [typea] (set b) [below=of set a, label=right:$T_2$] {SET B};
\draw [->] (set a) to node [auto,font=\itshape] {q} (set b);
\begin{pgfonlayer}{background}
\node [fill=black!30,fit=(current bounding box.north west) (current bounding box.south east)] {};
\end{pgfonlayer}
\end{tikzpicture}
\end{document}
答案4
肯定有比这更好的方法。这种方法相当丑陋,而且过于手动,但它确实有效。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,decorations.pathmorphing,backgrounds,positioning,fit,petri}
\begin{document}
\begin{tikzpicture}
\node [rectangle,draw] (set a) [label=right:$T_1$] {\textit{SET A}};
\node [rectangle,draw] (set b) [below=of set a, label=right:$T_2$] {\textit{SET B}};
%\node (set c) [right=of set a,xshift=-0.85cm] {}; % A dummy node not necessary
\node (set d) [right=of set b,xshift=-0.85cm] {}; % Another dummy node
\draw [->] (set a) to node [auto] {\textit{q}} (set b);
\begin{pgfonlayer}{background}
\node [fill=black!30,fit=(set a)(set d)] {}; % enough to add two diagonal nodes
\end{pgfonlayer}
\end{tikzpicture}
\end{document}
如果你有多个这样的集合A
,一个在另一个下面,那么在最后一个节点的右边定义一个虚拟节点就足够了,就像
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,decorations.pathmorphing,backgrounds,positioning,fit,petri}
\begin{document}
\begin{tikzpicture}
\node [rectangle,draw] (set a) [label=right:$T_1$] {\textit{SET A}};
\node [rectangle,draw] (set b) [below=of set a, label=right:$T_2$] {\textit{SET B}};
\node [rectangle,draw] (set c) [below=of set b,label=right:$T_3$] {\textit{SET C}};
\node [rectangle,draw] (set d) [below=of set c, label=right:$T_4$] {\textit{SET D}};
%\node (set e) [right=of set a,xshift=-0.85cm] {}; % A dummy node not really needed
\node (set f) [right=of set d,xshift=-0.85cm,yshift=-0.15cm] {}; % Another dummy node, add yshift also as needed.
\draw [->] (set a) to node [auto] {\textit{q}} (set b);
\begin{pgfonlayer}{background}
\node [fill=black!30,fit=(set a) (set f)] {};
\end{pgfonlayer}
\end{tikzpicture}
\end{document}