summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/tikz-qtree/pgftree.tex
diff options
context:
space:
mode:
Diffstat (limited to 'Master/texmf-dist/tex/latex/tikz-qtree/pgftree.tex')
-rw-r--r--Master/texmf-dist/tex/latex/tikz-qtree/pgftree.tex167
1 files changed, 121 insertions, 46 deletions
diff --git a/Master/texmf-dist/tex/latex/tikz-qtree/pgftree.tex b/Master/texmf-dist/tex/latex/tikz-qtree/pgftree.tex
index 4cdaad16433..a3fca8b0b48 100644
--- a/Master/texmf-dist/tex/latex/tikz-qtree/pgftree.tex
+++ b/Master/texmf-dist/tex/latex/tikz-qtree/pgftree.tex
@@ -1,5 +1,5 @@
% pgftree.tex
-% Version 1, 22 Dec 2009
+% Version 1.1, 25 Dec 2009
% Copyright 2009 by David Chiang
@@ -17,30 +17,81 @@
% with this program; if not, write to the Free Software Foundation, Inc.,
% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-% to do:
-% if \nodename does not exist as desired, wrap inside a rectangle node
-% don't use pgfsubpic internals, use anchors instead
+% New in version 1.1:
+% - major restructuring to not do arbitrary nesting of subpicture environments
+% - sideways trees
+
+% To do:
+% - trees with all leaves at same level
+% - if \nodename does not exist as desired, wrap inside a rectangle node
+% - don't use pgfsubpic internals
\newdimen\levelsep \levelsep=30pt
\newdimen\subtreesep \subtreesep=2pt
-\newif\ifrighttoleft
-\newif\ifupsidedown
+
+\def\leveldirection{down}
+\def\siblingdirection{right}
+
+% definitions of growing directions
+\def\pgftree@levelshift{\csname pgftree@levelshift@\leveldirection\endcsname}
+\def\pgftree@parentanchor{\csname pgftree@parentanchor@\leveldirection\endcsname}
+\def\pgftree@childanchor{\csname pgftree@childanchor@\leveldirection\endcsname}
+% these assume that the current subpicture is the child
+\def\pgftree@presiblingshift{\csname pgftree@presiblingshift@\siblingdirection\endcsname}
+\def\pgftree@postsiblingshift{\csname pgftree@postsiblingshift@\siblingdirection\endcsname}
+
+\def\pgftree@levelshift@down{\pgfpoint{0}{-\levelsep}}
+\def\pgftree@parentanchor@down{south}
+\def\pgftree@childanchor@down{north}
+
+\def\pgftree@levelshift@up{\pgfpoint{0}{\levelsep}}
+\def\pgftree@parentanchor@up{north}
+\def\pgftree@childanchor@up{south}
+
+\def\pgftree@levelshift@right{\pgfpoint{\levelsep}{0}}
+\def\pgftree@parentanchor@right{east}
+\def\pgftree@childanchor@right{west}
+
+\def\pgftree@levelshift@left{\pgfpoint{-\levelsep}{0}}
+\def\pgftree@parentanchor@left{west}
+\def\pgftree@childanchor@left{east}
+
+\def\pgftree@presiblingshift@right{\pgf@process{\pgf@x-\pgf@subpicminx \advance\pgf@x\subtreesep \pgf@y 0pt}}
+\def\pgftree@postsiblingshift@right{\pgf@process{\pgf@x\pgf@subpicmaxx \pgf@y 0pt}}
+
+\def\pgftree@presiblingshift@left{\pgf@process{\pgf@x-\pgf@subpicmaxx \advance\pgf@x-\subtreesep \pgf@y 0pt}}
+\def\pgftree@postsiblingshift@left{\pgf@process{\pgf@x\pgf@subpicminx \pgf@y 0pt}}
+
+\def\pgftree@presiblingshift@up{\pgf@process{\pgf@x 0pt \pgf@y-\pgf@subpicminy \advance\pgf@y\subtreesep}}
+\def\pgftree@postsiblingshift@up{\pgf@process{\pgf@x 0pt \pgf@y\pgf@subpicmaxy}}
+
+\def\pgftree@presiblingshift@down{\pgf@process{\pgf@x 0pt \pgf@y-\pgf@subpicmaxy \advance\pgf@y-\subtreesep}}
+\def\pgftree@postsiblingshift@down{\pgf@process{\pgf@x 0pt \pgf@y\pgf@subpicminy}}
+
+% for convenience if you are using \pgftree directly
\def\drawnode#1{\pgfnode{rectangle}{base}{#1}{\nodename}{\pgfusepath{discard}}}
\def\drawedge{%
-\pgfpathmoveto{\pgfpointanchor{\parentnodename}{\ifupsidedown north\else south\fi}}%
-\pgfpathlineto{\pgfpointanchor{\nodename}{\ifupsidedown south\else north\fi}}%
+\pgfpathmoveto{\pgfpointanchor{\parentnodename}{\pgftree@parentanchor}}%
+\pgfpathlineto{\pgfpointanchor{\nodename}{\pgftree@childanchor}}%
\pgfusepath{stroke}}
% local variables that we need to assign to inside a \pgfforeach
\newdimen\pgftree@childx
\newdimen\pgftree@savechildx
+\newdimen\pgftree@childy
+\newdimen\pgftree@savechildy
\newcount\pgftree@childi
\newcount\pgftree@savechildi
-\def\nodename{r}
+%%% \pgftree{subtree}
-%%% \pgftree{root}{subtrees}
+\def\pgftree#1{%
+\def\nodename{r}%
+#1
+\pgfplacesubpicture
+}
+%%% \pgfsubtree{root}{subtrees}
% The first argument draws the root node using PGF/TikZ commands.
% The node must be named \nodename.
@@ -48,60 +99,79 @@
% Token 2n-1 in the sequence draws the nth edge. It should draw an edge from \parentnodename to \nodename.
% Token 2n in the sequence draws the nth subtree. Its root must be named \nodename.
-\def\pgftree#1#2{%
-\pgfscope
-% create root node
-#1
+\pgfnewsubpicture{children}
+\pgfnewsubpicture{child}
+\newdimen\pgftree@lastchildx
+\newdimen\pgftree@lastchildy
+
+\def\pgfsubtree#1#2{%
\let\parentnodename\nodename
-\ifupsidedown
-\pgftransformshift{\pgfpoint{0}{\levelsep}}%
-\else
-\pgftransformshift{\pgfpoint{0}{-\levelsep}}%
-\fi
\pgftree@savechildx=\pgftree@childx
+\pgftree@savechildy=\pgftree@childy
\pgftree@savechildi=\pgftree@childi
-\pgfsubpicture
-\pgftree@childx=0pt%
+% Build subpicture with all the children and their subtrees. Save it in "children"
+{\pgftree@childx=0pt%
+\pgftree@childy=0pt%
\pgftree@childi=0%
-{\process@children #2}%
-\endpgfsubpicture
+\process@children #2}%
\ifnum\pgftree@childi>0%
-% Center children so that parent is midway between origins of first and last children
-\pgftransformshift{\pgfpointscale{-0.5}{\pgfpoint{\the\pgftree@childx}{0}}}%
-\pgfplacesubpicture
+\begin{pgfsubpicture}%
+% Create root node
+#1
+% Place children
+% move down \levelsep
+{\pgftransformshift{\pgftree@levelshift}%
+% center so that parent is midway between origins of first and last children
+\pgftransformshift{\pgfpointscale{-0.5}{\pgfpoint{\the\pgftree@childx}{\the\pgftree@childy}}}%
+\pgfplacesubpicture}
% Draw the edges
-\pgftree@childi=0%
-{\process@edges #2}%
+{\pgftree@childi=0%
+\process@edges #2}%
+\end{pgfsubpicture}%
+\else % leaf node
+\begin{pgfsubpicture}%
+#1
+\end{pgfsubpicture}%
\fi
\global\pgftree@childi=\pgftree@savechildi
\global\pgftree@childx=\pgftree@savechildx
-\endpgfscope
+\global\pgftree@childy=\pgftree@savechildy
}
\def\process@children{%
\pgfutil@ifnextchar\egroup
-{% Step back to origin of last child
-\advance\pgftree@childx by \ifrighttoleft-\pgf@subpicminx\else-\pgf@subpicmaxx\fi
-\global\pgftree@childx=\pgftree@childx
-\global\pgftree@childi=\pgftree@childi
+{% No more children, step back to origin of last child
+\global\pgftree@childx\pgftree@lastchildx
+\global\pgftree@childy\pgftree@lastchildy
+\global\pgftree@childi\pgftree@childi
+\pgfrestoresubpicture{children}% pass children back to caller
}%
{\@process@children}%
}
-\def\@process@children#1#2{%
-\edef\nodename{\parentnodename-\the\pgftree@childi}%
-\def\child{#2}%
-\pgfsubpicture
-#2%
-\endpgfsubpicture
+\def\@process@children#1#2{% #1 is the edge, #2 is the child
+% Build the current child and save it as "child"
+{\edef\nodename{\parentnodename-\the\pgftree@childi}%
+#2}%
+\pgfsavesubpicture{child}%
+\begin{pgfsubpicture}
+% Place previous children (if any)
\ifnum\pgftree@childi>0%
-\advance\pgftree@childx by \ifrighttoleft-\pgf@subpicmaxx\else-\pgf@subpicminx\fi
-\advance\pgftree@childx by \ifrighttoleft-\subtreesep\else\subtreesep\fi
-\fi
-\pgfscope
-\pgftransformshift{\pgfpoint{\the\pgftree@childx}{0}}%
+\pgfrestoresubpicture{children}%
\pgfplacesubpicture
-\endpgfscope
-\advance\pgftree@childx by \ifrighttoleft\pgf@subpicminx\else\pgf@subpicmaxx\fi
+\fi
+% Place current child
+\pgfrestoresubpicture{child}%
+\ifnum\pgftree@childi>0% the first child is always at 0
+\pgftree@presiblingshift \global\advance\pgftree@childx\pgf@x \global\advance\pgftree@childy\pgf@y
+\fi
+\global\pgftree@lastchildx\pgftree@childx
+\global\pgftree@lastchildy\pgftree@childy
+{\pgftransformshift{\pgfpoint{\the\pgftree@childx}{\the\pgftree@childy}}%
+\pgfplacesubpicture}%
+\pgftree@postsiblingshift \global\advance\pgftree@childx\pgf@x \global\advance\pgftree@childy\pgf@y
+\end{pgfsubpicture}%
+% Save the augmented row of children back into "children"
+\pgfsavesubpicture{children}%
\advance\pgftree@childi by 1%
\process@children
}
@@ -117,3 +187,8 @@
\advance\pgftree@childi by 1%
\process@edges
}
+
+\def\subtreeof#1{%
+% the subpicture which contains a node also contains exactly its subtree
+\csname pgf@sh@pi@#1\endcsname
+}