5

I'm trying to add and remove nodes in a tikz-qtree through the use of \only in beamer. And it gives weird results.

If I don't use \only I see the figure that I want.

\documentclass{beamer}
\usepackage{tikz-qtree}

\begin{document}

\begin{frame}
  \begin{tikzpicture}
    \Tree [.root
        [.subtree1 ] [.subtree2 ]
    ]
  \end{tikzpicture}
\end{frame}

\end{document}

root with two child nodes

But if I add an \only the text gets messed up.

\documentclass{beamer}
\usepackage{tikz-qtree}

\begin{document}

\begin{frame}
  \begin{tikzpicture}
    \Tree [.root
        \only<2>{ [.subtree1 ] [.subtree2 ] }
    ]
  \end{tikzpicture} 
\end{frame}

\end{document}

root with two child nodes, but weird labels e.g. [.subtree1] instead of subtree1

Expected output

Equivalent to the output of the code below. Unfortunately, I can't include more than two images due to lack of reputation.

\documentclass{beamer}
\usepackage{tikz-qtree}

\begin{document}

    \begin{frame}
      \begin{tikzpicture}
        \Tree [.root ]
      \end{tikzpicture}
    \end{frame}

    \begin{frame}
      \begin{tikzpicture}
        \Tree [.root
            [.subtree1 ] [.subtree2 ]
        ]
      \end{tikzpicture}
    \end{frame}

\end{document}
Shankari
  • 153
  • 1
    Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See minimal working example (MWE). – Henri Menke Sep 08 '17 at 04:34
  • I thought I did include an MWE - there's the working example (above) and the non-working example (below). Both of them use a three node tree (root + 2 child nodes). I am unsure I can get the code to be any shorter. – Shankari Sep 08 '17 at 05:43
  • 1
    This is not a minimal working example (MWE), emphasis on working. A MWE is an example that I can copy out of your question, paste into my editor and run without having to write boilerplate code around it, like \documentclass and \usepackage. – Henri Menke Sep 08 '17 at 07:45
  • 1
    I have edited your question accordingly. Please adopt this pattern in the future. You should have a look at the page I linked for further details. As a matter of fact, if you post a well-prepared minimal working example (MWE) with your question, the answers will have substantially higher quality because people don't have to guess what you mean. Also you will get more upvotes, granting you site privileges. – Henri Menke Sep 08 '17 at 07:50
  • 1
    The {...} is grouping the subnodes in to one. What effect to you exactly need to achieve? Do you mind the spacing of the tree changing with each overlay? Do you want future edges visible? – Andrew Swann Sep 08 '17 at 08:35
  • I want to have two slides - the first with only the root, and the second with the root and two properly formatted children. I don't mind the spacing of the tree changing with each overlay. I don't want future edges to be visible. I have edited the question to clarify this. – Shankari Sep 08 '17 at 13:53

1 Answers1

4

Forest is easier, I think, if you have a lot of this to do, but tikz-qtree can work, too.

Here's code for both, assuming edges should appear with nodes. The authors of the styles used are noted in comments, with links for further details.

\documentclass{beamer}
\usepackage{tikz-qtree}
\usepackage[linguistics]{forest}
\makeatletter
\tikzset{% set up for transitions using tikz with beamer overlays - developed by Daniel (http://tex.stackexchange.com/a/55849/) and, in earlier form, by Matthew Leingang (http://tex.stackexchange.com/a/6155/) and modified for this use, I think by Qrrbrbirlbel (http://tex.stackexchange.com/a/112471/)
  invisible/.style={opacity=0,text opacity=0},
  visible on/.style={alt=#1{}{invisible}},
  alt/.code args={<#1>#2#3}{%
    \alt<#1>{\pgfkeysalso{#2}}{\pgfkeysalso{#3}} % \pgfkeysalso doesn't change the path
  },
}
\forestset{%
  visible on/.style={% developed by Qrrbrbirlbel (http://tex.stackexchange.com/a/112471/)
    for tree={%
      /tikz/visible on={#1},
      edge={/tikz/visible on={#1}}}},
}
\makeatother
\begin{document}

\begin{frame}
  \begin{tikzpicture}\tikzset{edge from parent/.append style={visible on=<2->}}
    \Tree [.root
      [.\node [visible on=<2->]{subtree1}; ] [.\node [visible on=<2->]{subtree2}; ]
    ]
  \end{tikzpicture}
  \begin{forest}
    where level=1{for tree={visible on=<2->}}{}
    [root
      [subtree1]
      [subtree2]
    ]
  \end{forest}

\end{frame}

\end{document}

two trees

cfr
  • 198,882