我正在尝试向“流程图”添加多个背景,并将它们分为两组,EC2 实例和RDS 实例。
梅威瑟:
\documentclass[a4paper, 11pt]{report}
\usepackage{geometry}
\geometry{a4paper,
total={170mm,257mm},
left=20mm,
top=20mm,
}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
backgrounds,
chains,
fit,
positioning,
shadows.blur,
shapes.geometric}
\begin{document}
\begin{center}
\begin{tikzpicture}[
node distance = 12mm and 10mm,
start chain = A going below,
arr/.style = {thick,-Stealth},
block/.style = {rectangle, rounded corners, fill=blue!20,
text width=7em, align=center, minimum height=2em,
blur shadow, on chain=A},
blockJ/.style = {block, join=by arr},
cloud/.style = {ellipse,fill=red!20, minimum height=2em, drop shadow},
decision/.style = {diamond, aspect=1.2, fill=blue!20,
text width=4.5em, align=center, inner sep=0pt, blur shadow},
FIT/.style = {rectangle, rounded corners, draw, fill=yellow!30,
inner xsep=3em, inner ysep=1em, yshift=0.5em,
fit=#1},
FIT2/.style = {rectangle, rounded corners, draw, dashed, fill=green!10,
inner xsep=3em, inner ysep=1em, yshift=0.5em,
fit=#1},
FIT3/.style = {rectangle, rounded corners, draw, dashed, fill=red!10,
inner xsep=3em, inner ysep=1em, yshift=0.5em,
fit=#1},
]
% Place nodes
\node [block] {EC2 Server}; % A-1
\node [block, right=of A-1] {EC2 Collector}; % A-2
\node [block, right=of A-2] {RDS Database}; % A-3
\begin{pgfonlayer}{background}
\node (f1) [FIT=(A-1) (A-3), xshift=-0em] {};
\node[below right, font=\small] at (f1.north west) {\arial AWS Cluster Server closest to Site};
\node (f2) [FIT2=(A-1) (A-2), xshift=-0em] {};
\node[below right, font=\small] at (f2.north west) {\arial EC2 Instance};
\node (f3) [FIT3=(A-3) (A-3), xshift=-0em] {};
\node[below right, font=\small] at (f3.north west) {\arial RDS Instance};
\end{pgfonlayer}
\end{tikzpicture}
\end{center}
\end{document}
期望输出: 请原谅我拙劣的 MS Paint 尝试 - 但我想实现这样的目标:
答案1
这里有一个建议,用与字体大小成比例的距离来代替一些硬距离,或者完全避免它们。您可以将原来的尺寸调整为更大的尺寸,这需要引入另一个层behind
。
\documentclass[a4paper, 11pt]{report}
\usepackage{geometry}
\geometry{a4paper,
total={170mm,257mm},
left=20mm,
top=20mm,
}
\usepackage{tikz}
\pgfdeclarelayer{background}
\pgfdeclarelayer{behind}
\pgfsetlayers{behind,background,main}
\usetikzlibrary{fit,
positioning,
shadows.blur,
shapes.geometric}
\def\arial{\sffamily}%<- replace by your own
\begin{document}
\begin{center}
\begin{tikzpicture}[
arr/.style = {thick,-Stealth},
block/.style = {rectangle, rounded corners, fill=blue!20,
text width=7em, align=center, minimum height=2em,
blur shadow},
FIT/.style = {rectangle, rounded corners, draw, fill=yellow!30,
inner xsep=1em, inner ysep=1em,
fit=#1},
FIT2/.style = {rectangle, rounded corners, draw, dashed, fill=green!10,
inner xsep=1.5ex, inner ysep=2ex,yshift=-1ex,
fit=#1},
FIT3/.style = {rectangle, rounded corners, draw, dashed, fill=red!10,
inner xsep=1.5ex, inner ysep=2ex,yshift=-1ex,
fit=#1},
]
% Place nodes
\node [block] (A-1) {EC2 Server}; % A-1
\node [block, right=1em of A-1] (A-2) {EC2 Collector}; % A-2
\node [block, right=3em of A-2] (A-3) {RDS Database}; % A-3
\node [xshift=-1ex,anchor=south west] (EC2) at (A-1.north west) {\arial EC2
Instance};
\node [xshift=-1ex,anchor=south west] (RDS) at (A-3.north west) {\arial
RDS Instance};
\begin{pgfonlayer}{background}
\node (f1) [FIT2=(A-1) (A-2) (EC2)] {};
\node (f2) [FIT3=(A-3) (RDS)] {};
\end{pgfonlayer}
\node [xshift=-2em,anchor=south west] (AWS) at (f1.north west)
{\arial AWS Cluster closest to 1};
\begin{pgfonlayer}{behind}
\node (f0) [FIT=(f1) (f2) (AWS)] {};
\end{pgfonlayer}
\end{tikzpicture}
\end{center}
\end{document}