| 1 |
% Tree -- a macro to make aligned (horizontal) trees in TeX |
| 2 |
% |
| 3 |
% Input is of the form |
| 4 |
% \tree |
| 5 |
% item |
| 6 |
% \subtree |
| 7 |
% \leaf{item} |
| 8 |
% . |
| 9 |
% . |
| 10 |
% . |
| 11 |
% \endsubtree |
| 12 |
% \subtree |
| 13 |
% . |
| 14 |
% . |
| 15 |
% . |
| 16 |
% \endsubtree |
| 17 |
% \endsubtree |
| 18 |
% \endtree |
| 19 |
% |
| 20 |
% Nesting is to any level. \leaf is defined as a subtree of one item: |
| 21 |
% \def\leaf#1{\subtree#1\endsubtree}. |
| 22 |
% |
| 23 |
% A structure: |
| 24 |
% \subtree |
| 25 |
% item_part1 |
| 26 |
% item_part2 |
| 27 |
% . |
| 28 |
% . |
| 29 |
% . |
| 30 |
% |
| 31 |
% will print item_part2 directly below item_part1 as a single item |
| 32 |
% as if they were in a \box. |
| 33 |
% |
| 34 |
% The macro is a 3-pass macro. On the first pass it sets up a data |
| 35 |
% structure from the \subtree ... \endsubtree definitions. On the second pass |
| 36 |
% it recursively calculates the width of each level of the tree. On the third |
| 37 |
% pass it sets up the boxes, glue and rules. |
| 38 |
% |
| 39 |
% By David Eppstein, TUGboat, vol. 6 (1985), no. 1, pp. 31--35. |
| 40 |
% Transcribed by Margaret Kromer (peg), Feb., 1986. |
| 41 |
% |
| 42 |
% Permission to add to Source Forge repository granted by David Eppstein |
| 43 |
% ([email protected]) on 14 Nov 2000. In his email, he said: |
| 44 |
% |
| 45 |
% Sure. The original TeX source for the article is online, at |
| 46 |
% http://www.ics.uci.edu/~eppstein/pubs/p-ttree.tex.Z, if that helps. |
| 47 |
% |
| 48 |
% |
| 49 |
% Pass 1 |
| 50 |
% At the end of pass 1, the tree is coded as a nested collection of \hboxes |
| 51 |
% and \vboxes. |
| 52 |
\newbox\treebox\newcount\treeboxcnt |
| 53 |
\def\tree{\message{Begin tree}\treeboxcnt=1\global\setbox\treebox=\boxtree} |
| 54 |
\def\subtree{\ettext \advance\treeboxcnt by 1 \boxtree} |
| 55 |
\def\leaf#1{\subtree#1\endsubtree} |
| 56 |
\def\endsubtree{\ettext \egroup \advance\treeboxcnt-1{}% |
| 57 |
\ifnum\treeboxcnt=-1 \treeerrora\fi} |
| 58 |
\def\endtree{\endsubtree \ifnum\treeboxcnt>0 \treeerrorb\fi% |
| 59 |
\settreesizes \typesettree \message{-- end tree}} |
| 60 |
% Error messages for unbalanced tree |
| 61 |
\def\treeerrora{\errhelp=\treeerrorahelp% |
| 62 |
\errmessage{Unbalanced tree -- too many endsubtrees}} |
| 63 |
\newhelp\treeerrorahelp{There are more subtrees closed than opened} |
| 64 |
\def\treeerrorb{\errhelp=\treeerrorbhelp% |
| 65 |
\errmessage{Unbalanced tree -- not enough endsubtrees}} |
| 66 |
\newhelp\treeerrorbhelp{Not all the subtrees of the tree are closed. |
| 67 |
If you continue, you'll get some mysterious secondary errors.} |
| 68 |
% Set up \vbox containing root of tree |
| 69 |
\newif\iftreetext\treetextfalse % Whether still aligning text |
| 70 |
\def\boxtree{\hbox\bgroup % Start outer box of tree or subtree |
| 71 |
\baselineskip 2.5ex % Narrow line spacing slightly |
| 72 |
\tabskip 0pt % No spurious glue in alignment |
| 73 |
\vbox\bgroup % Start inner text \vbox |
| 74 |
\treetexttrue % Remember for \ettext |
| 75 |
\let\par\crcr \obeylines % New line breaks without explicit \cr |
| 76 |
\halign\bgroup##\hfil\cr} % Start alignment with simple template |
| 77 |
\def\ettext{\iftreetext % Are we still in inner text \vbox? |
| 78 |
\crcr\egroup \egroup \fi} % Yes, end alignment and box |
| 79 |
% Pass 2 |
| 80 |
% Recursively calculate widths of tree with \setsizes; keep results in |
| 81 |
% \treesizes; \treewidth contains total width calculated so far. \treeworkbox |
| 82 |
% is workspace containing subtree being sized. |
| 83 |
\newbox\treeworkbox |
| 84 |
\def\cons#1#2{\edef#2{\xmark #1#2}} % Add something to start of list |
| 85 |
\def\car#1{\expandafter\docar#1\docar} % Take first element of list |
| 86 |
\def\docar\xmark#1\xmark#2\docar{#1} % ..by ignoring rest in expansion |
| 87 |
\def\cdr#1{\expandafter\docdr#1\docdr#1}% Similarly, drop first element |
| 88 |
\def\docdr\xmark#1\xmark#2\docdr#3{\def#3{\xmark #2}} |
| 89 |
\def\xmark{\noexpand\xmark} % List separator expands to self |
| 90 |
\def\nil{\xmark} % Empty list is just separator |
| 91 |
\def\settreesizes{\setbox\treeworkbox=\copy\treebox% |
| 92 |
\global\let\treesizes\nil \setsizes} |
| 93 |
\newdimen\treewidth % Width of this part of the tree |
| 94 |
\def\setsizes{\setbox\treeworkbox=\hbox\bgroup% Get a horiz list as a workspace |
| 95 |
\unhbox\treeworkbox\unskip % Take tree, unpack it into horiz list |
| 96 |
\inittreewidth % Get old width at this level |
| 97 |
\sizesubtrees % Recurse through all subtrees |
| 98 |
\sizelevel % Now set width from remaining \vbox |
| 99 |
\egroup} % All done, finish our \hbox |
| 100 |
\def\inittreewidth{\ifx\treesizes\nil % If this is the first at this level |
| 101 |
\treewidth=0pt % ..then we have no previous max width |
| 102 |
\else \treewidth=\car\treesizes % Otherwise take old max level width |
| 103 |
\global\cdr\treesizes % ..and advance level width storage |
| 104 |
\fi} % ..in preparation for next level. |
| 105 |
\def\sizesubtrees{\loop % For each box in horiz list (subtree) |
| 106 |
\setbox\treeworkbox=\lastbox \unskip % ..pull it off list and flush glue |
| 107 |
\ifhbox\treeworkbox \setsizes % If hbox, it's a subtree - recurse |
| 108 |
\repeat} % ..and loop; end loop on tree text |
| 109 |
\def\sizelevel{% |
| 110 |
\ifdim\treewidth<\wd\treeworkbox % If greater than previous maximum |
| 111 |
\treewidth=\wd\treeworkbox \fi % Then set max to new high |
| 112 |
\global\cons{\the\treewidth}\treesizes}% In either case, put back on list |
| 113 |
% Pass 3 |
| 114 |
% Recursively typeset tree with \maketree by adding an \hbox containing |
| 115 |
% a subtree (in \treebox) to the horizontal list. |
| 116 |
\newdimen\treeheight % Height of this part of the tree |
| 117 |
\newif\ifleaf % Tree has no subtrees (is a leaf) |
| 118 |
\newif\ifbotsub % Bottom subtree of parent |
| 119 |
\newif\iftopsub % Top subtree of parent |
| 120 |
\def\typesettree{\medskip\maketree\medskip} % Make whole tree |
| 121 |
\def\maketree{\hbox{\treewidth=\car\treesizes % Get width at this level |
| 122 |
\cdr\treesizes % Set up width list for recursion |
| 123 |
\makesubtreebox\unskip % Set \treebox to text, make subtrees |
| 124 |
\ifleaf \makeleaf % No subtrees, add glue |
| 125 |
\else \makeparent \fi}} % Have subtrees, stick them at right |
| 126 |
{\catcode`@=11 % Be able to use \voidb@x |
| 127 |
\gdef\makesubtreebox{\unhbox\treebox % Open up tree or subtree |
| 128 |
\unskip\global\setbox\treebox\lastbox % Pick up very last box |
| 129 |
\ifvbox\treebox % If we're already at the \vbox |
| 130 |
\global\leaftrue \let\next\relax % ..then this is a leaf |
| 131 |
\else \botsubtrue % Otherwise, we have subtrees |
| 132 |
\setbox\treeworkbox\box\voidb@x % Init stack of processed subs |
| 133 |
\botsubtrue \let\next\makesubtree % ..and call \maketree on them |
| 134 |
\fi \next}} % Finish up for whichever it was |
| 135 |
\def\makesubtree{\setbox1\maketree % Call \maketree on this subtree |
| 136 |
\unskip\global\setbox\treebox\lastbox % Pick up box before it |
| 137 |
\treeheight=\ht1 % Get height of subtree we made |
| 138 |
\advance\treeheight 2ex % Add some room around the edges |
| 139 |
\ifhbox\treebox \topsubfalse % If picked up box is a \vbox, |
| 140 |
\else \topsubtrue \fi % ..this is the top, otherwise not |
| 141 |
\addsubtreebox % Stack subtree with the rest |
| 142 |
\iftopsub \global\leaffalse % If top, remember not a leaf |
| 143 |
\let\next\relax \else % ..(after recursion), set return |
| 144 |
\botsubfalse \let\next\makesubtree % Otherwise, we have more subtrees |
| 145 |
\fi \next} % Do tail recursion or return |
| 146 |
\def\addsubtreebox{\setbox\treeworkbox=\vbox{\subtreebox\unvbox\treeworkbox}} |
| 147 |
\def\subtreebox{\hbox\bgroup % Start \hbox of tree and lines |
| 148 |
\vbox to \treeheight\bgroup % Start \vbox for vertical rules |
| 149 |
\ifbotsub \iftopsub \vfil % If both bottom and top subtree |
| 150 |
\hrule width 0.4pt % ..vertical rule is just a dot |
| 151 |
\else \treehalfrule \fi \vfil % Bottom gets half-height rule |
| 152 |
\else \iftopsub \vfil \treehalfrule % Top gets half-height the other way |
| 153 |
\else \hrule width 0.4pt height \treeheight \fi\fi % Middle, full height |
| 154 |
\egroup % Finish vertical rule \vbox |
| 155 |
\treectrbox{\hrule width 1em}\hskip 0.2em\treectrbox{\box1}\egroup} |
| 156 |
\def\treectrbox#1{\vbox to \treeheight{\vfil #1\vfil}} |
| 157 |
\def\treehalfrule{\dimen\treeworkbox=\treeheight % Get total height |
| 158 |
\divide\dimen\treeworkbox 2% |
| 159 |
\advance\dimen\treeworkbox 0.2pt % Divide by two, add half horiz height |
| 160 |
\hrule width 0.4pt height \dimen\treeworkbox}% Make a vertical rule that high |
| 161 |
\def\makeleaf{\box\treebox} % Add leaf box to horiz list |
| 162 |
\def\makeparent{\ifdim\ht\treebox>% |
| 163 |
\ht\treeworkbox % If text is higher than subtrees |
| 164 |
\treeheight=\ht\treebox % ..use that height |
| 165 |
\else \treeheight=\ht\treeworkbox \fi % Otherwise use height of subtrees |
| 166 |
\advance\treewidth-\wd\treebox % Take remainder of level width |
| 167 |
\advance\treewidth 1em % ..after accounting for text and glue |
| 168 |
\treectrbox{\box\treebox}\hskip 0.2em % Add text, space before connection |
| 169 |
\treectrbox{\hrule width \treewidth}% |
| 170 |
\treectrbox{\box\treeworkbox}} % Add \hrule, subs |
| 171 |
% No idea what \spouse is supposed to do... wasn't included |
| 172 |
\def\spouse{\bf} |