我正在尝试使用 tikz 实现以下目标:
我很难弄清楚如何告诉 tikz 开始在节点处绘制树;以及如何正确获取树的方向。 MWE 演示了我的问题。
任何帮助都将不胜感激。我确信这是一个非常简单的问题,但是我从未能够理解 tikz。
梅威瑟:
\documentclass[10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{trees}
\usetikzlibrary{decorations.pathreplacing}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\coordinate (i) at (0,0);
\coordinate (o) at (2.5cm,0);
\coordinate (s) at (60:2.5cm);
\draw [] (0,0) -- (60:2.5cm) -- (2.5cm,0) -- cycle;
\node at (s) [above = 1mm of s] {S};
\node at (i) [below = 1mm of i] {I};
\node at (o) [below = 1mm of o] {O};
\end{tikzpicture}
\begin{tikzpicture}[grow=up,
thestyle/.style={align=center},
sibling distance=4cm,
level distance=1.5cm
]
\node at (s) [anchor=north]{}
[edge from parent fork up]
child {node[thestyle] (s1) {S1}}
child {node[thestyle] (s2) {S2}}
child {node[thestyle] (s3) {S3}};
\end{tikzpicture}
\begin{tikzpicture}[grow=right,
thestyle/.style={align=center},
sibling distance=4cm,
level distance=1.5cm
]
\node at (i) [anchor=east]{}
[edge from parent fork down]
child {node[thestyle] (i1) {I1}}
child {node[thestyle] (i2) {I2}}
child {node[thestyle] (i3) {I3}};
\end{tikzpicture}
\begin{tikzpicture}[grow=left,
thestyle/.style={align=center},
sibling distance=4cm,
level distance=1.5cm
]
\node at (i) [anchor=west]{}
[edge from parent fork down]
child {node[thestyle] (o1) {O1}}
child {node[thestyle] (o2) {O2}}
child {node[thestyle] (o3) {O3}};
\end{tikzpicture}
\end{document}
答案1
除非您使用选项,否则每个tikzpicture
都是独立的remember picture
。
因为你想在同一张图片中绘制一个三角形和三棵树,所以最好使用scopes
\documentclass[10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{trees}
\usetikzlibrary{decorations.pathreplacing}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\coordinate (i) at (0,0);
\coordinate (o) at (2.5cm,0);
\coordinate (s) at (60:2.5cm);
\draw [] (0,0) -- (60:2.5cm) -- (2.5cm,0) -- cycle;
\node (S) at (s) [above = 1mm of s] {S};
\node (I) at (i) [below left = 1mm of i] {I};
\node (O) at (o) [below right = 1mm of o] {O};
\begin{scope}[grow=up,
thestyle/.style={align=center},
sibling distance=2cm,
level distance=1.5cm
]
\node at (S.north) [anchor=north]{}
[edge from parent fork up]
child {node[thestyle] (s1) {S1}}
child {node[thestyle] (s2) {S2}}
child {node[thestyle] (s3) {S3}};
\end{scope}
%
\begin{scope}[grow=left,
thestyle/.style={align=center},
sibling distance=2cm,
level distance=1.5cm
]
\node at (I.west) [anchor=east]{}
[edge from parent fork left]
child {node[thestyle] (i1) {I1}}
child {node[thestyle] (i2) {I2}}
child {node[thestyle] (i3) {I3}};
\end{scope}
%
\begin{scope}[grow=right,
thestyle/.style={align=center},
sibling distance=2cm,
level distance=1.5cm
]
\node at (O.east) [anchor=west]{}
[edge from parent fork right]
child {node[thestyle] (o1) {O1}}
child {node[thestyle] (o2) {O2}}
child {node[thestyle] (o3) {O3}};
\end{scope}
\end{tikzpicture}
\end{document}
答案2
这实际上不是对你问题的回答,伊格纳西非常好。
这是使用 tikz 直接绘制图片的替代方法,使用\foreach
命令以及根据scope
需要更改轴。
\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[line cap=round,line join=round]
\def\h {2} % triangle height (2/3)
\def\tw {1.5} % tree width
\def\th {0.25} % tree hegiht (branches)
\def\sep{0.25} % separations
\coordinate (S) at (90:\h);
\coordinate (I) at (210:\h);
\coordinate (O) at (330:\h);
\draw[thick] (S) -- (I) -- (O) -- cycle;
\foreach\c/\x in {S/0,I/1,O/-1}
{%
\begin{scope}[shift={(\c)},x={({1-abs(\x)},-\x*\x cm)},y={(-\x cm,{1-abs(\x)})}]
\node at (0,\sep) {\c};
\draw (-\tw,\th+3*\sep) -- (-\tw,3*\sep) -- (\tw,3*\sep) -- (\tw,\th+3*\sep);
\draw (0,\th+3*\sep) -- (0,-\th+3*\sep);
\foreach\i in {1,2,3}
{%
\node at ({\tw*(\i-2)},\th+4*\sep) {\c\i};
}
\end{scope}
}
\end{tikzpicture}
\end{document}