这是我当前的图片代码:
\documentclass[a4paper,12pt]{article}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\resizebox{.5\textwidth}{!}{
\tikzset{level 1/.style={level distance=0.7cm, sibling distance=3cm}}
\tikzset{level 2/.style={level distance=0.7cm, sibling distance=6cm}}
\tikzset{bag/.style={text centered,yshift=-0.2cm}}
\begin{tikzpicture}[grow=down, -stealth]
\node[bag]{$S_0{:}(B,true,0)$}
child{ edge from parent node[right]{$\_ DS$}; \node[bag]{$S_1{:}(R_{good})$}
child{ edge from parent node[right]{and}; \node[bag]{$S_2{:}(and)$}
child[missing]
child{ edge from parent node[right=0.1cm]{$[else]$}; \node[bag]{$S_3{:}(A_1)$}
}
child{ edge from parent node[right=0.9cm]{$[if]$}; \node[bag]{$S_4{:}(R_{good})$}
}
}
};
\end{tikzpicture}}
\end{document}
然而结果是,我想将从 s2 到 s4 的线改为 -|,我在图像中使用红线描述了这一点,我尝试使用代码child{ edge from parent[-|]node[right=0.9cm]{$[if]$}; \node[bag]{$S_4{:}(R_{good})$}; \node[bag]{$S_3{:}(A_1)$}
但它不起作用,有人可以帮帮我吗?
答案1
从您发布的图片来看,树似乎不是最方便使用的结构。在下面的代码中,我展示了另一个(在我看来更简单的)选项:
\documentclass[a4paper,12pt]{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\tikzset{
bag/.style={text centered},
aux/.style={font=\footnotesize}
}
\begin{tikzpicture}[node distance=5mm and 20mm,-stealth]
\node[bag] (s0) {$S_0{:}(B,true,0)$};
\node[bag,below =of s0] (s1) {$S_1{:}(R_{good})$};
\node[bag,below =of s1] (s2) {$S_2{:}(and)$};
\node[bag,below =of s2] (s3) {$S_3{:}(A_1)$};
\node[bag,right =of s3] (s4) {$S_4{:}(R_{good})$};
\draw (s0) -- node[aux,auto] {$\_DS$} (s1);
\draw (s1) -- node[aux,auto] {and} (s2);
\draw (s2) -| node[aux,auto,near start] {$[if]$} (s4);
\draw (s2) -- node[aux,auto] {$[else]$} (s3);
\end{tikzpicture}
\end{document}