1

I'm trying to use the graph library of tikz to position my nodes (using above=of mynode is not always really practical when you have big chain of nodes that you want to link... Or maybe you have a better solution?). The idea is that:

  • I would like to be able to bound the size of some nodes, but some node may be bigger that the bounded nodes
  • I would like to have a tree going down, where the branches join in the middle of the two "columns". Ideally, the branches would be far enough so that they don't touch each other, even when some nodes are quite big.

I tried the following code, but first, I can't find how to split the branches "in the middle", but also I can't find how to change the width of the nodes (I can't use text width, it fails with TeX capacity exceeded...)

Thank you!

MWE:

\documentclass{report}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{graphs}

\begin{document}

\begin{tikzpicture}[
  mybox/.style={
    draw,
    rounded corners,
    fill=blue!20,
  },
  mysmallbox/.style={
    mybox,
    % Fail
    % text width=.5cm
  }
  ]
  \tikz \graph[trie, simple, grow down, branch right] {
    {
      a[mybox] -> b,
      c -> My small split node[mysmallbox] -> My big node[mybox]
    }
    -> x2;
  };

\end{tikzpicture}

\end{document}

Output: enter image description here

What I want: "My small split node" should have a maximum width (say 1cm) forcing it to split in two lines, the two "columns" a -> b and c -> ... -> My big node should not overlap, and x2 should be in the middle of these two columns (branch right put it on the left, branch left on the right... but branch middle does not exist!)

4

I take it you want something like this

target output

Since this is a tree proper, if read bottom to top, I'd use Forest as this will automatically space things out for you.

\documentclass[border=10pt]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
  my box/.style={top color=blue!20, draw, rounded corners, bottom color=blue!30},
  for tree={
    grow'=90,
    edge+={<-},
  },
  where n children=0{tier=terminus}{}
  [x2
    [b[a, my box]]
    [my big node, my box
      [my small split node, text width=10mm, text centered, my box [c]]
    ]
  ]
\end{forest}
\end{document}

If you want to stick with the graphs stuff from TikZ, then you can use align=center and split the node manually. However, I think you need a separate name in this case as you can't have line breaks in node names. Moreover, as the manual says on p. 273

You can even “reiterate” over a path in conjunction with the simple option. However, in this case, the default placement strategies will not work and you will need options like layered layout from the graph drawing libraries, which need LuaTeX.

you can't, I think, expect this to work without LuaTeX and a suitable layout algorithm. If you're OK with that, however, then the approach works fine.

\documentclass[border=10pt]{article}
\usepackage{tikz}
\usetikzlibrary{graphs,graphdrawing}
\usegdlibrary{layered}
\begin{document}
\begin{tikzpicture}[
  mybox/.style={
    draw,
    rounded corners,
    fill=blue!20,
  },
  mysmallbox/.style={
    mybox, align=center,
  }
  ]
  \tikz \graph[trie, simple, layered layout] {
    {
      a[mybox] -> b,
      c -> s/"My small\\split node"[mysmallbox] -> My big node[mybox]
    }
    -> x2;
  };
\end{tikzpicture}
\end{document}

manually split node

  • Thank you very much! I'm curious, do you know why it's not possible to manually add a text width property to graph nodes without breaking everything? – tobiasBora Feb 26 '18 at 9:06
  • (it would be useful to have nodes of equal length, and forest may not be suited because the structure I need finally may not be presentable using a down-top tree. – tobiasBora Feb 26 '18 at 9:35
  • No, sorry, I don't know. I am not all that familiar with the graphs stuff. And if it might not be a tree, Forest isn't probably the best option. However, I wasn't sure if LuaTeX was an option, so figured the Forest version might be useful in that case. You can make the nodes equal length. I thought you explicitly didn't want that. In Forest, you can just say for tree={text width=<whatever>} and they'll all have that width. – cfr Feb 26 '18 at 22:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.