3

Here is the code:

\documentclass[tikz,border=10pt]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
for tree={
   draw,
   align=center
}
[TowersOfHanoi\left 4, 1, 3, 2\right
[TowersOfHanoi\left 3, 1, 2, 3\right
  [TowersOfHanoi\left 2, 1, 3, 2\right
    [TowersOfHanoi\left 1, 1, 2, 3\right]
    [TowersOfHanoi\left 1, 2, 3, 1\right]
  ]
  [TowersOfHanoi\left 2, 3, 2, 1\right
    [TowersOfHanoi\left 1, 3, 1, 2\right]
    [TowersOfHanoi\left 1, 1, 2, 3\right]
  ]
]
[TowersOfHanoi\left 3, 2, 3, 1\right
  [TowersOfHanoi\left 2, 2, 1, 3\right
    [TowersOfHanoi\left 1, 2, 3, 1\right]
    [TowersOfHanoi\left 1 ,3, 1, 2\right]
  ]
  [TowersOfHanoi\left 2, 1, 3, 2\right
    [TowersOfHanoi\left 1, 1, 2, 3\right]
    [TowersOfHanoi\left 1, 2, 3, 1\right]
  ]
 ]
]
\end{forest}
\end{document}

Here is the error message:

! Missing \endcsname inserted.
to be read again
\right
l.32 \end{forest}

?

What am I doing wrong?


Note: The above code is a slightly modified version of this benchmark code which reproduces the drawing.

Ziezi
  • 455

1 Answers1

3

First of all, the commas. A comma means that the text of the node is ended, and options pertaining to it follows. To have commas in the node text, wrap it in braces.

Second, \left/\right. They require math mode, and they also require a delimiter to follow them, which you've forgotten. I'd remove them altogether though, and insert just (/) instead. \left .. \right isn't needed when you only have numbers between them like that.

As noted in a comment below, align=center doesn't really do much useful here, so unless you're planning to, for example, add linebreaks, it can be removed.

\documentclass[tikz,border=10pt]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
for tree={
   draw
}
[{TowersOfHanoi(4, 1, 3, 2)}
[{TowersOfHanoi(3, 1, 2, 3)}
  [{TowersOfHanoi(2, 1, 3, 2)}
    [{TowersOfHanoi(1, 1, 2, 3)}]
    [{TowersOfHanoi(1, 2, 3, 1)}]
  ]
  [{TowersOfHanoi(2, 3, 2, 1)}
    [{TowersOfHanoi(1, 3, 1, 2)}]
    [{TowersOfHanoi(1, 1, 2, 3)}]
  ]
]
[{TowersOfHanoi(3, 2, 3, 1)}
  [{TowersOfHanoi(2, 2, 1, 3)}
    [{TowersOfHanoi(1, 2, 3, 1)}]
    [{TowersOfHanoi(1 ,3, 1, 2)}]
  ]
  [{TowersOfHanoi(2, 1, 2, 2)}
    [{TowersOfHanoi(1, 1, 2, 3)}]
    [{TowersOfHanoi(1, 2, 3, 1)}]
  ]
 ]
]
\end{forest}
\end{document}

That said, your tree becomes very wide when you write TowersOfHanoi in every node. Perhaps you could consider abbreviating it to ToH, or leaving it out altogether.

\documentclass[tikz,border=10pt]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
for tree={
   draw,
}
[{ToH(4, 1, 3, 2)},label={above:ToH = TowersOfHanoi}
[{ToH(3, 1, 2, 3)}
  [{ToH(2, 1, 3, 2)}
    [{ToH(1, 1, 2, 3)}]
    [{ToH(1, 2, 3, 1)}]
  ]
  [{ToH(2, 3, 2, 1)}
    [{ToH(1, 3, 1, 2)}]
    [{ToH(1, 1, 2, 3)}]
  ]
]
[{ToH(3, 2, 3,1)}
  [{ToH(2, 2, 1, 3)}
    [{ToH(1, 2, 3, 1)}]
    [{ToH(1, 3, 1, 2)}]
  ]
  [{ToH(2, 1, 2, 2)}
    [{ToH(1, 1, 2, 3)}]
    [{ToH(1, 2, 3, 1)}]
  ]
 ]
]
\end{forest}

\begin{forest}
for tree={
   draw,
}
[{(4, 1, 3, 2)},label={above:Arguments to TowersOfHanoi}
[{(3, 1, 2, 3)}
  [{(2, 1, 3, 2)}
    [{(1, 1, 2, 3)}]
    [{(1, 2, 3, 1)}]
  ]
  [{(2, 3, 2, 1)}
    [{(1, 3, 1, 2)}]
    [{(1, 1, 2, 3)}]
  ]
]
[{(3, 2, 3,1)}
  [{(2, 2, 1, 3)}
    [{(1, 2, 3, 1)}]
    [{(1, 3, 1, 2)}]
  ]
  [{(2, 1, 2, 2)}
    [{(1, 1, 2, 3)}]
    [{(1, 2, 3, 1)}]
  ]
 ]
]
\end{forest}
\end{document}
Torbjørn T.
  • 206,688