我在如何将文本与节点对齐以及如何修剪树方面遇到了问题。到目前为止,我有以下代码,请忽略错误,我不熟悉 TikZ 或 LaTeX。
\usepackage{tikz} % Flow chart
\usetikzlibrary{shapes,arrows,positioning,calc,chains}
\usepackage{float, subcaption}
\begin{document}
\tikzstyle{test} = [rectangle, fill=white!20,
text width=0.1em, rounded corners, minimum height=8em]
\tikzset{
treenode/.style = {align=center, inner sep=0pt, text centered,
font=\sffamily},
arn_n/.style = {treenode, circle, black, font=\sffamily, draw=black,
fill=white, text width=1.5em},% arbre rouge noir, noeud noir
arn_r/.style = {treenode, circle, black, draw=black,
text width=1.5em},% arbre rouge noir, noeud rouge
arn_x/.style = {treenode, rectangle, draw=black,
minimum width=0.5em, minimum height=0.5em}% arbre rouge noir, nil
}
\begin{figure}[ht]
\centering
\begin{subfigure}[b]{0.26\textwidth}
\centering
\begin{tikzpicture}[->,>=stealth',level/.style={sibling distance = 3cm/#1,
level distance = 1.5cm},baseline=(current bounding box.center)]
\node [arn_n] {33}
child{ node [arn_r] {15}
}
child{ node [arn_r] {47}
}
;
\end{tikzpicture}
\caption{}
\end{subfigure}%
\begin{subfigure}[b]{0.20\textwidth}
\centering
\begin{tikzpicture}
% Place nodes
\node [test] (empty) { } ;
\node [right of=empty, node distance=2cm] (empty1) {} ;
% Draw edges
\path [line] (empty) -- (empty1);
\end{tikzpicture}
\end{subfigure}
\begin{subfigure}[b]{0.26\textwidth}
\centering\begin{tikzpicture}[->,>=stealth',level/.style={sibling distance = 3cm/#1,
level distance = 1.5cm}, baseline=(current bounding box.center)]
\node [arn_n] {33}
child{ node [arn_r] {15}
}
child{ node [arn_r] {47}
}
;
\end{tikzpicture}
\caption{}
\end{subfigure}
\caption{Pruned by infeasibility} \label{fig:3}
\end{figure}
这给了我以下信息:
现在我想得到这样的结果:
我想在节点旁边添加文本,以便我可以说明上限和下限。我还想剪切/修剪树的部分。
我希望有人能帮帮忙。
此致
答案1
作为起点:
\documentclass{article}
\usepackage{tikz} % Flow chart
\usetikzlibrary{arrows, calc, positioning}
\usepackage{float, subcaption}
\begin{document}
\begin{figure}[ht]
\tikzset{
every label/.append style = {label distance=1pt, inner sep=1pt, align=left},
base/.style = {draw,
inner sep=1pt, minimum size=5mm, font=\sffamily},
arn_c/.style = {base, circle, fill=#1},% arbre rouge noir, noeud noir
arn_c/.default = white,
arn_r/.style = {base, rectangle, draw, fill=#1},% arbre rouge noir, nil
arn_r/.default = white,
}
\centering
\begin{subfigure}[b]{0.3\textwidth}
\centering
\begin{tikzpicture}[->,>=stealth',
level/.style={sibling distance = 22mm,
level distance = 1.5cm},
]
\node (a) [arn_c,label={right:$\alpha=3$}] {33}
child{ node (b) [arn_c] {15}}
child{ node (c) [arn_c] {47}}
;
\path (a) -- node [sloped,label={[font=\footnotesize]60:$\beta$ cut}] {$\|$} (c);
\end{tikzpicture}
\caption{}
\end{subfigure}\quad
\begin{tikzpicture}[baseline=-17mm]
\draw (0,0) -- (2,0);
\end{tikzpicture}\quad
\begin{subfigure}[b]{0.3\textwidth}
\centering
\begin{tikzpicture}[->,>=stealth',
level/.style={sibling distance = 22mm,
level distance = 1.5cm},
]
\node [arn_c,label={[blue]left:$\alpha=3$}] {33}
child{ node [arn_c] {15}}
child{ node [arn_c] {47}}
;
\end{tikzpicture}
\caption{}
\end{subfigure}
\caption{Pruned by infeasibility}
\label{fig:3}
\end{figure}
\end{document}
如果您喜欢在给定的链接上绘制图像中显示的树,只需添加具有所需节点样式的更多子节点,然后对于旁边的文本使用标签,就像上面的 MWE 对根节点所做的那样。
编辑:对于多行标签,您需要添加align=...
到样式的定义中label
。例如,如果大多数标签位于字体大小为的节点的右侧\tiny
,则
every label/.append style = {label distance=1pt, inner sep=1pt,
align=left, font=\tiny}, % <-- added
例外情况请本地更改此设置。例如
\path (a) -- node [sloped,label={[font=\small]60:$\beta$ cut}] {$\|$} (c);
现在,这两个例子都在 MWE 中被考虑,并且出现在树的图像中。