3

I want to add the green arcs, labels and boxes to this tree. (The labels will be black and red, as in the not quite minimal MWE). I don't need a programmatic solution. I can finish the job if I see how to do it once for each of the three kinds of green arcs.

enter image description here

MWE:

\documentclass{standalone}
\usepackage{tikz-qtree}
\newcommand{\anti}[1]{%
    \ensuremath{{\color{red}\overline{#1}}}%
}
\newcommand{\ee}[1]{%
    node[midway,left] {#1}
}
\begin{document}
\begin{tikzpicture}[every tree node/.style={draw,rectangle,align=center},
   level distance=1.5cm,sibling distance=0.5cm, 
   edge from parent path={(\tikzparentnode) -- (\tikzchildnode)}]
  \Tree [.0
    \edge \ee{1};
    [.1
      \edge \ee{1};
      [.2
        \edge \ee{1};
        [.3
          \edge \ee{1}; 4
          \edge \ee{\anti{2}}; \anti{5}
        ]
        \edge \ee{\anti{2}};
        [.\anti{4}
          \edge \ee{1}; \anti{5}
          \edge \ee{\anti{2}}; 6
        ]
      ]
      \edge \ee{\anti{2}};
      [.\anti{3}
        \edge \ee{1};
              [.\anti{4}
                \edge \ee{1}; \anti{5}
                \edge \ee{\anti{2}}; 6
              ]
              \edge \ee{\anti{2}};
              [.5
                \edge \ee{1}; 6
                \edge \ee{\anti{2}}; \anti{7}
              ]
      ]
    ]
%    second half
    \edge \ee{\anti{2}};          
    [.\anti{2}
      \edge \ee{1};
      [.\anti{3}
        \edge \ee{1};
              [.\anti{4}
                \edge \ee{1}; \anti{5}
                \edge \ee{\anti{2}}; 6
              ]
              \edge \ee{\anti{2}};
              [.5
                \edge \ee{1}; 6
                \edge \ee{\anti{2}}; \anti{7}
              ]
      ]
      \edge \ee{\anti{2}};
            [.4
              \edge \ee{1};
              [.5
                \edge \ee{1}; 6
                \edge \ee{\anti{2}}; \anti{7}
              ]
              \edge \ee{\anti{2}};
                    [.\anti{6}
                \edge \ee{1}; \anti{7}
                \edge \ee{\anti{2}}; 8
                    ]
              ]
    ]
    ]
  ]
\end{tikzpicture}
\end{document}
2
  • 1
    This would be a lot easier to do with forest. Is that a possibility for you?
    – Alan Munn
    Commented Jan 11 at 1:59
  • @AlanMunn I thought that might be the case. Yes, it's possible. I don't have many of these to draw, so I want to minimize the amount of new tools I need to learn to use. This complicated tree came up after I learned enough qtree to do the easy ones. Commented Jan 11 at 12:40

2 Answers 2

1

As a follow-up question, and being a different topic, here's a separate answer with a different focus.

Basic ideas:

  • introduce named nodes as shown before
  • define useful styles, to be used with the next item
  • place new \nodes with these styles at the remembered postions (named)
  • \draw some lines between them for the parallels

If you look carefully tikz-qtree seems to connect its tree nodes slightly off-center, at least sometimes. So, though the styles sq and crc define a minimum size (basically making them a square or circle) this attempt fails, resulting in a non-parallel line:

    \node[sq] (X0) at (A0){};   
    \node[crc] (X1) at (C1){}; 
    \draw[teal] (X0.south west) -- (X1.south east); % <<< no parallel line !

So the alternative I choose is:

  • using polar notation instead, like (X1.340) (intersection with nodes shape at 340deg)
  • adjusting angles "by eye"

You could also use e.g. ([yshift=-2mm] X1), but this probably won't result in a real advantage.

For a more uniform visual result you should perhaps also require a useful minimum size or minimum width for all nodes of the tree. If these values are large enough, all black rectangles will have the same dimensions, besides my A4 one etc.

result

\documentclass[border=3mm]{standalone}  % <<<
\usepackage{tikz}   % <<< see manual
\usepackage{tikz-qtree}
\newcommand{\anti}[1]{%
    \ensuremath{{\color{red}\overline{#1}}}%
}
\newcommand{\ee}[1]{%
    node[midway,left] {#1}
}
\begin{document}
\begin{tikzpicture}[
    every tree node/.style={draw,rectangle,align=center},
    level distance=1.5cm,
    sibling distance=0.5cm, 
    edge from parent path={(\tikzparentnode) -- (\tikzchildnode)},
    sq/.style ={draw=teal!100!black!80, minimum size=9mm},
    crc/.style={draw=teal!100!black!80, minimum size=8mm,circle},
  ]
  \Tree [.\node(A0){0}; % <<<
    \edge \ee{1};
    [.\node(C1){1}; % <<<
      \edge \ee{1};
      [.2
        \edge \ee{1};
        [.3
          \edge \ee{1}; 4
          \edge \ee{\anti{2}}; \anti{5}
        ]
        \edge \ee{\anti{2}};
        [.\node(B4){\anti{4}};  % <<<
          \edge \ee{1}; \anti{5}
          \edge \ee{\anti{2}}; 6
        ]
      ]
      \edge \ee{\anti{2}};
      [.\anti{3}
        \edge \ee{1};
              [.\anti{4}
                \edge \ee{1}; \anti{5}
                \edge \ee{\anti{2}}; 6
              ]
              \edge \ee{\anti{2}};
              [.5
                \edge \ee{1}; 6
                \edge \ee{\anti{2}}; \anti{7}
              ]
      ]
    ]
%    second half
    \edge \ee{\anti{2}};          
    [.\anti{2}
      \edge \ee{1};
      [.\anti{3}
        \edge \ee{1};
              [.\anti{4}
                \edge \ee{1}; \anti{5}
                \edge \ee{\anti{2}}; 6
              ]
              \edge \ee{\anti{2}};
              [.5
                \edge \ee{1}; 6
                \edge \ee{\anti{2}}; \anti{7}
              ]
      ]
      \edge \ee{\anti{2}};
            [.\node(A4){A4};    % <<<
              \edge \ee{1};
              [.5
                \edge \ee{1}; 6
                \edge \ee{\anti{2}}; \anti{7}
              ]
              \edge \ee{\anti{2}};
                    [.\anti{6}
                \edge \ee{1}; \anti{7}
                \edge \ee{\anti{2}}; 8
                    ]
              ]
    ]
    ]
  ]
  
    % ~~~ extra-nodes and connectors ~~~~~~
    \node[sq] (X0) at (A0){};   
    \node[crc] (X1) at (C1){}; 
    \draw[teal] (X0.south west) -- (X1.340);    
    
    
    % ~~~ some connectors ~~~~~~~~~
    \draw[teal,dashed] (A0) -- (A4);
    \draw[teal,
          loosely dashed, <->] 
          (A0) to[out=-10,in=90] node[sloped,above]{off-road}(A4);
%   \draw[dotted,blue] (B4) -- node[below,pos=.7,anchor=west]{note!}(A0);
\end{tikzpicture}
\end{document}
1
  • 1
    Thanks. Upvoted both. Will accept one or the other when I decide which to use. Commented Jan 11 at 22:09
3

Here's a way to do it:

  • see ch. 4 in the manual
  • place extra nodes
  • \draw later
  • you may need to modify your anti-macro

result

Besides the controls statement as given in the manual I tend to favor replacing -- in a \draw command by to[out=angle,in=angle], which I think is easier to visualize, hence to control.

\documentclass{standalone}
\usepackage{tikz}   % <<< see manual
\usepackage{tikz-qtree}
\newcommand{\anti}[1]{%
    \ensuremath{{\color{red}\overline{#1}}}%
}
\newcommand{\ee}[1]{%
    node[midway,left] {#1}
}
\begin{document}
\begin{tikzpicture}[every tree node/.style={draw,rectangle,align=center},
   level distance=1.5cm,sibling distance=0.5cm, 
   edge from parent path={(\tikzparentnode) -- (\tikzchildnode)}]
  \Tree [.\node(A0){A0};    % <<<
    \edge \ee{1};
    [.1
      \edge \ee{1};
      [.2
        \edge \ee{1};
        [.3
          \edge \ee{1}; 4
          \edge \ee{\anti{2}}; \anti{5}
        ]
        \edge \ee{\anti{2}};
        [.\anti{4}
          \edge \ee{1}; \anti{5}
          \edge \ee{\anti{2}}; 6
        ]
      ]
      \edge \ee{\anti{2}};
      [.\anti{3}
        \edge \ee{1};
              [.\anti{4}
                \edge \ee{1}; \anti{5}
                \edge \ee{\anti{2}}; 6
              ]
              \edge \ee{\anti{2}};
              [.5
                \edge \ee{1}; 6
                \edge \ee{\anti{2}}; \anti{7}
              ]
      ]
    ]
%    second half
    \edge \ee{\anti{2}};          
    [.\anti{2}
      \edge \ee{1};
      [.\anti{3}
        \edge \ee{1};
              [.\anti{4}
                \edge \ee{1}; \anti{5}
                \edge \ee{\anti{2}}; 6
              ]
              \edge \ee{\anti{2}};
              [.5
                \edge \ee{1}; 6
                \edge \ee{\anti{2}}; \anti{7}
              ]
      ]
      \edge \ee{\anti{2}};
            [.\node(A4){A4};    % <<<
              \edge \ee{1};
              [.5
                \edge \ee{1}; 6
                \edge \ee{\anti{2}}; \anti{7}
              ]
              \edge \ee{\anti{2}};
                    [.\anti{6}
                \edge \ee{1}; \anti{7}
                \edge \ee{\anti{2}}; 8
                    ]
              ]
    ]
    ]
  ]
    % ~~~ some connectors ~~~~~~~~~
    \draw[teal,dashed] (A0) -- (A4);
    \draw[teal,
          loosely dashed, <->] 
          (A0) to[out=-10,in=90] node[sloped,above]{off-road}(A4);
    
\end{tikzpicture}
\end{document}

P.S.

Though you can use your \anti macro inside a node, it's probably not the best way to do it:

...
        \edge \ee{\anti{2}};
        [.\node(B4){\anti{4}};  % <<<
          \edge \ee{1}; \anti{5}
          \edge \ee{\anti{2}}; 6
        ]
...
    \draw[dotted,blue] (B4) -- node[below,pos=.7,anchor=west]{note!}(A0);
\end{tikzpicture}
\end{document}

This code:

  • defines \node(B4){\anti{4}}; to the lower left
  • draws a blue dotted line to (A0)
  • puts a node at position pos=.7 along that path (which is just a straight line here)
  • inserts a node with text there, below
  • anchors it west, i.e.makes text appear like starting always from this nodes right side

result2

3
  • Looks like something I can work with. I see how to make the tag for the node different from the contents of the node. How do I change the style of the nodes connected by the new edges? In my picture it's a hand drawn extra box, but an oval or circle or (and?) color change would do. I also need a parallel new edge in some places. Commented Jan 11 at 16:56
  • Let me think about the extra shapes: there are ways to do it. // Can you pleas give one or two examples about parallel edges? You could add a sketch to your question (though it perhaps qualifies for a new one …). . thank you
    – MS-SPO
    Commented Jan 11 at 17:41
  • 1
    The sketch in the question shows a green edge parallel to the black edge from the root 0 to node 1. Commented Jan 11 at 17:54

You must log in to answer this question.

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