6

I have troubles when running my old Latex code today (I guess because the corresponding package changed). The problem is that now Latex gives me many errors related to the node names. What should I change to make it work again?

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usetikzlibrary{trees}
\begin{document}
\begin{tikzpicture}[>=stealth,sloped]
 \tikzstyle{hn}=[circle,draw,inner sep=0.01]
 \tikzstyle{sn}=[circle,draw,inner sep=0.01,fill=black]
      \matrix (tree) [%
        matrix of nodes,
        minimum size=0.6cm,
        column sep=0.8cm,
        row sep=0.1cm,
      ]
      {
                   &              &              &              &              & \node[sn]{};  \\
                   &              &              &              & \node[sn]{}; &             \\
                   &              &              & \node[sn]{}; &              & \node[sn]{};  \\
                   &              & \node[sn]{}; &              & \node[sn]{}; &             \\
                   & \node[hn]{}; &              & \node[hn]{}; &              & \node[sn]{};  \\
      \node[hn]{}; &              & \node[hn]{}; &              & \node[hn]{}; &             \\
                   & \node[hn]{}; &              & \node[hn]{}; &              & \node[sn]{};  \\
                   &              & \node[hn]{}; &              & \node[hn]{}; &             \\
                   &              &              & \node[hn]{}; &              & \node[sn]{};  \\
                   &              &              &              & \node[sn]{}; &             \\
                   &              &              &              &              & \node[sn]{};  \\           
      };
      \draw[-] (tree-6-1)--(tree-5-2){};
      \draw[-] (tree-6-1)--(tree-7-2){};
      \draw[-] (tree-5-2)--(tree-4-3){};
      \draw[-] (tree-5-2)--(tree-6-3){};
      \draw[-] (tree-7-2)--(tree-6-3){};
      \draw[-] (tree-7-2)--(tree-8-3){};
      \draw[-] (tree-4-3)--(tree-3-4){};
      \draw[-] (tree-4-3)--(tree-5-4){};
      \draw[-] (tree-6-3)--(tree-5-4){};
      \draw[-] (tree-6-3)--(tree-7-4){};
      \draw[-] (tree-8-3)--(tree-7-4){};
      \draw[-] (tree-8-3)--(tree-9-4){};
      \draw[-] (tree-3-4)--(tree-2-5){};
      \draw[-] (tree-3-4)--(tree-4-5){};
      \draw[-] (tree-5-4)--(tree-4-5){};
      \draw[-] (tree-5-4)--(tree-6-5){};
      \draw[-] (tree-7-4)--(tree-6-5){};
      \draw[-] (tree-7-4)--(tree-8-5){};
      \draw[-] (tree-9-4)--(tree-8-5){};
      \draw[-] (tree-9-4)--(tree-10-5){};
      \draw[-] (tree-2-5)--(tree-1-6){};
      \draw[-] (tree-2-5)--(tree-3-6){};
      \draw[-] (tree-4-5)--(tree-3-6){};
      \draw[-] (tree-4-5)--(tree-5-6){};
      \draw[-] (tree-6-5)--(tree-5-6){};
      \draw[-] (tree-6-5)--(tree-7-6){};
      \draw[-] (tree-8-5)--(tree-7-6){};
      \draw[-] (tree-8-5)--(tree-9-6){};
      \draw[-] (tree-10-5)--(tree-9-6){};
      \draw[-] (tree-10-5)--(tree-11-6){};
      \end{tikzpicture}

Figure 1.    

\end{document}
  • And when the disciples saw it, they marvelled, saying, "How did the binomial tree immediately wither away?" ([near] Matthew 21:20) ;-) – Przemysław Scherwentke Sep 19 '16 at 13:49
  • Don't mix matrix of nodes and \node{...}! – Paul Gaborit Sep 19 '16 at 13:59
  • Is there any particular reason you don't want to draw the tree as a tree? Why make it a matrix? I only ask because, if you don't have some reason to make life difficult for yourself, I'd recommend the easier route ;). – cfr Sep 20 '16 at 15:42

2 Answers2

7

You just have change all

\node[hn]{};

to

|[hn]|

and similar for sn nodes. When \node is used at the start of the cell, it is not named automatically, but you can add options to a cell using |[...]| instead.

That said, there are probably much better, more convenient ways of drawing a tree like that. I'm no tree-expert though, so I'll leave that to others.

A couple of other comments:

  • \draw[-] (tree-6-1)--(tree-5-2){};: Both [-] and the {} at the end are not needed.
  • It is generally recommended to use \tikzset{stylename/.style={...}} instead of tikzstyle. For this case though, where you're defining the styles inside the tikzpicture, I'd just do

    \begin{tikzpicture}[>=stealth,sloped,
       hn/.style={circle,draw,inner sep=0.01},
       sn/.style={circle,draw,inner sep=0.01,fill=black}]
    
Torbjørn T.
  • 206,688
4

Here's is one of the more convenient ways of drawing the graph mentioned by Torbjørn T.. The diagram is not technically a tree. Hence, my attempts to typeset it as a tree met with only partial success. However, using the modular Sugiyama layout algorithm, as implemented by TikZ, produced satisfactory results.

Because this uses automated algorithms to layout the tree, it requires LuaTeX. If you get errors because you do not have luatex85.sty, simply remove the line \RequirePackage{luatex85} as this is only required for quite recent versions of the engine. You probably don't need to load the package anyway if you use a standard class rather than standalone.

\RequirePackage{luatex85}
\documentclass[multi,tikz,border=10pt]{standalone}
\usetikzlibrary{graphs,graphdrawing}
\usegdlibrary{layered}
\begin{document}
\begin{tikzpicture}
  \graph [layered layout, grow=0, sibling distance=10mm, nodes={circle, draw, as= }]
  {
    0  -- { 11 -- { 21 -- { 31 -- { 41[fill] -- { 51[fill], 52[fill] }, 42 -- { 52, 53[fill] } } , 32 -- { 42, 43 -- { 53, 54[fill] } } } , 22  -- { 32 , 33 -- { 43, 44[fill] -- { 54, 55[fill] } } } } , 12 -- { 22 , 23[fill] -- { 33, 34[fill] -- { 44, 45[fill] -- { 55, 56[fill] } } } } }
  }
  ;
\end{tikzpicture}
\end{document}

layered graph

cfr
  • 198,882