# How to Create Directed Graphs in LaTeX

How can one create a tree structure in LaTeX that begins with a single number and extends with an arbitrary number of arrows that connect numbers below with numbers above such that the head faces upward? For example, a tree of this form:

I see that there are similar trees without directed arrows as shown here. Similarly, I see that there are trees with directed arrows that are not similar as shown here. Is there a way to combine these two elements in tikz?

Is simply copying the following elements from \usepackage{tikz,forest} sufficient or are entirely different elements needed?

\documentclass{article}
\usepackage{tikz,forest}

\begin{document}
\texttt{grow} used in a Ti\textit{k}Z tree

\begin{tikzpicture}
\node{1}
child{node{child 1}}
child[grow=south]{node{child 2}
child child child
}
;
\end{tikzpicture}
end{forest}
end{document}


• Read about the edge from parent syntax and style in the TikZ manual. – Bordaigorl Sep 25 '14 at 23:24
• @Bordaigorl Thanks I'll take a look at that. – Blender Sep 25 '14 at 23:25
• If you take a minute to write a MWE where you draw the tree in the picture without the arrows I can show you how to set them – Bordaigorl Sep 25 '14 at 23:27

Here's a forest solution which numbers the nodes automatically:

\documentclass[tikz,border=5pt]{standalone}
\usepackage{forest}

\begin{document}

\bracketset{action character=@}% based on code from page 22 of forest's manual
\newcount\xcount
\edef\xtemp{$\noexpand{\the\xcount}$}%
\expandafter\bracketResume\xtemp
}
\begin{forest}
delay={%
content={#1}%
},
for tree={%
edge path={
\noexpand\path[<-, \forestoption{edge}]
(!u.parent anchor) -- (.child anchor)\forestoption{edge label};
},
}
@+
[\x
[\x
[\x
[\x
[\x]
[\x]
[\x]
]
[\x
[\x]
[\x]
]
[\x
[\x]
]
]
[\x]
]
]
\end{forest}

\end{document}


• Sorry, how would I centre this diagram? – Blender Oct 3 '14 at 12:39
• @Blender With respect to what? Do you just want to wrap it in \begin{center}...\end{center}? – cfr Oct 3 '14 at 14:32

You could try the new graphs stuff in the latest PGF with lualatex:

\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{graphs,graphdrawing,arrows.meta}
\usegdlibrary{trees}
\begin{document}
\begin{tikzpicture}[>=Stealth]
\graph  [tree layout, grow=down]{
1 <- 2 <- {
3 <- {
5 <- {10,11,12}, 6 <- {13,14}, 7 <- {,15}
},
4 <- {,/}
};
};
\end{tikzpicture}
\end{document}


• If I already have a document with a \documentclass how do I add in this new document class? Simply adding it below the other results in an error. – Blender Oct 3 '14 at 12:26
• @Blender with another document class (e.g., article) you would just replace the first line in the example above with \documentclass{article} \usepackage{tikz}. – Mark Wibrow Oct 4 '14 at 10:55