4

I have created following tree/graph:

%! TEX program = lualatex
\documentclass[]{scrartcl}
\usepackage{tikz}

\usetikzlibrary{
  graphs,
  graphdrawing,
  quotes,
}
\usegdlibrary{trees}

\begin{document}

{\footnotesize
\begin{tikzpicture}[
  every node/.style={align=center},
]

\graph[
    tree layout,
    minimum number of children=3,
    missing nodes get space=false,
    edge quotes={anchor=center, align=center},
    edges={nodes={fill=white}},
    sibling distance=28mm,
    level distance=28mm,
  ]{

  % % level 1:
  %   a/"$P4/m2/m/2/m$";
  % % level 2:
  %   b/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}";
  % % level 3:
  %   c/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}";
  %   d/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}";
  % % level 4:
  %   e/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}";
  %   f/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}";
  %   g/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}";
  % % level 5:
  %   h/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}";
  %   i/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}";
  % % level 6:
  %   j/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}";
  %   k/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}";
  %   l/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}";
  % % level 7:
  %   m/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}";

  a ->
  b[second] ->  {  c[first] -> {e[first], , }, d[second]->{f[first] -> h[second] -> {, j[second],k[third] -> m}, ,g[third] -> i[second] -> l[second] -> m}, };
};

\end{tikzpicture}
}
\end{document} 

The result looks just like I want it:

enter image description here

However, if I uncomment the code block, the second children are no longer at centered positions:

enter image description here

How can I force tikz graphdrawing to stick to the same positioning as for the single letter node labels.

Related question seem to be Centering children of tikz tree layout

The only answer recommends to switch to the forest package. However, I would be happy if I could stay with the tikz graphdrawing library. I am also not sure whether the forrest package actually allows to draw a graph not only trees.

4

This solution uses numerous post-anchoring adjustments to position nodes in their desired positions on the canvas. This is done using the regardless at key (see p380 of TikZ manual). The result is that rather than nodes being placed at locations determined by the graph drawing algorithm, the nodes are placed at the nominated coordinates.

In an instance like this, where the location of the nodes is known, and they have a symmetrical layout, my preference would be to use either a matrix of nodes, or the features of the positioning library to place nodes appropriately, without the need to invoke any graph drawing algorithm. Nevertheless, the question asks for a solution using graph drawing syntax, so maybe this will be useful.

To make it easier to follow the code and the graph it generates, the nodes are identified with a name (a through m), together with the labels given by the OP. This is done with a command \lb. The background grid is from How can I show coordinates by grid in TikZ automatically?

This is the result: enter image description here

This is the code:

%! TEX program = lualatex
\documentclass[tikz,border=3pt]{standalone}

\usetikzlibrary{
  graphs,
  graphdrawing,
}
\usegdlibrary{
layered}

\pagestyle{empty}

