我是 latex 和 tikz 的新手,想用它们画一个框图。但我遇到了一些问题。有人能帮我解答一下我遇到的几个问题吗?
如何写入“此文本”以使其位于填充框的中间,并且没有文本 0 和文本 6 的框?
如何在框之间或箭头上方书写文字?
有没有办法控制单个框的大小并具有不同的箭头形状?
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,positioning,fit,backgrounds}
\tikzstyle{block} = [draw, rectangle, align=center, text width=2.4cm, text centered, minimum height=1.2cm, node distance=3.5cm,fill=white]
\tikzstyle{container} = [draw, rectangle, inner sep=0.3cm, fill=gray,minimum height=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}
\resizebox{14cm}{3cm}{%
\begin{tikzpicture}[>=latex',mybackground={This text}]
\node [block, name= text0] (text0){text0};
\node [block, right of=text0](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 [block, right of=text5] (text6) {text6};
\begin{scope}[on background layer]
\node [container,fit= (text3) (text4)] (container) {};
\end{scope}
\draw [->] (text0) -- (text1);
\draw [->] (text1) -- (text2);
\draw [->] (text2) -- node {}(text3);
\draw [->] (text3) -- node {}(text4);
\draw [->] (text4) -- node {} (text5);
\draw [->] (text5) -- node {} (text6);
\end{tikzpicture}
}
\end{document}
答案1
希望下面的例子能满足你的所有要求:
\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{arrows.meta,
backgrounds,
chains,
fit,
quotes}
\begin{document}
\begin{tikzpicture}[auto,
node distance = 15mm,
start chain = A going right,
block/.style = {draw, fill=white,
text width=#1, minimum height=12mm, align=center,
outer sep=0pt, on chain},
block/.default = 18mm,
container/.style = {draw, fill=gray!50,
inner xsep=2mm, inner ysep=7mm},
]
\node [block] {text 0}; % block name is A-1
\node [block] {text 1};
\node [block] {text 2};
\node [block=22mm] {text 3}; % block name is A-4
\node [block=11mm] {text 4}; % block name is A-5
\node [block] {text 5};
\node [block] {text 6}; % block name is A-7
%
\scoped[on background layer]
\node [container, label={[anchor=north]This text},
fit= (A-4) (A-5)] (container) {};
\draw [-Stealth]
(A-1) edge ["text 1"] (A-2) % text on arrow is between " and "
(A-2) edge ["text 2"] (A-3)
(A-3) edge ["text 3"] (A-4)
(A-4) edge ["text 4"] (A-5)
(A-5) edge ["text 5"] (A-6)
(A-6) edge ["text 5"] (A-7);
\end{tikzpicture}
\end{document}
- 由于您的框图很简单,所有块都在链中,我建议使用
chains
库进行定位(假设它们之间的距离相等,但是,如果其中一些不同,则可以通过本地使用进行更改right=of <name previous block>
- 对于块之间箭头上的文本,可以很容易地通过库的帮助来
quotes
编写(<node name i>) edge [" your text"] (<node name i+1>)
- 抱歉,我不明白您的第一个问题!!
- 不要使用
\resizebox
!它会导致图像看起来不好。而是调整块的大小、字体和块之间的距离 - 使用时,
arrows.meta
您可以简单地调整箭头大小(长度、宽度、角度等)。有关详细信息,请参阅“tikz 和 pgf 手册,v 3.0.1a”,第 201 页的“16.5 参考:箭头提示”部分。在下面的 mwe 中,我使用默认值,但是您可以尝试例如(参见上述手册的第 209 页):
\draw[-{Stealth[length=2mm,width=3mm,inset=0.5mm]}] ...