banner



How To Draw Trees In Latex

I am going to put in a plea on behalf of forest. Although forest is extremely flexible and powerful, information technology tin can likewise exist used very just. Like qtree, it uses a simple, concise syntax which 'reads similar a tree' but I found woods's syntax a bit easier to learn. (However, this may be because I learnt qtree first.) Like tikz-qtree, it gives you lot all the power of TikZ but, unlike qtree, you exercise not take to change packages in order to use that power as forest is already using TikZ underneath. Moreover, in addition to all the power of TikZ, you take all the power of wood itself. And that is very significant: powerful, specialised support for drawing trees of all kinds, manually, automatically and/or dynamically. Have a look at examples on this site for a sense of the possibilities.

For simple trees, however, forest is very elementary.

This reply consists of two parts. The first part applies to the current version of forest (version two). The second, which consists of my original answer, applies to the earlier version of forest (version 1). The changes are not great just, if you are new to the package, yous will find it easier to follow if you lot read the section which applies to the version you are using without modifications.


Electric current forest: version 2


Hither is a forest version of the lawmaking for the tree posted in Antal Due south-Z'due south answer.

          \documentclass[tikz,border=10pt]{standalone} \usepackage[linguistics]{forest} \brainstorm{document} \begin{forest}   [IP     [NP      [Det        [\textit{the}]      ]      [Northward$'$        [N          [\textit{bundle}]        ]      ]     ]     [I$'$       [I         [\textsc{3sg.Pres}         ]       ]       [VP         [5$'$           [V             [\textit{is}]           ]           [AP             [Deg               [\textit{extremely}]             ]             [A$'$               [A                 [\textit{straightforward}]               ]               [CP                 [\textit{to wield}, roof]               ]             ]           ]         ]       ]     ]   ] \end{forest} \end{document}                  

The basic idea is that every node in the tree is enclosed in square brackets. The node's subtree is included in the aforementioned brackets, if information technology has one.

Annotation that there are no dots for branch points and that there is no need to leave a space before a closing square bracket. In fact, the above code is equivalent to

          \begin{woods}   [IP[NP[Det[\textit{the}]][N$'$[N[\textit{package}]]]][I$'$[I[\textsc{3sg.Pres}]][VP[Five$'$[V[\textit{is}]][AP[Deg[\textit{extremely}]][A$'$[A[\textit{straightforward}]][CP[\textit{to wield}, roof]]]]]]]] \end{wood}                  

Obviously, this is anything but easy to read - hence my preference for lots of 'unnecessary' space.

forest cannot, however, abide a blank line and these must be avoided.

The above trees produce the post-obit output:

tree

How to plough a tree into the bracket specification woods uses

Offset with the root and put it inside a forest environment and inside square brackets:

          \begin{forest}   [IP% root % remainder of tree volition become here   ] \end{forest}                  