% https://tex.stackexchange.com/a/39698
\makeatletter
\def\grd@save@target#1{%
    \def\grd@target{#1}}
\def\grd@save@start#1{%
    \def\grd@start{#1}}
\tikzset{
    grid with coordinates/.style={
        to path={%
            \pgfextra{%
                \edef\grd@@target{(\tikztotarget)}%
                \tikz@scan@one@point\grd@save@target\grd@@target\relax
                \edef\grd@@start{(\tikztostart)}%
                \tikz@scan@one@point\grd@save@start\grd@@start\relax
                \draw[minor help lines] (\tikztostart) grid (\tikztotarget);
                \draw[major help lines] (\tikztostart) grid (\tikztotarget);
                \grd@start
                \pgfmathsetmacro{\grd@xa}{\the\pgf@x/1cm}
                \pgfmathsetmacro{\grd@ya}{\the\pgf@y/1cm}
                \grd@target
                \pgfmathsetmacro{\grd@xb}{\the\pgf@x/1cm}
                \pgfmathsetmacro{\grd@yb}{\the\pgf@y/1cm}
                \pgfmathsetmacro{\grd@xc}{\grd@xa + \pgfkeysvalueof{/tikz/grid with coordinates/major step}}
                \pgfmathsetmacro{\grd@yc}{\grd@ya + \pgfkeysvalueof{/tikz/grid with coordinates/major step}}
                \foreach \x in {\grd@xa,\grd@xc,...,\grd@xb}
                \node[anchor=north,color=blue] at ([yshift=-0.15cm]\x,\grd@ya) {\pgfmathprintnumber{\x}};
                \foreach \y in {\grd@ya,\grd@yc,...,\grd@yb}
                \node[anchor=east,color=blue] at ([xshift=-0.15cm]\grd@xa,\y) {\pgfmathprintnumber{\y}};
            }
        }
    },
    minor help lines/.style={
        help lines,
        step=\pgfkeysvalueof{/tikz/grid with coordinates/minor step},
        minor line width/.initial=0.25pt,
    },
    major help lines/.style={
        help lines,
        line width=\pgfkeysvalueof{/tikz/grid with coordinates/major line width},
        step=\pgfkeysvalueof{/tikz/grid with coordinates/major step},
        line cap=round,
        color=lightgray,
    },
    grid with coordinates/.cd,
    minor step/.initial=1, % just use major
    major step/.initial=1,
    major line width/.initial=0.5pt,
}
\makeatother
\newcommand{\lb}[3]{{\begin{tabular}{c}#1\\#2\\\fbox{#3}\end{tabular}}}

\begin{document}

\begin{tikzpicture}[
font=\normalsize\bfseries
]

\draw (-14,-20) to[grid with coordinates] (8,1);

\graph[
    layered layout,
    sibling distance=50mm,
    level distance=30mm,
  ]{
      a/\lb{a}{$P4/m2/m/2/m$}{} -> b/\lb{b}{$P4/n2_1/m2/m$}{$HT-WO_3$} -> d/\lb{d}{$P4/n2_1/m2/m$}{$HT-WO_3$}[regardless at={(0,-6)}],
%%
      d -> {f/\lb{f}{$P4/n2_1/m2/m$}{$HT-WO_3$}[regardless at={(-6,-9)}],g/\lb{g}{$P4/n2_1/m2/m$}{$HT-WO_3$}[regardless at={(6,-9)}]},
%%
      f -> h/\lb{h}{$P4/n2_1/m2/m$}{$HT-WO_3$}[regardless at={(-6,-12)}] -> j/\lb{j}{$P4/n2_1/m2/m$}{$HT-WO_3$}[regardless at={(-6,-15)}],
      g -> i/\lb{i}{$P4/n2_1/m2/m$}{$HT-WO_3$}[regardless at={(6,-12)}] -> l/\lb{l}{$P4/n2_1/m2/m$}{$HT-WO_3$}[regardless at={(6,-15)}] -> m/\lb{m}{$P4/n2_1/m2/m$}{$HT-WO_3$}[regardless at={(6,-18)}],
      h -> k/\lb{k}{$P4/n2_1/m2/m$}{$HT-WO_3$}[regardless at={(0,-15)}],
      k -> m,
%%
      b -> c/\lb{c}{$P4/n2_1/m2/m$}{$HT-WO_3$}[regardless at={(-6,-6)}] -> e/\lb{e}{$P4/n2_1/m2/m$}{$HT-WO_3$}[regardless at={(-12,-9)}]
  };

\end{tikzpicture}

\end{document} 
  • Thanks for your answer. I had a similar idea: print out the coordinates of the first run and then simply use them for the full version. I would not mind if you could show me as well how to do the first picture with a matrix of nodes. I guess also tikzcd could be another approach which has been suggested for a different question of me. However, I should point you to my original question. Please have a look at the third picture. As you can see it can be become more complicated. – Hotschke Jun 5 '18 at 3:36
1

Use calculated coordinates from first run

%! TEX program = lualatex
\documentclass[]{scrartcl}
\usepackage{tikz}

\usetikzlibrary{
  calc,
  graphs,
  graphdrawing,
  quotes,
}
\usegdlibrary{trees}

\makeatletter
\def\extractcoord#1#2#3{
  \path let \p1=(#3) in \pgfextra{
    \pgfmathsetmacro#1{\x{1}/\pgf@xx}
    \pgfmathsetmacro#2{\y{1}/\pgf@yy}
    \xdef#1{#1} \xdef#2{#2}
  };
}
\makeatother

\begin{document}

{\footnotesize
\begin{tikzpicture}[
  every node/.style={align=center},
]

\graph[
    tree layout,
    minimum number of children=3,
    missing nodes get space=false,
    edge quotes={anchor=center, align=center},
    edges={nodes={fill=white}},
    sibling distance=28mm,
    level distance=28mm,
  ]{

    % % copy `nail at` from log file
    % a/"$P4/m2/m/2/m$"                    [nail at={(TO BE DETERMINED)}];
    % b/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}" [nail at={(TO BE DETERMINED)}];
    % c/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}" [nail at={(TO BE DETERMINED)}];
    % d/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}" [nail at={(TO BE DETERMINED)}];
    % e/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}" [nail at={(TO BE DETERMINED)}];
    % f/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}" [nail at={(TO BE DETERMINED)}];
    % g/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}" [nail at={(TO BE DETERMINED)}];
    % h/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}" [nail at={(TO BE DETERMINED)}];
    % i/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}" [nail at={(TO BE DETERMINED)}];
    % j/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}" [nail at={(TO BE DETERMINED)}];
    % k/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}" [nail at={(TO BE DETERMINED)}];
    % l/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}" [nail at={(TO BE DETERMINED)}];
    % m/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}" [nail at={(TO BE DETERMINED)}];

  a ->
    b[second] ->  {
    c[first] -> { e[first], , },
    d[second]->{
      f[first] -> h[second] -> {,
        j[second],
        k[third] -> m
      }, ,
      g[third] -> i[second] -> l[second] -> m
    },
  };
};

