2

I want to draw several trees with tikZ (Version 2.10) which are very similar in the sense that there is a first tree and the others are obtained by ommiting some of the nodes of the first one. I wanted to avoid copy and paste in order to keep things editable. So a minimal example of how the result should look like is this:

  \begin{tikz}
    \node {eins}
    child{
      node{zwei}
      child{
        node{drei}
      }
    }
    ;
  \end{tikz}


  \begin{tikz}
    \node {eins}
    child{
      child{
        node{drei}
      }
    }
    ;
  \end{tikz}

I tried to use the following \newif \if construct:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \newif\ifShow
  \Showtrue
  \node {eins}
   child{
     \ifShow
       node{zwei}
      \fi
      child{
        node{drei}
      }
   }
   ;
\end{tikzpicture}
\end{document}

but it results in the error message "ERROR: Package pgf Error: No shape named is known." The problem seems to be with the child{node{drei}}. If I remove this part, the conditional drawing of node{zwei} works fine.

Do you have any suggestions how to conditionally draw tree nodes with TikZ?

1 Answers1

4

Following Peter's link, I tried a different if then else mechanism. The following code works correctly with tikz 2.10. I guess tikz 2.10 is fairly outdated but I add this solution for people like me who have no easy control on the version of their tikz.

\documentclass{article}

\usepackage{tikz}
\usepackage{etoolbox}

\newbool{DEBUG}
\booltrue{DEBUG}
%\boolfalse{DEBUG}

\begin{document}
  \begin{tikzpicture}
    \node {eins}
    child{
      \ifbool{DEBUG}{%IF Debug True
       node{zwei}
       }{%else nothing
       }
      child{
        node{drei}
      }
    }
    ;
  \end{tikzpicture}

\end{document}