[Tex/LaTex] TikZ: Drawing directed trees using tree layout, child{…}-syntax and lualatex

luatextikz-trees

There seems to be no way to draw directed edges using the new (?) tree graph layout and the child-syntax.

\documentclass{standalone}

\usepackage{tikz}
\usepackage{lmodern}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{graphs}
\usetikzlibrary{graphdrawing}
\usegdlibrary{trees}

\tikzset{
  ->, % doesn't work
  nodes={draw, circle, minimum size = .5cm},
  >=Stealth[], % doesn't work
}

\begin{document}

\tikz[tree layout,
edge from parent/.style={draw,-latex}, % doesn't work
-> % doesn't work
] {
  \node {$\land$}
  child {
    node {$\lnot$}
    child {
      node {$a$}
    }
  }
  child {
    node {$\rightarrow$}
    child {
      node {$b$}
    }
    child {
      node {$c$}
    }
  };
}

\end{document}

Prove me wrong!

enter image description here

EDIT: I think I do use the new graph layout engine because using the conventional tikzpicture environment, the tree looks like this:

enter image description here

Best Answer

If you are using graph syntax, directed edges are specified within the tree specification itself. For example,

{ r -> a }

indicates a directed edge from r to a.

The whole point of the graph drawing libraries is that you specify the relationships and let Lua figure out the detailed structure.

Using this syntax, the directed edges are no problem. And, thanks to Gonzalo Medina's comment, I'm now able to get mathematical content into the nodes as well.

So, for example, using the graph syntax, I can produce

graph

using this specification:

% modified from Gonzalo Medina's comment (below)
\tikz [tree layout] \graph [math nodes, nodes={draw, circle, minimum size = .5cm}, sibling sep=0pt]
{aaaa [as=$\land$] -> {aa [as=$\lnot$] -> a , aaa [as=$\rightarrow$] -> {b , c}} };

If your graphs are all trees i.e. have a single root, you might like to use a specialised tree-drawing package such as forest, qtree or tikz-qtree instead. If they are not all trees, you can still do this but only by tricking the package into thinking you are really drawing a tree, so it gets more complicated.

For your tree, for example, the forest specification looks like this:

\begin{forest}
  for tree={
    edge = {->},
    circle,
    minimum size=5mm,
    inner sep=0pt,
    draw,
    math content,
    tier/.wrap pgfmath arg={tier #1}{level()},
    anchor=center
  },
  [\land
    [\lnot
      [a]
    ]
    [\rightarrow
      [b]
      [c]
    ]
  ]
\end{forest}

tree

Here including maths in the nodes is a bit more straightforward, but obviously you have to specify the precise structure of the tree you want - you can't just specify the relationships as you can with the graph drawing libraries.

Complete Code

\documentclass[tikz,multi,border=10pt]{standalone}
\usetikzlibrary{arrows.meta,graphs,graphdrawing}
\usegdlibrary{trees}
\usepackage{forest}
\tikzset{
  >=Stealth[], % does work
}

\begin{document}

% modified from 406 of manual, and using Gonzalo Medina's comment (below)
\tikz [tree layout] \graph [math nodes, nodes={draw, circle, minimum size = .5cm}, sibling sep=0pt]
{aaaa [as=$\land$] -> {aa [as=$\lnot$] -> a , aaa [as=$\rightarrow$] -> {b , c}} };

\begin{forest}
  for tree={
    edge = {->},
    circle,
    minimum size=5mm,
    inner sep=0pt,
    draw,
    math content,
    tier/.wrap pgfmath arg={tier #1}{level()},
    anchor=center
  },
  [\land
    [\lnot
      [a]
    ]
    [\rightarrow
      [b]
      [c]
    ]
  ]
\end{forest}

\end{document}

EDIT

If you prefer to stick to the old syntax for trees, you can do that so long as you don't mix it in problematic ways with the graph drawing environment.

\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{arrows.meta}
\tikzset{
  my tree/.style={
    ->,
    nodes={draw, circle, minimum size = .5cm},
    >=Stealth[],
  },
}
\begin{document}
\tikz [ my tree ]
  \node {$\land$}
  child {
    node {$\lnot$}
    child {
      node {$a$}
    }
  }
  child {
    node {$\rightarrow$}
    child {
      node {$b$}
    }
    child {
      node {$c$}
    }
  };
\end{document}

I don't get the problems with overlapping nodes shown in your screenshot provided I do not load the graphdrawing TikZ library:

old tree syntax