The rest of the tree consists of one or more smaller copse. These are subtrees of the root node. In this case, we accept 2 subtrees so nosotros offset with the root of each subtree and add each of them in its ain pair of square brackets.

          \begin{forest}   [IP% root     [NP% root of subtree % rest of subtree will go hither     ]     [I$'$% root of subtree % residual of subtree volition go hither     ]   ] \end{forest}                  

So you can piece of work on each of the subtrees in turn and echo the performance.

Special styles

Because nosotros are interested in this question in copse for linguistics, I added the selection linguistics when loading the package.

          \usepackage[linguistics]{forest}                  

This does two things: (one) loads the linguistics library, making various styles of use in linguistics available (east.g. the roof used to a higher place); (two) changes the default advent of trees.

If nosotros only wanted some trees to utilise the library's defaults (perhaps some of our trees are linguistics copse and some are some other kind of tree), nosotros could instead utilise

          \usepackage{wood} \useforestlibrary{linguistics}                  

in the preamble. Then we could apply

          \forestapplylibrarydefaults{linguistics}                  

inside a local TeX scope in the body of the document to apply the defaults only to selected copse. For instance,

          {\forestapplylibrarydefaults{linguistics} \begin{forest}   <specification 1> \end{forest}}  \begin{forest}   <specification ii> \end{forest}                  

would apply lingustics defaults to the tree given by <specification 1> but not to that given by <specification 2>.

But we'll assume that all the trees in our electric current document are linguistics trees for now.

To brand the triangular roof, we but add , roof to the appropriate node and forest applies its triangular roof style.

If you later on want to make more complex trees, you can also use TikZ styles in just the same manner. Adding , text=ruddy to a node will plow the text of the node red, for example.

                      [N$'$, text=scarlet        [N          [\textit{package}]        ]      ]                  

red node

Conditional styling saves typing

Yous can also, if y'all wish, relieve changing the font for every concluding node. If most of the concluding nodes should exist italicised, you can say

          if north children=0{% if the node doesn't have any children of its own   font=\itshape }{},                  

To utilise this provisional to the whole tree and, hence, italics to all concluding nodes, we tin can say

                      for tree={     if n children=0{       font=\itshape     }{},   }                  

afterwards \begin{forest} and before specifying the tree itself. This is known as the preamble of the tree.

Yous can then override this for the single minor-caps node, by saying , font=\scshape after specifying the node's contents.

Consummate lawmaking for revised tree

          \documentclass[tikz,border=10pt]{standalone} \usepackage[linguistics]{forest} \brainstorm{document} \brainstorm{forest}   for tree={     if n children=0{       font=\itshape     }{},   }   [IP     [NP      [Det        [the]      ]      [N$'$        [Northward          [package]        ]      ]     ]     [I$'$       [I         [3sg.Pres, font=\scshape]       ]       [VP         [V$'$           [V             [is]           ]           [AP             [Deg               [extremely]             ]             [A$'$               [A                 [straightforward]               ]               [CP                 [to wield, roof]               ]             ]           ]         ]       ]     ]   ] \end{forest} \end{document}                  

Selected additional features

One common characteristic requested for linguists' trees seems to be to ensure that the terminal nodes are aligned.

woods allows you lot to specify that nodes should be aligned on a common tier. To use this feature, you just write , tier=<proper noun of tier> after the content of the node is specified.

Here'southward an extreme example:

extreme tier alignment

          \begin{woods}   for tree={     fit=band,% spaces the tree out a little to avoid collisions   }   [things     [cabbages, tier=vegetables       [peaches, tier=fruits]     ]     [kings, tier=aristocrats]     [sealing wax       [queens, tier=aristocrats         [carrots, tier=vegetables]         [pineapple, tier=fruits]         [aubergine, tier=vegetables]       ]     ]   ] \cease{forest}                  

In the case of our linguists' tree, we but want the concluding nodes aligned. We are already saying nosotros want them in italics, so we can but add a tier specification there.

We did have

                      for tree={     if north children=0{       font=\itshape     }{},   }                  

At present nosotros change this to the following:

                      for tree={     if n children=0{       font=\itshape,       tier=terminal,     }{},   }                  

and the tree changes quite dramatically:

aligned terminal nodes


Earlier wood: version 1


Here is a forest version of the code for the tree posted in Antal S-Z'due south answer.

          \documentclass[tikz,border=10pt]{standalone} \usepackage{forest} \begin{document} \brainstorm{forest}   [IP     [NP      [Det        [\textit{the}]      ]      [North$'$        [N          [\textit{package}]        ]      ]     ]     [I$'$       [I         [\textsc{3sg.Pres}         ]       ]       [VP         [V$'$           [V             [\textit{is}]           ]           [AP             [Deg               [\textit{extremely}]             ]             [A$'$               [A                 [\textit{straightforward}]               ]               [CP                 [\textit{to wield}, triangle]               ]             ]           ]         ]       ]     ]   ] \end{forest} \end{document}                  

The basic idea is that every node in the tree is enclosed in foursquare brackets. The node's subtree is included in the aforementioned brackets, if it has one.

Note that there are no dots for branch points and that there is no need to leave a space earlier a closing square bracket. In fact, the higher up code is equivalent to

          \brainstorm{forest}   [IP[NP[Det[\textit{the}]][N$'$[North[\textit{parcel}]]]][I$'$[I[\textsc{3sg.Pres}]][VP[V$'$[5[\textit{is}]][AP[Deg[\textit{extremely}]][A$'$[A[\textit{straightforward}]][CP[\textit{to wield}, triangle]]]]]]]] \end{forest}                  

Obviously, this is anything but piece of cake to read - hence my preference for lots of 'unnecessary' space.

forest cannot, however, abide a bare line and these must be avoided.

The above trees produce the post-obit output:

tree

How to turn a tree into the subclass specification forest uses

Start with the root and put it inside a forest surroundings and within square brackets:

          \begin{forest}   [IP% root % rest of tree will go hither   ] \end{wood}                  

The rest of the tree consists of 1 or more smaller trees. These are subtrees of the root node. In this example, we have 2 subtrees and then we start with the root of each subtree and add together each of them in its own pair of square brackets.

          \begin{woods}   [IP% root     [NP% root of subtree % rest of subtree will go here     ]     [I$'$% root of subtree % rest of subtree will become hither     ]   ] \end{forest}                  

Then you can work on each of the subtrees in turn and repeat the operation.

Special styles

To make the triangular roof, we just add , triangle to the appropriate node and wood applies its triangular root manner.

If you later want to brand more than complex trees, you tin besides apply TikZ styles in simply the same style. Adding , text=scarlet to a node will turn the text of the node red, for case.

                      [N$'$, text=cherry-red        [N          [\textit{package}]        ]      ]                  

red node

Branches for logicians and linguists

Even simple logicians' and linguists' trees probably need a picayune styling in order to look right. In item, trees typically take branches which starting time from a common point below the parent node and extend to a point above the child. Although this is non default, we can implement it by proverb

          parent anchor=south,% start branches beneath the parent kid anchor=north,% end branches in a higher place the child                  

To use this to the whole tree, we can say

          \begin{forest}   for tree={     parent ballast=south,     child anchor=north,   }   [IP     ...   ] \end{forest}                  

branches for logicians and linguists

Conditional styling saves typing

You can as well, if yous wish, save irresolute the font for every terminal node. If almost of the terminal nodes should be italicised, you can say

          if n children=0{% if the node doesn't have any children of its own   font=\itshape }{},                  

You lot can so override this for the single pocket-size-caps node, by saying , font=\scshape subsequently specifying the node's contents.

Consummate code for revised tree

          \documentclass[tikz,edge=10pt,multi]{standalone} \usepackage{forest} \begin{document} \begin{forest}   for tree={     parent anchor=south,     kid ballast=north,     if due north children=0{       font=\itshape     }{},   }   [IP     [NP      [Det        [the]      ]      [Northward$'$        [N          [package]        ]      ]     ]     [I$'$       [I         [3sg.Pres, font=\scshape]       ]       [VP         [V$'$           [V             [is]           ]           [AP             [Deg               [extremely]             ]             [A$'$               [A                 [straightforward]               ]               [CP                 [to wield, triangle]               ]             ]           ]         ]       ]     ]   ] \end{woods} \end{certificate}                  

Selected additional features

One common feature requested for linguists' trees seems to be to ensure that the concluding nodes are aligned.

wood allows y'all to specify that nodes should be aligned on a common tier. To use this feature, you simply write , tier=<name of tier> subsequently the content of the node is specified.

Here'south an extreme example:

extreme tier alignment

          \begin{wood}   for tree={     parent anchor=south,     child anchor=due north,     fit=band,% spaces the tree out a little to avert collisions   }   [things     [cabbages, tier=vegetables       [peaches, tier=fruits]     ]     [kings, tier=aristocrats]     [sealing wax       [queens, tier=aristocrats         [carrots, tier=vegetables]         [pineapple, tier=fruits]         [aubergine, tier=vegetables]       ]     ]   ] \end{forest}                  

In the example of our linguists' tree, nosotros just want the final nodes aligned. Nosotros are already proverb we want them in italics, then we tin can just add together a tier specification at that place.

We did accept

                      for tree={     parent anchor=south,     child ballast=north,     if n children=0{       font=\itshape     }{},   }                  

Now nosotros change this to the following:

                      for tree={     parent anchor=south,     child anchor=due north,     if n children=0{       font=\itshape,       tier=terminal,     }{},   }                  

and the tree changes quite dramatically:

aligned terminal nodes

Source: https://tex.stackexchange.com/questions/5447/how-can-i-draw-simple-trees-in-latex

Posted by: woodbeetch.blogspot.com

0 Response to "How To Draw Trees In Latex"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel