这是我的第一个 TikZ 图。我想得到这样的图
我用这个代码
\documentclass{article}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\begin{document}
\pagestyle{empty}
\tikzstyle{line} = [draw, -latex']
\begin{tikzpicture}
\node [draw,rounded corners, fill=blue!20] at (2,1) (Start) {Some Text};
\node [draw,rounded corners, fill=blue!20] at (0,0) (Block1) {Text Text};
\node [draw,rounded corners, fill=blue!20] at (4,0) (Block2) {Text};
\node [draw,rounded corners, fill=blue!20] at (6,0) (Block3) {Text Text Text};
\node[text width=3cm] at (0,-1.5) {some text spanning three lines with automatic line breaks\\
Another line of text};
\path [line] (Start) -| (Block1);
\path [line] (Start) -| (Block2);
\path [line] (Start) -| (Block3);
\end{tikzpicture}
\end{document}
- 将节点和相关文本放置在节点下方。
- 节点下方的文本之间的距离相等。
- 将此图表置于页面的中心。
答案1
自从我的答案到Diaa Abidou 随后提出的问题显然这个问题的答案可能更好,我在这里重复这个答案以保证完整性。如需完整解释代码,请请参阅我提供的详细信息。
\documentclass[border=10pt,multi,tikz]{standalone}
\usepackage[edges]{forest}
\usepackage{calc}
\usetikzlibrary{arrows.meta}
\forestset{%
declare dimen register={gap},
gap'=20mm,
declare dimen register={lbox width},
lbox width=(\textwidth-2*\forestregister{gap})/3,
rbox/.style = {draw=blue!80!black, fill=blue!20, rounded corners},
lbox/.style = {align/.wrap pgfmath arg={@{}p{##1 pt}@{}}{(lbox_width)}},
}
\begin{document}
\begin{forest}
forked edges,
for tree={%
font = \sffamily,
edge = {draw, -{Latex}},
},
where n children=0{%
lbox,
no edge,
}{%
rbox,
}
[Some Text, name=Start, l sep+=5pt
[Text Text, name=Block1
[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
]
]
[Text, name=Block2
[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
]
]
[Text Text Text, name=Block3
[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
]
]
]
\end{forest}
\end{document}
编辑
为了回答如何减少终点节点与其父节点之间的距离的问题,我会更改l
这些节点的值。但是,我们需要将此操作推迟到树打包之后,以便覆盖 Forest 的计算。
例如,
where n children=0{%
lbox,
no edge,
before computing xy={%
l'-=10pt,
},
}{%
rbox,
}
生产
\documentclass[border=10pt,multi,tikz]{standalone}
\usepackage[edges]{forest}
\usepackage{calc}
\usetikzlibrary{arrows.meta}
\forestset{%
declare dimen register={gap},
gap'=20mm,
declare dimen register={lbox width},
lbox width=(\textwidth-2*\forestregister{gap})/3,
rbox/.style = {draw=blue!80!black, fill=blue!20, rounded corners},
lbox/.style = {align/.wrap pgfmath arg={@{}p{##1 pt}@{}}{(lbox_width)}},
}
\begin{document}
\begin{forest}
forked edges,
for tree={%
font = \sffamily,
edge = {draw, -{Latex}},
},
where n children=0{%
lbox,
no edge,
before computing xy={%
l'-=10pt,
},
}{%
rbox,
}
[Some Text, name=Start, l sep+=5pt
[Text Text, name=Block1
[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
]
]
[Text, name=Block2
[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
]
]
[Text Text Text, name=Block3
[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
]
]
]
\end{forest}
\end{document}
答案2
尝试:
\documentclass{article}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,positioning}
\begin{document}
\pagestyle{empty}
\begin{figure}
\centering
\begin{tikzpicture}[
font = \sffamily,
line/.style = {draw, -Latex},
rbox/.style = {draw=blue!80!black, fill=blue!20,
rounded corners, inner sep=2mm,
node distance = 12mm and 18mm},
lbox/.style = {text width=32mm, inner sep=0pt,
node distance = 2mm}
]
\node (Start) [rbox] {Some Text};
\node (Block1) [rbox,below left=of Start] {Text Text};
\node (Block2) [rbox,below=of Start] {Text};
\node (Block3) [rbox,below right=of Start] {Text Text Text};
\node [lbox,below=of Block1] {some text spanning three lines
with automatic line breaks.
Another line of text};
\node [lbox,below=of Block2] {some text spanning three lines
with automatic line breaks};
\node [lbox,below=of Block3] {some text spanning three lines
with automatic line breaks.};
\path [line] (Start) -- coordinate (a) (Block2);
\path [line] (a) -| (Block1);
\path [line] (a) -| (Block3);
\end{tikzpicture}
\caption{my figure}
\label{fig-1}
\end{figure}
\end{document}
我假设该图像在文档中以浮动方式放置figure
。在其中,您可以使用使其居中\centering
。如果不是这种情况,那么您可以使用
\begin{center}
< your tikz image >
\end{center}
在上面的 MWE 中,我定义了rbox
圆角节点和lbox
圆角框下方节点的样式。对于它们的定位,我使用了库中的相对定位positioning
。
附录: 如果图像如编辑的问题中所示,您需要做一些小的更改:
(a)
以新的方式定义坐标:\draw (Start.south) -- ++ (0,-6mm) coordinate (a);
node distance
将节点样式重新定义rbox
为类似node distance = 12mm and 0mm
- 当然,
block
从代码中删除...
\documentclass{article}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,positioning}
\begin{document}
\pagestyle{empty}
\begin{figure}
\centering
\begin{tikzpicture}[
font = \sffamily,
line/.style = {draw, -Latex},
rbox/.style = {draw=blue!80!black, fill=blue!20,
rounded corners, inner sep=2mm,
node distance = 12mm and 0mm},
lbox/.style = {text width=32mm, inner sep=0pt,
node distance = 2mm}
]
\node (Start) [rbox] {Some Text};
\node (Block1) [rbox,below left=of Start] {Text Text};
\node (Block3) [rbox,below right=of Start] {Text Text Text};
\node [lbox,below=of Block1] {some text spanning three lines
with automatic line breaks.
Another line of text};
\node [lbox,below=of Block3] {some text spanning three lines
with automatic line breaks.};
\draw (Start.south) -- ++ (0,-6mm) coordinate (a);
\path [line] (a) -| (Block1);
\path [line] (a) -| (Block3);
\end{tikzpicture}
\caption{my figure}
\label{fig-1}
\end{figure}
\end{document}