我想要画这样的东西:
这是我的 tikz 代码:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usetikzlibrary{trees}
\tikzstyle{bag} = [text width=8em,
text centered]
\tikzstyle{bag_mod} = [text width=2em,
text centered]
\tikzstyle{bag_rect} = [draw=black,rectangle, black,text width=8em,
text centered]
\tikzstyle{bag1} = [draw=black,rectangle, black,text width=4em,
text centered]
\begin{document}
\begin{tikzpicture}[-,>=stealth',level/.style={sibling distance = 2.5cm/#1,
level distance = 1cm}]
\node [bag] {$||$}
child{node[bag] {$\&\&$}
child{node[bag] {$enabled$}}
child{node[bag] {$\&\&$}
child{node[bag1,left] {$tcas\_eq$}}
child{node[bag_rect,right] {$intent\_not\_known$}}
}
}
child{node[bag] (A) {$!$}
child{node[bag] (B) {$tcas\_eq$}}
}
;
%\node [draw=red, fit= (A) (B)] {};
\end{tikzpicture}
\end{document}
输出结果如下:
我有两个问题:
- 如何像第一张图那样打勾和叉?
- 如何绘制包含两个节点的矩形?示例:我想绘制一个包围(根节点右侧)的矩形。该怎么做
!
?tcas_eq
答案1
一种方法是标记节点,以便您有一个坐标来引用它们,然后添加具有所需符号的节点,并以类似的方式在其他节点周围绘制一个矩形。
笔记:
- 我不太清楚为什么需要添加
xshift
。由于某种原因,节点比应有的要大(也许是一个inner sep
问题outer sep
)。 - 来自
Cross
我怎样才能得到一个大十字来表示广义笛卡尔积?。
參考文獻:
- 这是一个简短的教程风格的答案,展示了该库的使用方法
fit
:如何绘制嵌套节点?
代码:
\documentclass{article}
\usepackage{tikz}
\usepackage{amssymb}
%\usetikzlibrary{fit}
\usetikzlibrary{arrows}
\usetikzlibrary{trees}
\tikzstyle{bag} = [text width=8em,
text centered]
\tikzstyle{bag_mod} = [text width=2em,
text centered]
\tikzstyle{bag_rect} = [draw=black,rectangle, black,text width=8em,
text centered]
\tikzstyle{bag1} = [draw=black,rectangle, black,text width=4em,
text centered]
\newcommand{\Cross}{$\mathbin{\tikz [x=1.4ex,y=1.4ex,line width=.2ex, red] \draw (0,0) -- (1,1) (0,1) -- (1,0);}$}%
\newcommand{\Checkmark}{$\color{green}\checkmark$}
\begin{document}
\begin{tikzpicture}[-,>=stealth',level/.style={sibling distance = 2.5cm/#1,
level distance = 1cm}]
\node [bag] {$||$}
child{node[bag] (X0) {$\&\&$}
child{node[bag] (enabled) {$enabled$}}
child{node[bag] (X1) {$\&\&$}
child{node[bag1,left] (X2) {$tcas\_eq$}}
child{node[bag_rect,right] (intent) {$intent\_not\_known$}}
}
}
child{node[bag] (A) {$!$}
child{node[bag] (B) {$tcas\_eq$}}
}
;
\node at ([xshift=2.5em]enabled.north west) {\Cross};
\node at ([xshift=3.0em]X0.north west) {\Cross};
\node at ([xshift=-0.5em]X2.north west) {\Cross};
\node at ([xshift=-3.0em]X1.north east) {\Checkmark};
\node at ([xshift=1em]intent.north east) {\Checkmark};
% Use fit library
%\node [draw=blue, ultra thick, fit=(A) (B), shape=rectangle, inner sep=0pt] {};
\draw [draw=blue, ultra thick]
([shift={(-2.0em,-0.25ex)}]A.north) rectangle ([xshift=2.0em]B.south);
\end{tikzpicture}
\end{document}
答案2
label
另一个选项是在节点中使用选项。在这里我使用了\tikzset
而不是\tikzstyle
。我还添加了其他样式bag2
和bag_rect2
。我还用 替换了text width
和text centered
组合,align=center
因为您的示例中没有非常长的节点。但您可以根据实际使用情况随时忽略这一点。 和复选标记的位置x
没有那么漂亮,但您可以像 Peter Grill 的答案中那样随时进行节点定位。
修改后的代码
\documentclass{article}
\usepackage{amssymb}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usetikzlibrary{trees}
\tikzset{
bag/.style = {align=center, outer sep=0pt, inner sep=0pt,
label=above left:\color{red}$\times$},
bag2/.style = {align=center, outer sep=0pt,
inner sep=0pt,label=north east:\color{green}$\checkmark$},
bag_mod/.style = {align=center},
bag_rect/.style = {draw=black,rectangle, black,
label=above right: \color{green}$\checkmark$},
bag_rect2/.style = {draw=black,rectangle, black,
label=above left: \color{red}$\times$},
bag1/.style = {draw = black, rectangle, black, align = center}
}
\begin{document}
\begin{tikzpicture}[-,>=stealth',level/.style={sibling distance = 2.5cm/#1,
level distance = 1cm}]
\node [bag_mod] {$||$}
child{node[bag] {$\&\&$}
child{node[bag] {$enabled$}}
child{node[bag2] {$\&\&$}
child{node[bag_rect2,left] {$tcas\_eq$}}
child{node[bag_rect,right] {$intent\_not\_known$}}
}
}
child{node[bag_mod] (A) {$!$}
child{node[bag2] (B) {$tcas\_eq$}}
}
;
%\node [draw=red, fit= (A) (B)] {};
\end{tikzpicture}
\end{document}