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