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?

  • It should work. Can you paste the full code? Also use tikzpicture environment instead. – percusse Oct 16 '14 at 8:28
  • I agree that it should -- But it DOES NOT WORK and I want to understand why and how to resolve the problem. May there be any dependence on the tikz-version I use. I am not sure which version it is but it is surely not the newest one. (How do I figure out my tikz version?) Anyway, I now created a minimal example that should work but it does not. – user136015 Oct 16 '14 at 20:05
  • I copied and pasted to my editor and it runs without any problems. If you put \pgfversion somewhere outside the tikzpicture environment it shows the version of TikZ/PGF – percusse Oct 16 '14 at 20:10
  • Ok, so my TIKZ/PGF-Version is 2.10; @percusse which version do you have? – user136015 Oct 16 '14 at 20:28
  • 1
    Related Question: Conditional typesetting / build. – Peter Grill Oct 16 '14 at 20:34
3

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}

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.