3

How can I position a tikz tree based on an interior node, i.e. instead of defining the position of the root, defining the position of an interior node?

For example, consider the following code.

\documentclass[border=10pt,multi,tikz]{standalone}
\begin{document}
\begin{tikzpicture}
  \node {a}
    child {node at (1,0) {b}
      child {node {c}}
  };

  \node {x}
    child {node at (3,0) {y}
      child {node {z}}
  };
\end{tikzpicture}
\end{document}

This code does not produce the desired output, but it shows what I imagine.

Desired would be two trees where b and y are at (1,0) and (3,0), respectively, and the rest of the trees is normal around them, e.g., a is above b and c is below b respecting the level distance, etc.

Especially if a and c become larger trees, aligning the two trees at the inner node becomes tedious shifting work.

1
  • 1
    Please make your code into a compilable example.
    – cfr
    Jan 11, 2017 at 23:24

1 Answer 1

2

In order to prevent the nodes being placed as they are defined, you can use TikZ's graph drawing facilities with the trees layout library.

For example,

\IfFileExists{luatex85.sty}{\RequirePackage{luatex85}}{}
\documentclass[border=10pt,multi,tikz]{standalone}
\usetikzlibrary{graphs,graphdrawing}
\usegdlibrary{trees}
\begin{document}
\begin{tikzpicture}
  \graph [tree layout]
  {
    a -- { b[at={(1,0)}] , c }
  };
  \graph [tree layout]
  {
    x -- { y[at={(3,0)}] , z }
  };
\end{tikzpicture}
\end{document}

Compiling with LuaTeX gives the following result.

trees around nodes

You can stick to the more verbose syntax if you prefer and still use the algorithmic graph-drawing facilities. This is just more concise.

2
  • Too sad that there seems to be no solution for plain pdflatex. However, this works nicely in lualatex, so thank you!
    – Markus
    Feb 1, 2017 at 8:02
  • You could set the baseline to a particular node with Forest or, for a tikzpicture in the environment options. But that is for the whole picture. Probably Forest could do it with pdflatex. Well, definitely, but you need to fake a root in that case.
    – cfr
    Feb 2, 2017 at 2:20

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.