# Tikz tree in beamer

% Red-black tree
\documentclass[leqno,fleqn,table]{beamer}
\usepackage{tikz}
%%%<
%\usepackage{verbatim}
%\usepackage[active,tightpage]{preview}
%\PreviewEnvironment{tikzpicture}
%\setlength{\PreviewBorder}{10pt}%
%%%>
%\begin{comment}
%:Title: Red-black tree
%:Tags: Trees;Graphs
%:Slug: red-black-tree
%
%A red-black tree is a special type of binary tree, used in computer science
%to organize pieces of comparable data, such as text fragments or numbers.
%(Wikipedia)
%\end{comment}
\usetikzlibrary{arrows}

\tikzset{
treenode/.style = {align=center, inner sep=0pt, text centered,
font=\sffamily},
arn_n/.style = {treenode, circle, white, font=\sffamily\bfseries, draw=black,
fill=black, text width=1.5em},% arbre rouge noir, noeud noir
arn_r/.style = {treenode, circle, red, draw=red,
text width=1.5em, very thick},% arbre rouge noir, noeud rouge
arn_x/.style = {treenode, rectangle, draw=black,
minimum width=0.5em, minimum height=0.5em}% arbre rouge noir, nil
}

\begin{document}

\begin{frame}
\begin{minipage}[c]{\textwidth}
\begin{tikzpicture}[->,>=stealth',level/.style={sibling distance = 5cm/#1,
level distance = 1.5cm}]
\node [arn_n] {33}
child{ node [arn_r] {15}
child{ node [arn_n] {10}
child{ node [arn_r] {5} edge from parent node[above left]
{$x$}} %for a named pointer
child{ node [arn_x] {}}
}
child{ node [arn_n] {20}
child{ node [arn_r] {18}}
child{ node [arn_x] {}}
}
}
child{ node [arn_r] {47}
child{ node [arn_n] {38}
child{ node [arn_r] {36}}
child{ node [arn_r] {39}}
}
child{ node [arn_n] {51}
child{ node [arn_r] {49}}
child{ node [arn_x] {}}
}
}
;
\end{tikzpicture}
\end{minipage}
\end{frame}
\end{document}


So I'm using this in a beamer but get an error (illegal parameter number in definition of iterate) Though it works when i delete the frames and make the documentclass article. Is it possible to get it working in beamer?

• Have you tried passing the [fragile] option to the frame environment? – Daniel Jun 14 '16 at 11:22
• If I just put fragile it crashes. Though I had a font warning (\usepackage{helvet}): When I get rid of that warning additionally it works. Though why is it so sensitive to these fragile option set or not. Dont I change sth else too when turning fragile on ? It doesnt sound trustworthy to me... – Diger Jun 14 '16 at 11:31
• Diger, what exactly do you mean with "it crashes"? Moreover, I don't see a \usepackage{helvet} in your MWE so I cannot investigate that issue. Compiling your MWE using [fragile] with pdflatex works fine and yields a nice red-black tree. – Daniel Jun 14 '16 at 13:29
• Just tried it also with \usepackage{helvet} – no problem. – Daniel Jun 14 '16 at 13:31

Pass the [fragile] option to the 'frame' environment:

\begin{frame}[fragile]
...
% \end{frame} must be on a single line!
\end{frame}


In your case, this is necessary because of the use of a tex parameter (#1) inside the style definition of the tikzpicture. TeX'nically speaking: To provide for the parameter syntax in style definitions, TikZ needs to change the catcode of #, which in turn requires to use the fragile option for the frame enivornment.

Background

fragile appears to be a "magic" option to solve all kinds of strange error messages. Basically, it just has to be used whenever some part of the frame content needs to be read verbatim, that is, without immediately interpreting special characters (#1 in this case).

The details are explained in the beamer user guide in §8.1:

fragile[=singleslide] tells beamer that the frame contents is “fragile.” This means that the frame contains text that is not “interpreted as usual.” For example, this applies to verbatim text, which is, obviously, interpreted somewhat differently from normal text. If a frame contains fragile text, different internal mechanisms are used to typeset the frame to ensure that inside the frame the character codes can be reset. The price of switching to another internal mechanism is that either you cannot use overlays or an external file needs to be written and read back (which is not always desirable).

In detail, the following happens when this option is given for normal (pdf)LATEX: The contents of the frame is scanned and then written to a special le named ⟨jobname⟩.vrb or, if a label has been assigned to the frame, ⟨jobname⟩.⟨current frame number⟩.vrb. Then, the frame is started anew and the content of this file is read back. Since, upon reading of a file, the character codes can be modified, this allows you to use both verbatim text and overlays.

To determine the end of the frame, the following rule is used: The first occurence of a single line containing exactly \end{⟨frame environment name⟩} ends the frame.