10

I want to draw something like this:

enter image description here

This is my tikz code:

\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}

And this is the output:

enter image description here

I have two questions:

  1. How to put tick and cross mark as shown in the first figure?
  2. How to draw a rectangle containing two node? Example: I want to draw a single rectangle enclosing ! and tcas_eq (right side of the root). How to do that?

2 Answers 2

13

One way would be to label the nodes so that you have a coordinate to refer to them and then add a node with the desired symbol, and similarly to draw a rectangle around some other nodes.

enter image description here

Notes:

Refernece:

Code:

\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}
7

Another option is to use label option in your nodes. Here I have used \tikzset instead of \tikzstyle. I have also included additional styles bag2 and bag_rect2. I have also replaced your text width and text centered combo with align=center since there is no very long nodes in your example. But you can always ignore this depending on your actual use case. The positions of the x's and check marks are not as prettily positioned but you can always node positioning as in Peter Grill's answer.

The modified code

\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}

The output

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .