I am trying to draw a graph (actually, generate TikZ code automatically) which has a main branch that must be straight; secondary branches must always branch on the left. On each layer there must be exactly one node, except for branches that have exactly one node (see node 17a
).
The order of the branches is not important, as long as the aforementioned conditions hold.
The MWE shows my problem: even manually, I cannot manage to achieve the desired result.
I have tried several combination of grow=down
, grow'=down
, branch left
either on branching nodes when declaring the branching edge, directly on the branching edge (i.e. on the --
) or in global graph
options, with no result (the MWE has been polished from those not-working ugly hacks). The only thing that changes the branch order is swapping lines 17 -- 18
and 17a -- 18
, but this results in a very ugly graph, so an answer that take into account this would be very appreciated (but I'll be fine even with a simpler one).
Background notes:
The graph is generated through a SAX parser, and only previous and next nodes (if any) and if the current node belong to the main branch or not is known. I know if a node is a branching point and/or joining point, so I could generate weight=0
on the branching edges (or anything else).
The graph is acyclic (all the edges actually point downwards, so arrows are superfluos and I just use --
, but as long as there is no arrow, I could print '->' if this makes TikZ happier).
MWE:
\documentclass[a5paper]{article}
\usepackage{tikz}
\usepackage[default]{sourcesanspro}
\usepackage[margin=1cm]{geometry}
\usetikzlibrary{graphdrawing,graphs,calc}
\usegdlibrary{layered}
\tikzset{linehead/.style={circle, fill=white, outer sep = 0pt, inner sep = 0pt, minimum size = 5pt, thick, draw=black}}
\tikzset{linestop/.style={circle, fill=black, outer sep = 0pt, inner sep = 0pt, minimum size = 4pt}}
\tikzset{variantgraphtxt/.style={near start, left, font=\scriptsize\bfseries}}
\begin{document}
\thispagestyle{empty}
\fontsize{8.25}{9}\selectfont
\begin{tikzpicture}
\graph[layered layout, level distance=\baselineskip, sibling distance=\baselineskip,
edges={line width=1pt, rounded corners=0.3\baselineskip}, nodes={linestop}, empty nodes, branch left]
{
% Main branch, must be vertical
1[linehead] -- 2,
2 -- 5,
5 -- 6,
6 -- 7,
7 -- 8,
8 -- 9,
9 -- 10,
10 -- 11,
11 --[minimum layers=12] 23,
23 -- 24,
24 -- 25,
25 -- 26,
26 -- 27,
27 -- 28,
28 -- 29,
29 -- 30,
30 -- 31,
31 -- 32,
32 -- 33,
33 -- 34,
34 -- 35,
35 -- 36,
36 -- 37,
37 -- 38,
38 -- 39,
39 -- 40,
40 -- 41,
41 -- 42,
42 -- 43,
43 -- 44,
44 -- 45[linehead],
% all the following branches must be on the left of the main one
2 --[weight=0, edge node={node[variantgraphtxt] {C, D}}] 3,
3 -- 4,
4 -- 5,
10 --[weight=0,minimum layers=2,edge node={node[variantgraphtxt] {B, D}}] 12,
12 -- 13,
13 -- 14,
14 -- 15,
15 -- 16,
16 -- 17,
17 -- 18,
17a[linehead,edge node={node[variantgraphtxt, at start] {B2}}] -- 18, % B2 not showing up
19 -- 20,
18 -- 19,
20 -- 21,
21 -- 22,
22 --[weight=0] 26,
};
\node[text=red, align=center] (text) at ($(12) + (2, \baselineskip)$) {This branch\\should be\\on the other side!};
\draw[red, ->] (text) -- ($(12) + (0.1, \baselineskip)$);
\node[text=red, align=center] (text2) at ($(17a) + (1.7, 0)$) {This branch\\should extend\\on the left as well\\ and show B2!};
\draw[red, ->] (text2) -- ($(17a) + (0.2, 0)$);
\end{tikzpicture}
\end{document}