\extractcoord\xa\ya{a}\typeout{[nail at={(\xa,\ya)}];}
\extractcoord\xb\yb{b}\typeout{[nail at={(\xb,\yb)}];}
\extractcoord\xc\yc{c}\typeout{[nail at={(\xc,\yc)}];}
\extractcoord\xd\yd{d}\typeout{[nail at={(\xd,\yd)}];}
\extractcoord\xe\ye{e}\typeout{[nail at={(\xe,\ye)}];}
\extractcoord\xf\yf{f}\typeout{[nail at={(\xf,\yf)}];}
\extractcoord\xg\yg{g}\typeout{[nail at={(\xg,\yg)}];}
\extractcoord\xh\yh{h}\typeout{[nail at={(\xh,\yh)}];}
\extractcoord\xi\yi{i}\typeout{[nail at={(\xi,\yi)}];}
\extractcoord\xj\yj{j}\typeout{[nail at={(\xj,\yj)}];}
\extractcoord\xk\yk{k}\typeout{[nail at={(\xk,\yk)}];}
\extractcoord\xl\yl{l}\typeout{[nail at={(\xl,\yl)}];}
\extractcoord\xm\ym{m}\typeout{[nail at={(\xm,\ym)}];}

\end{tikzpicture}
}

\end{document} 

Now you can find in the log file following code snippet

[nail at={(0.0,0.0)}];
[nail at={(0.0,-2.8)}];
[nail at={(-2.8,-5.6)}];
[nail at={(0.0,-5.6)}];
[nail at={(-5.6,-8.40001)}];
[nail at={(-2.8,-8.40001)}];
[nail at={(2.8,-8.40001)}];
[nail at={(-2.8,-11.2)}];
[nail at={(2.8,-11.2)}];
[nail at={(-2.8,-14.0)}];
[nail at={(0.0,-14.0)}];
[nail at={(2.8,-14.0)}];
[nail at={(2.8,-16.8)}];

You can insert this into the main document and uncomment the full vertex labels.

enter image description here

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.