TikZ/PGF is a flexible and powerful package for creating graphics in LaTeX. It’s syntax can be somewhat overwhelming at the beginning. In this post I show the basics of tree drawing using TikZ and LuaLaTeX.
The following code only works with LuaLaTeX. Make sure you use the lualatex engine rather than standard tex/latex for typesetting.
Setup
First, we load TikZ and it’s graphdrawing.trees
library in the document preamble:
\usepackage{tikz} \usetikzlibrary{graphdrawing.trees}
In the document body, we prepare the figure environment. Here, we set nodes to be circles and edges to be directed vertices using the default arrow head.
\begin{tikzpicture}[nodes={draw, circle}, ->] % Tree structure \end{tikzpicture}
With that, we are ready to start drawing trees.
Tree drawing syntax
We start with a root node r
which has two children, node a
and node b
.
\node{r} child { node {a} } child { node {b} };
The library implements an algorithm which handles the distribution of children under each parent node. Therefore, we don’t have to worry about the placement of nodes in the tree.
Next, we might want to add extra levels, for example by adding three children under the child node a
.
This is not really complicated, we just have to make sure that we place children of node a
within the child node.
\node{r} child { node {a} child { node {c} } child { node {d} } child { node {e} } } child { node {b} };
Obviously, there is not enough space to add another three children to node b
. As a consequence, child nodes would overlap. However, this can be fixed by adding extra missing nodes and therefore increase the angle between child nodes. I will show how to do that in a moment.
Missing nodes
Missing nodes do just what they say, they add space for missing nodes. In the example below, node b
from the previous example was replaced by a missing node. Therefore, node a
is slightly shifted towards the left, leaving space for the ‘missing’ child node.
\node{r} child { node {a} child { node {c} } child { node {d} } } child [ missing ];
Using the concept of missing nodes, we can now add extra children to node b
, whereby we omit overlap with children of node a
.
\node{r} child { node {a} child { node {c} } child { node {d} } child { node {e} } } child [missing] child [missing] child {node {b} child { node {c} } child { node {d} } child { node {e} } };
Directed and undirected edges
TikZ supports different types of edges. For trees, the following are frequently used types of edges:
-> % Directed edge <- % Reverse directed edge -- % Undirected edge <-> % Bi-directed edge
We previously saw how to globally set the direction of edges. We can locally change the direction of a specific vertex using the edge
keyword. In the example below, we change edge a
to a bi-directed edge.
\begin{tikzpicture}[nodes={draw, circle}, ->] \node{r} child { node {a} edge from parent [<->] } child { node {b} }; \end{tikzpicture}
Node size and line thickness
The size of a node changes, depending on its content/text. The minimum size
imposes a minimal size on each node. By adjusting the value depending on the longest text/largest node size, we can ensure that all nodes have roughly the same size.
\begin{tikzpicture}[nodes={draw, circle}, minimum size=1cm]
We can further adjust the line thickness using the keywords thick
or very thick
.
\begin{tikzpicture}[nodes={draw, circle}, thick]
In the example below, I increased the line thickness to thick
and changed nodes to a minimum size of 0.7cm
. As you can see, all nodes are of the same size now. Whereas before, node b
was slightly bigger due to the size of the character.
Further options
Below are a few more options that might be useful to improve the appearance of the tree.
Changing the level distance (parent to child):
\begin{tikzpicture}[nodes={draw,circle}, level distance=2cm]
Changing the sibling distance (child to child):
\begin{tikzpicture}[nodes={draw,circle}, sibling distance=1cm]
Using rectangular rather than circular nodes:
\begin{tikzpicture}[nodes={draw,rectangle}]
Colored nodes:
\begin{tikzpicture}[nodes={draw,circle,fill=blue!20}]
Tree layout and graph syntax
The TikZ tree layout
and graph
syntax make tree building even easier. Instead of child
and node
keywords, curly brackets are used to indicate dependencies (edges). Siblings are separated by a comma and missing nodes are created by an empty node.
Here is the example from above in graph
syntax:
\documentclass{article} \usepackage{tikz} \usetikzlibrary{graphs} \usetikzlibrary{graphdrawing} \usegdlibrary{trees} \begin{document} \tikz[tree layout]\graph[nodes={draw, circle}] { r -> { a -> { c, d }, % b missing } }; \end{document}
Using graph
syntax, we can change the direction of the tree. For example grow=0
lets the tree grow to the right rather than downwards.
\tikz[tree layout, grow=0]\graph[nodes={draw, circle}] { r -> { a -> { c, d }, % b missing } }; \end{document}
For more details, please consider reading the documentation (page 405+) or drop me a comment below.
Fernando
Hello, I hope you are fine.
I am sorry if this question should be in other post but it has to do with tikz. The thing is that I am doing a Venn diagram just as the one in .
I did it using the following online exmaple: http://www.texample.net/tikz/examples/venn/
This is the minimal example:
When I compile the code using (for the pictures of the document in .png): “PdfLaTex+Bib(la)tex+PdfLaTex (x2) + Ver PDF” the blending works just fine.
However, since I have the pictures of the document also in .eps (they are better quality), when I compile the code using “LaTex+Bib(la)tex+LaTex (x2)+divps+ps2pdf+Ver PDF” the blending does not work properly and I get the circles one on top of the other as this:
http://s13.postimg.org/6qu79swxj/Venn.png
I would really appreciate any comments on this. I do not understand the different effects of the way to compile the code.
Thank you for the blog and all the help in different topics.
Regards.
tom
Hi Fernando,
Although not perfect, you might try the following:
Not sure why you think that EPS is better in quality than PDF…
Cheers, Tom
Fernando
Thank you very much Tom I will try it.
Well I do not know for sure about the quality of the images. The truth is that one of my colleagues told me that if the figure is in “.eps”, when converting to pdf using texmaker, the quality of the picture woul be better than using the format in “.png”.
I have many pictures in power point, so I did two things so I have two versions of the document:
1) I printed all the images in “.png” and then used those images for the latex document (using “PdfLaTex+Bib(la)tex+PdfLaTex (x2) + Ver PDF”).
2) I saved each slide of the power point as enhanced metafile, then using adobe illustrator I saved the images in “.eps” and then run texmaker (“LaTex+Bib(la)tex+LaTex (x2)+divps+ps2pdf+Ver PDF”). I did this to get a vector image that I understand are better than pixel for diagrams.
I am not using images in pdf and maybe I am mistaken about the qualities I was thinking, but if you think that is a better idea, should I save the images in “.pdf” instead of “.eps” or “.png”?
Thank you very much!
tom
Sure, you might loose quality with figures in png format. However, what you get from both approaches is pdf, which is equivalent to eps. I’d suggest to use pdf.
Cheers, Tom
Fernando
Hello man. Well first of all sorry for the order, I am lost in where to put my questions so what I´ve been doing is to reply to your answeres with different questions.
I am using Bibtex to cite online references but I am having a problem in the way Latex prints the results, not respecting the document margins.
For example, if I use the following:
I get the following result:
http://s18.postimg.org/lbvv18gu1/Example_Latex.png
Which is the number [6] and as you can see the reference goes way out the margins. This also happens in the names of tables and figures when their captions are long. I am using the option ““LaTex+Bib(la)tex+LaTex (x2)+divps+ps2pdf+Ver PDF”” and I think that may be what is causing the problem.
Any help will be much appreciated.
Best regards.
tom
Loading the breakurl package might fix your problem. Make sure you load it after
hyperref
!Fernando
Ok thank you very much for the comment.
I will try converting the power point slides to pdf and then use those images for texmaker.
Regards!
Fernando
Awesome Tom it works just fine for the Bibliography.
Sorry I have another question since I believe I didn´t ask it correctly.
The same happens in the list of figures/tables when the capton is too long, is there a way to solve that? I was looking for something like breakcaption but it seems the objective is different.
Thank you for all the help. Great blog.
Regards.
tom
Is this still related to URLs or does you normal text not break correctly in the LOF/LOT? Please provide a minimal working example.
Thanks, Tom
Fernando
Thank you Tom. Here is the result:
http://s3.postimg.org/4gwmg9083/Latex_Example.png
As you can see, the problem is when the caption is too long (as numbers 1.4, 2.3, 2.4, …)
The caption for figure 1.4 is: “\caption{Producción y consumo de cobre refinado; producción de concentrado de cobre.Exportaciones e importaciones de cobre refinado y concentrado de cobre} “.
Again this problem arises when using “LaTex+Bib(la)tex+LaTex (x2)+divps+ps2pdf+Ver PDF”
Regards!
tom
Hi Fernando,
Thanks for the pictures. It seems you are missing a space: “cobre._Exportaciones”. My guess is that once you add this space, the line-break will be added correctly. If not, this might help.
Best, Tom
Fernando
Thank you Tom.
I think the compilation is the problem since when I used “PdfLatex + Pdf” the alignment for all the tables was ok.
I will have a look at the post you say.
Regards!
suzuka185185
Dear Sir,
Thank you so much for a very nice topic.
I would like to ask a question. I would like to draw an arrow from (a) to (b) in the tree below, but I do not know how to do that. Could you please help me?
Thank you again.
Best regards,
Suzuka
tom
Hi there,
Give your nodes names using round brackets (
child {node (a){a}}
) and with these you can connect them with an arrow.Best, Tom