1

I'm a new/beginner on tikz-qtree package, and trying to draw the straight line from F under R to T (level 2). Another problem, it not easy to use command \only{...}, how to use \only{...} in the better way.

\documentclass{beamer}
\usepackage{tikz-qtree}
\begin{document}
\begin{frame}
\begin{tikzpicture}[level distance=1.5cm, sibling distance=0.5cm]
\tikzset{grow'=up,frontier/.style={distance from root=150pt}}
\only<1>{
\Tree [.\mbox{ } \edge[draw=none]; 
[.\mbox{ } \edge[draw=none]; [.\mbox{ } \edge[draw=none]; [.$T$    
\edge[draw=none]; $(P$ ] \edge[draw=none]; [.\mbox{ } \edge[draw=none];  
$\wedge$ ] \edge[draw=none]; [.$T$ \edge[draw=none]; $Q)$ ] ]  
\edge[draw=none]; $\vee$ \edge[draw=none]; [ \edge[draw=none]; [.$F$  
\edge[draw=none]; $R$ ] ] ]
\edge[draw=none]; [.\mbox{ } \edge[draw=none]; $\leftrightarrow$ ] 
\edge[draw=none]; [.\mbox{ } \edge[draw=none]; [.\mbox{ }  
\edge[draw=none]; [.$\sim F$ \edge[draw=none]; $(\sim R$ ] ] 
\edge[draw=none]; [.\mbox{ } \edge[draw=none]; [.\mbox{ } 
\edge[draw=none]; $\wedge$ ] ] \edge[draw=none]; [.\mbox{ } 
\edge[draw=none]; [.$\sim T$ \edge[draw=none]; $\sim Q)$ ] ] ]
]
}
\only<2>{
\Tree [.\mbox{ } \edge[draw=none]; 
[.\mbox{ } \edge[draw=none]; [.$T$ [.$T$ \edge[draw=none]; $(P$ ]   
\edge[draw=none]; [.\mbox{ } \edge[draw=none]; $\wedge$ ] [.$T$ 
\edge[draw=none]; $Q)$ ] ] \edge[draw=none]; $\vee$ \edge[draw=none]; [ 
\edge[draw=none]; [.$F$ \edge[draw=none]; $R$ ] ] ]
\edge[draw=none]; [.\mbox{ } \edge[draw=none]; $\leftrightarrow$ ] 
\edge[draw=none]; [.\mbox{ } \edge[draw=none]; [.$T$ [.$\sim F$   
\edge[draw=none]; $(\sim R$ ] ] \edge[draw=none]; [.\mbox{ } 
\edge[draw=none]; [.\mbox{ } \edge[draw=none]; $\wedge$ ] ] 
\edge[draw=none]; [.$F$ [.$\sim T$ \edge[draw=none]; $\sim Q)$ ] ] ]
]
}
\only<3>{
\Tree [.\mbox{ } \edge[draw=none]; 
[.$T$ [.$T$ [.$T$ \edge[draw=none]; $(P$ ] \edge[draw=none]; [.\mbox{ }
\edge[draw=none]; $\wedge$ ] [.$T$ \edge[draw=none]; $Q)$ ] ]  
\edge[draw=none]; $\vee$  [ [.$F$ \edge[draw=none]; $R$ ] ] ]
\edge[draw=none]; [.\mbox{ } \edge[draw=none]; $\leftrightarrow$ ] 
\edge[draw=none]; [.$F$ [.$T$ [.$\sim F$ \edge[draw=none]; $(\sim R$ ] ]    
\edge[draw=none]; [.\mbox{ } \edge[draw=none]; [.\mbox{ } 
\edge[draw=none]; $\wedge$ ] ] [.$F$ [.$\sim T$ \edge[draw=none]; $\sim Q)$ ] ] ]
]
}
\only<4>{
\Tree [.\alert{$F$}
[.$T$ [.$T$ [.$T$ \edge[draw=none]; $(P$ ] \edge[draw=none]; [.\mbox{ }  
\edge[draw=none]; $\wedge$ ] [.$T$ \edge[draw=none]; $Q)$ ] ]  
\edge[draw=none]; $\vee$  [ [.$F$ \edge[draw=none]; $R$ ] ] ]
\edge[draw=none]; [.\mbox{ } \edge[draw=none]; $\leftrightarrow$ ] 
[.$F$ [.$T$ [.$\sim F$ \edge[draw=none]; $(\sim R$ ] ] 
\edge[draw=none]; [.\mbox{ } \edge[draw=none]; [.\mbox{ } 
\edge[draw=none]; $\wedge$ ] ] [.$F$ [.$\sim T$ \edge[draw=none]; $\sim Q)$ ] ] ]
]
}
\end{tikzpicture}
\end{frame}
\end{document}
1

If Forest is an option, you can automate the incremental uncovering, improve alignment and simplify the code required to specify the tree.

Forest is a specialist TikZ-based package for drawing trees. Some of the code below has a distinguished pedigree and the code is annotated accordingly to attribute (I hope) all those involved. (If I've omitted somebody, please let me know!)

I define a special Forest style semantic tree for drawing trees of the kind in the question.

  • All nodes are typeset in maths mode, but those above the last level (i.e. below the sentence) are wrapped by \text{} so that the T/F are upright. Hence, $...$ must be used here for negations.

  • The tree is automatically rendered visible incrementally, with one line per slide, as shown in the question. Since this doesn't require anything additional in the tree specification at all, I assume this is a simple enough method ;).

In addition,

  • I use \tnot for the tilde representation of not, which corrects the spacing. (TeX assumes that the symbol represents a binary relation, which is incorrect.)

  • \ob and \cb typeset opening and closing brackets without affecting the alignment of the following or preceding content.

  • connective=<connective> adds a node containing a connective to the right of the current one.

  • direct connect to=<node> draws a direct connection between the current node and the specified node, rather than following the usual path.

The document preamble is quite extensive but the pay-off is that the tree itself can be specified very simply and concisely.

\begin{frame}
  \centering
  \begin{forest}
    semantic tree
    [F
      [T
        [T
          [T
            [\ob P, connective=\land]
          ]
          [T
            [Q\cb, connective=\lor]
          ]
        ]
        [
          [F, direct connect to=!uu
            [R, connective=\leftrightarrow]
          ]
        ]
      ]
      [F
        [T
          [$\tnot$F
            [\ob\tnot R, connective=\land]
          ]
        ]
        [F
          [$\tnot$T
            [\tnot Q\cb]
          ]
        ]
      ]
    ]
  \end{forest}
\end{frame}

The result is 5 slides.

forest solution

Complete code:

\documentclass{beamer}
\usepackage{forest}
\usepackage{amsmath}
\makeatletter
\tikzset{% set up for transitions using tikz with beamer overlays - developed by Daniel (http://tex.stackexchange.com/a/55849/) and, in ear lier 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
  },
  transparent/.style={opacity=0.1,text opacity=0.1},
  opaque on/.style={alt=#1{}{transparent}},
  alerted/.style={color=alerttextdefaultfg},
  alert on/.style={alt=#1{alerted}{}},
}
\makeatother
\newcommand*{\tnot}{\ensuremath{\mathord{\sim}}}
\newcommand*\ob{\llap{(}}
\newcommand*\cb{\rlap{)}}
\forestset{%
  semantic tree/.style={%
    for tree={%
      math content,
      tier/.wrap pgfmath arg={tier ##1}{level()},
      parent anchor=children,
      child anchor=parent,
      grow'=90,
    },
    delay={%
      where level=0{%
        red,
      }{},
      where content={}{%
        coordinate,
      }{},
      where n children=0{%
        calign with current,
      }{%
        content/.wrap value=\text{##1},
      },
    },
    before packing={%
      where n children=0{%
        tier=terminus,
        no edge,
      }{},
    },
    before computing xy={%
      where n children=0{%
        l'-=10pt,
      }{},
    },
    before typesetting nodes={%
      for tree={%
        for current and ancestors/.wrap pgfmath arg={%
          visible from=##1,
        }{int(5-(level()))},
      },
    },
  },
  connective/.style={%
    before typesetting nodes={%
      insert after={[#1, math content]},
    },
  },
  direct connect to/.style={%
    edge path'=(#1.parent anchor) -- (.child anchor),
    tempcounta/.pgfmath=id("#1"),
    for nodewalk={%
      fake=parent,
      while={id()!=(tempcounta)}{current,fake=parent},
    }{%
      no edge,
    },
  },
  visible on/.style={% developed by Qrrbrbirlbel (http://tex.stackexchange.com/a/112471/)
    for tree={%
      /tikz/visible on={#1},
      edge={/tikz/visible on={#1}}}},
  opaque on/.style={%
    for tree={%
      /tikz/opaque on={#1},
      edge={/tikz/opaque on={#1}}}},
  alerted on/.style={%
    for tree={%
      /tikz/alerted on={#1},
      edge={/tikz/alerted on={#1}}}},
  visible from/.style={% based on Qrrbrbirlbel's answer at http://tex.stackexchange.com/a/112895/
    /tikz/visible on=<#1->,
    /tikz/every label/.append style={visible on=<#1->},
    for children={%
      /tikz/every edge label/.append style={visible on=<#1->},
      edge={/tikz/visible on=<#1->},
    },
  },
}
\begin{document}
\begin{frame}
  \centering
  \begin{forest}
    semantic tree
    [F
      [T
        [T
          [T
            [\ob P, connective=\land]
          ]
          [T
            [Q\cb, connective=\lor]
          ]
        ]
        [
          [F, direct connect to=!uu
            [R, connective=\leftrightarrow]
          ]
        ]
      ]
      [F
        [T
          [$\tnot$F
            [\ob\tnot R, connective=\land]
          ]
        ]
        [F
          [$\tnot$T
            [\tnot Q\cb]
          ]
        ]
      ]
    ]
  \end{forest}
\end{frame}
\end{document}
  • I'm a fan of Forest, I think it is fair to say ;). It is very, very nice! – cfr Aug 31 '16 at 2:16

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.