![使用 tikz 包在框图中创建容器](https://linux22.com/image/344650/%E4%BD%BF%E7%94%A8%20tikz%20%E5%8C%85%E5%9C%A8%E6%A1%86%E5%9B%BE%E4%B8%AD%E5%88%9B%E5%BB%BA%E5%AE%B9%E5%99%A8.png)
我在通过包在流程图内创建容器时遇到了问题tikz
。
第一张图片显示的是预期结果,第二张图片显示的是我制作的当前版本。
如何调整容器框高度以及如何将文本放置在“中心”点上方?此外,我在使用该fill=gray
命令时遇到了问题:如何修复图层问题,以便容器位于背景中?
这是我当前的代码:
\documentclass[12pt,a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,fit}
\tikzstyle{block} = [draw, rectangle, text width=2cm, text centered, minimum height=1.2cm, node distance=3cm]
%\tikzstyle{container} = [draw, rectangle, inner sep=0.3cm, fill=gray]
\tikzstyle{container} = [draw, rectangle, inner sep=0.3cm]
\begin{document}
\begin{figure}[]
\centering
\begin{tikzpicture}
\node [block, name=text1] {Text1};
\node [block, right of=text1] (text2) {Text2};
\node [block, right of=text2] (text3) {Text3};
\node [block, right of=text3] (text4) {Text4};
\node [block, right of=text4] (text5) {Text5};
\node [container,fit=(text2) (text3) (text4)] (container) {};
\draw [->] (text1) -- (text2);
\draw [->] (text2) -- node {} (text3);
\draw [->] (text3) -- node {} (text4);
\draw [->] (text4) -- node {} (text5);
\end{tikzpicture}
\caption{Test.}
\end{figure}
\end{document}
谢谢你!
答案1
我找到了一个使用backgrounds
tikzlibrary 的解决方案。
\documentclass[12pt,a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,fit,backgrounds}
\tikzstyle{block} = [draw, rectangle, text width=2cm, text centered, minimum height=1.2cm, node distance=3cm,fill=white]
\tikzstyle{container} = [draw, rectangle, inner sep=0.3cm, fill=gray,minimum height=3cm]
%\tikzstyle{container} = [draw, rectangle, inner sep=0.3cm]
\def\bottom#1#2{\hbox{\vbox to #1{\vfill\hbox{#2}}}}
\tikzset{
mybackground/.style={execute at end picture={
\begin{scope}[on background layer]
\node[] at (current bounding box.north){\bottom{1cm} #1};
\end{scope}
}},
}
\begin{document}
\begin{figure}[]
\centering
\begin{tikzpicture}[mybackground={Box1}]
\node [block, name=text1] {Text1};
\node [block, right of=text1] (text2) {\color{red} Text2};
\node [block, right of=text2] (text3) {\color{blue} Text3};
\node [block, right of=text3] (text4) {\color{orange} Text4};
\node [block, right of=text4] (text5) {Text5};
\begin{scope}[on background layer]
\node [container,fit=(text2) (text3) (text4)] (container) {};
\end{scope}
\draw [->] (text1) -- (text2);
\draw [->] (text2) -- node {} (text3);
\draw [->] (text3) -- node {} (text4);
\draw [->] (text4) -- node {} (text5);
\end{tikzpicture}
\caption{Test.}
\end{figure}
\end{document}