I would like to easily create tree-like structures like
D─┬───────────┬───A
R─┬───A─┬─A R─┐
R─┐ R R
R
D is the top root node. It starts a horizontal line to the right, which stands for the "same level". This horizontal line can have more letters A
threaded at any position, which do not have additional semantic meaning. These letters A
always have a line incoming from the left.
The more interesting feature is that a horizontal line can split downwards to a child (base) node R
which always have a line incoming from the top. This split can happen at multiple positions on the horizontal baseline. The child nodes recursively start a new virtual block and hence are usually continued by an outgoing horizontal line to the right.- Hence, the R
next to the D
effectively starts the whole thing again in its own "bounding box" (here marked with +
).
D─┬───────────────┬───A
+++++++++++ R─┐
+R─┬───A─┬─A+ R
+ R─┐ R +
+ R +
+++++++++++
If for a node there are no more nodes connected to the right, then the horizontal outgoing line is not shown. And of course tree/text overlap needs to be avoided, i.e. some horizontal lines need to stretch far enough before starting a new child node. A type A
node also should only appear after all previous child blocks have completed to the left of them.
What would be the best way to create such a tree-like drawing in LaTeX?
I've found \usetikzlibrary{trees}
which goes in this direction, but I'm not sure how to flip the example, how to make multiple letters on a horizontal line, how to control text overlap, etc…
Any good suggestion how to recreate that diagram?
There is a working answer with manually positioning everything, but I hope there is another solution like tikz-tree which can calculate the positioning automatically for me, so that I only specify the child-node relations.
With arbitrary and longer labels:
D─┬───────────────────────G─┬─────C
Cc─┬─────────Aa─┬──Aaaa Eee─┐
Bbb─┐ Dd A
Aaaaa
Best Answer
Variant with parameter for label
Adaptations
\nodeD
,\nodeR
and\nodeA
each have now one parameter to set the text.base node/.style={draw, dashed}
just for clarificationResult
Code
Variant without parameters (original)
Description
I defined some commands that can be used within tikz:
\nodeD
,\nodeR
and\nodeA
for the different node types\branchend
to stop a branch and go up one level.I used counters
treeRnode
andtreeAnode
to name the nodes and variables\treeX
and\treeY
to save the current position. Futhermore,\treeLastY<y>
saves the last node in layer<y>
.So the code for the given tree is in my syntax:
The indentation in the source code is just for readability. So, this would be the same:
Result
Code