
sawine@9: %    tikz-qtree.tex
sawine@9: %    Version 1.11, 25 Dec 2010
sawine@9: 
sawine@9: %    Copyright (C) 2002, 2009 by David Chiang
sawine@9: 
sawine@9: %    This program is free software; you can redistribute it and/or modify
sawine@9: %    it under the terms of the GNU General Public License as published by
sawine@9: %    the Free Software Foundation; either version 2 of the License, or
sawine@9: %    (at your option) any later version.
sawine@9: 
sawine@9: %    This program is distributed in the hope that it will be useful,
sawine@9: %    but WITHOUT ANY WARRANTY; without even the implied warranty of
sawine@9: %    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
sawine@9: %    GNU General Public License for more details.
sawine@9: 
sawine@9: %    You should have received a copy of the GNU General Public License along
sawine@9: %    with this program; if not, write to the Free Software Foundation, Inc.,
sawine@9: %    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
sawine@9: 
sawine@9: % New in version 1.11:
sawine@9: % - make options compatible with standard tikz trees
sawine@9: 
sawine@9: % New in version 1.1:
sawine@9: 
sawine@9: % - sideways trees
sawine@9: 
sawine@9: %% These macros facilitate building up an object recursively before
sawine@9: %% putting it into the input stream.
sawine@9: 
sawine@9: \newtoks\@result
sawine@9: \def\@call#1#2{\let\@cont=#2\bgroup\@result={}#1}
sawine@9: \def\@return{\global\@result=\@result\egroup\@cont}
sawine@9: 
sawine@9: \def\@ifequal#1#2{\edef\testa{#1}\edef\testb{#2}\ifx\testa\testb}
sawine@9: 
sawine@9: %% scan a tree: this just scans a subtree and then puts it onto the
sawine@9: %% input stream
sawine@9: 
sawine@9: \def\Tree{\@call\@subtree\@Tree}
sawine@9: \def\@Tree{%
sawine@9: %\showthe\@result %debug
sawine@9: \ifpgfpicture % is there a test for tikzpicture?
sawine@9: \pgftree{\the\@result}%
sawine@9: \else
sawine@9: \tikzpicture[baseline]\pgftree{\the\@result}\endtikzpicture
sawine@9: \fi
sawine@9: }
sawine@9: 
sawine@9: %% scan a subtree
sawine@9: \newtoks\child@list
sawine@9: \newtoks\root@node
sawine@9: 
sawine@9: \def\@subtree[{%
sawine@9: \root@node={}%
sawine@9: \pgfutil@ifnextchar.{\@call\@interior\@@subtree}{\@@@subtree}}
sawine@9: \def\@@subtree{%
sawine@9: \root@node=\@result
sawine@9: \@@@subtree
sawine@9: }
sawine@9: \def\@@@subtree{%
sawine@9: \@call\@children\@@@@subtree
sawine@9: }
sawine@9: \def\@@@@subtree]{%
sawine@9: \child@list=\@result
sawine@9: \pgfutil@ifnextchar.{\@call\@interior\@@@@@subtree}{\@@@@@@subtree}}
sawine@9: \def\@@@@@subtree{%
sawine@9: %%% Check for mismatch.
sawine@9: \@ifequal{\the\root@node}{\pgfutil@empty}%
sawine@9:   \root@node=\@result
sawine@9: \fi
sawine@9: \@ifequal{\the\root@node}{\the\@result}\else
sawine@9:   \message{Warning: mismatched labels, \the\root@node{} and \the\@result.}%
sawine@9: \fi
sawine@9: \@@@@@@subtree
sawine@9: }
sawine@9: \def\@@@@@@subtree{%
sawine@9: \@ifequal{\the\root@node}{\pgfutil@empty}%
sawine@9: \edef\act{\noexpand\@result={\noexpand\pgfsubtree{\noexpand\path coordinate (\noexpand\nodename);}{\the\child@list}}}%
sawine@9: \else
sawine@9: \edef\act{\noexpand\@result={\noexpand\pgfsubtree{\the\root@node}{\the\child@list}}}%
sawine@9: \fi
sawine@9: \act
sawine@9: \@return}
sawine@9: 
sawine@9: %% scan a sequence of subtrees or leaves
sawine@9: 
sawine@9: \newif\ifscanned@edge
sawine@9: 
sawine@9: \def\@children{%
sawine@9: \scanned@edgefalse
sawine@9: \child@list{}%
sawine@9: \@@children}
sawine@9: \def\@@children{%
sawine@9: \pgfutil@ifnextchar]{\@result\child@list\@return}{% end of children
sawine@9: \pgfutil@ifnextchar\edge{% explicit edge
sawine@9: \ifscanned@edge
sawine@9: \message{Warning: more than one edge given for a single child}\let\next\@@children % ignore
sawine@9: \else
sawine@9: \scanned@edgetrue\let\next\@@@children
sawine@9: \fi
sawine@9: \@call\@edge\next}{%
sawine@9: % else, a real node is next
sawine@9: \ifscanned@edge\else % no explicit edge, supply default
sawine@9: \expandafter\child@list\expandafter{\the\child@list{\edge@adapter{}}}%
sawine@9: \fi
sawine@9: \scanned@edgefalse
sawine@9: \pgfutil@ifnextchar[{\@call\@subtree\@@@children}% subtree
sawine@9: {\@call\@leaf\@@@children}% leaf
sawine@9: }}}
sawine@9: \def\@@@children{%
sawine@9: % wrap child inside curly braces
sawine@9: \expandafter\@result\expandafter{\expandafter{\the\@result}}%
sawine@9: \edef\act{\noexpand\child@list{\the\child@list \the\@result}}\act
sawine@9: \@@children
sawine@9: }
sawine@9: 
sawine@9: \def\@interior.{\@result{\node[alias=\nodename][every tree node,every internal node]}\@label}
sawine@9: 
sawine@9: \def\@leaf{\@call\@label\@@leaf}
sawine@9: \def\@@leaf{\edef\act{\noexpand\@result{\noexpand\pgfsubtree{\noexpand\node[alias=\noexpand\nodename][every tree node,every leaf node]\the\@result}{}}}\act\@return}
sawine@9: 
sawine@9: \def\@edge\edge #1;{%
sawine@9: \@result{\edge@adapter{#1}}%
sawine@9: \@return}
sawine@9: \def\edge@adapter#1{%
sawine@9: \let\tikzparentnode\parentnodename
sawine@9: \let\tikzchildnode\nodename
sawine@9: \path edge from parent #1;%
sawine@9: }
sawine@9: 
sawine@9: % a label is either text or PGF/TikZ code starting with \node
sawine@9: \def\@label{\pgfutil@ifnextchar\node{\@litlabel}{\@@label}}
sawine@9: \def\@@label#1 {%
sawine@9: \expandafter\@result\expandafter{\the\@result{#1};}%
sawine@9: \@return}
sawine@9: 
sawine@9: % try to copy \node command into \@result without stripping braces
sawine@9: \def\@litlabel\node{\@@litlabel}
sawine@9: \def\@@litlabel{\pgfutil@ifnextchar\bgroup{\@@@litlabel}{\@@@@litlabel}}
sawine@9: \def\@@@litlabel#1{\expandafter\@result\expandafter{\the\@result {#1}}\@@litlabel}
sawine@9: \def\@@@@litlabel#1;{\expandafter\@result\expandafter{\the\@result #1;}\@return}
sawine@9: 
sawine@9: % predefined edges
sawine@9: 
sawine@9: \def\tree@edge#1#2{(#1.\pgftree@parentanchor) -- (#2.\pgftree@childanchor)}
sawine@9: 
sawine@9: \def\roof@edge#1#2{\csname roof@edge@\leveldirection\endcsname{#1}{#2}}
sawine@9: \def\roof@edge@down#1#2{(#1.south) -- (#2.north west) -- (#2.north east) -- cycle}
sawine@9: \def\roof@edge@up#1#2{(#1.north) -- (#2.south west) -- (#2.south east) -- cycle}
sawine@9: \def\roof@edge@left#1#2{(#1.west) -- (#2.north east) -- (#2.south east) -- cycle}
sawine@9: \def\roof@edge@right#1#2{(#1.east) -- (#2.north west) -- (#2.south west) -- cycle}
sawine@9: 
sawine@9: %%% Options
sawine@9: \pgfkeysgetvalue{/tikz/level distance/.@cmd}{\orig@leveldistance}
sawine@9: \tikzoption{level distance}{\pgfmathsetlength\levelsep{#1}\orig@leveldistance#1\pgfeov}
sawine@9: \pgfkeysgetvalue{/tikz/sibling distance/.@cmd}{\orig@siblingdistance}
sawine@9: \tikzoption{sibling distance}{\pgfmathsetlength\subtreesep{#1}\orig@siblingdistance#1\pgfeov} % different semantics
sawine@9: 
sawine@9: % I don't really like this scheme
sawine@9: \pgfkeysgetvalue{/tikz/grow/.@cmd}{\orig@grow}
sawine@9: \tikzoption{grow}{\csname grow@#1\endcsname\orig@grow#1\pgfeov}
sawine@9: \pgfkeysgetvalue{/tikz/grow'/.@cmd}{\orig@growprime}
sawine@9: \tikzoption{grow'}{\csname growprime@#1\endcsname\orig@growprime#1\pgfeov}
sawine@9: \def\grow@up{\def\leveldirection{up}\def\siblingdirection{left}}
sawine@9: \def\grow@down{\def\leveldirection{down}\def\siblingdirection{right}}
sawine@9: \def\growprime@up{\def\leveldirection{up}\def\siblingdirection{right}}
sawine@9: \def\growprime@down{\def\leveldirection{down}\def\siblingdirection{left}}
sawine@9: \def\grow@left{\def\leveldirection{left}\def\siblingdirection{down}}
sawine@9: \def\grow@right{\def\leveldirection{right}\def\siblingdirection{up}}
sawine@9: \def\growprime@left{\def\leveldirection{left}\def\siblingdirection{up}}
sawine@9: \def\growprime@right{\def\leveldirection{right}\def\siblingdirection{down}}
sawine@9: 
sawine@9: % defaults appropriate for linguistic trees
sawine@9: \tikzset{every tree node/.style={anchor=base}}
sawine@9: \tikzset{every leaf node/.style={}}
sawine@9: \tikzset{every internal node/.style={}}
sawine@9: \def\tikz@edge@to@parent@path{\tree@edge{\tikzparentnode}{\tikzchildnode}}
sawine@9: 
sawine@9: % predefined roof style
sawine@9: \tikzset{roof/.style={edge from parent path=\roof@edge{\tikzparentnode}{\tikzchildnode}}}
sawine@9: 




