Article View: comp.tex
Article #1Drawing trees in LaTeX (was: Re: Digital circuits in LaTeX.)
From: Peter Vanroose
Date: Thu, 12 Jul 1990 15:16
Date: Thu, 12 Jul 1990 15:16
144 lines
6857 bytes
6857 bytes
In article <50460@iuvax.cs.indiana.edu>, Raja Sooriamurthi writes:
> Have you included circuits with LaTeX text before and if so what did you use.
> Information on other packages/macros is welcome.
The following macros let you draw a (binary or ternary) tree of any size.
For each "internal node", you only have to specify which are the descending
nodes, with a \branch command. (\tbranch for ternary node.)
To this end, nodes are given a label (only used internally!)
These macros will give you some ideas on designing similar things for, e.g.,
digital circuits.
Trees are constructed with labels on the branches (default 0 and 1), and
with text (e.g., its name or value) on the nodes. The first parameter to
\branch (0, 1, 2 or 3) determines the steepness of the branches.
Example:
\begin{picture}(16,12)
\setlength\unitlength{2mm}
\branchlabels ABC % 012 is the default
\root(2,10) 0. % root at absolute coordinate (2,10)
% its (internally used) label is 0
% the space before the 0 is obligatory
\branch2{16} 0:1,2. % node 0 (i.e., the root) has children 1 and 2
% the text "1.00" is written above it
% space is optional, :,. are obligatory
\leaf{4}{$u_1$} 1. % node 1 is a leaf
% "0.45" written above, "$u_1$" to the right
\branch2{12} 2:3,7. % branch to node 3 goes up, and has label A
\tbranch2{9} 3:4,5,6.
\leaf{4}{$u_3$}4. % the symbols 0--7 can be replaced by anything
\leaf{3}{$u_4$}5.
\leaf{2}{$u_5$}6.
\leaf{3}{$u_2$} 7.
\end{picture}
will typeset something like
4 4
. u . u
/ 1 A/ 3
A/ /
16/ 9 / 3
+--./ ./_B_. u
-+- \ / \C 4
\B A/ \
\ 12/ \ 2
\./ \. u
\B 5
\
\ 3
\. u
2
Peter Vanroose
peter@dit.lth.se
%---------- trees.sty --------------------
% Peter Vanroose, 18 april 1990.
\newcount\branchwidth\newcount\xcoord\newcount\ycoord\newcount\coordstep
\def\branchlabels#1#2#3{\edef\labela{#1}\edef\labelb{#2}\edef\labelc{#3}}
\branchlabels012
%
\def\root(#1,#2) #3.{\catcode32=9 % ignore spaces
% #3 is the node label of the root
\expandafter\xdef\csname nodex#3\endcsname{#1}
\expandafter\xdef\csname nodey#3\endcsname{#2}
\put(#1,#2){
\put(0,0){\line(-1,0)6}\put(-6,0){\line(0,-1)3}\put(-9,-3){\line(1,0)6}}}
%
\def\branch#1#2#3:#4,#5.{ \branchwidth=#1
\xcoord=\expandafter\number\csname nodex#3\endcsname
\ycoord=\expandafter\number\csname nodey#3\endcsname
\edef\coord{(\number\xcoord,\number\ycoord)}
\advance\xcoord by 12 % #4,#5 are the labels of the childs
\expandafter\xdef\csname nodex#4\endcsname{\number\xcoord}
\expandafter\xdef\csname nodex#5\endcsname{\number\xcoord}
\ifcase\branchwidth
\advance\ycoord by 24\coordstep=-48 \or % case 0
\advance\ycoord by 12\coordstep=-24 \or % case 1
\advance\ycoord by 6\coordstep=-12 \or % case 2
\advance\ycoord by 3\coordstep=- 6 \fi % case 3
\expandafter\xdef\csname nodey#4\endcsname{\number\ycoord}
\advance\ycoord by \coordstep
\expandafter\xdef\csname nodey#5\endcsname{\number\ycoord}
\expandafter\put\coord{
\put(0,0){\circle*1}\put(0,1.5){\makebox(0,0){\scriptsize #2}}
\ifcase\branchwidth
\put(0,0){\line(1,2){12}}\put(0,0){\line(1,-2){12}}
\put(5,12){\tiny$\labela$}\put(5,-12){\tiny$\labelb$} \or % case 0
\put(0,0){\line(1,1){12}}\put(0,0){\line(1,-1){12}}
\put(6,7){\tiny$\labela$}\put(6,-8){\tiny$\labelb$} \or % case 1
\put(0,0){\line(2,1){12}}\put(0,0){\line(2,-1){12}}
\put(6,3.6){\tiny$\labela$}\put(6,-5){\tiny$\labelb$} \or % case 2
\put(0,0){\line(4,1){12}}\put(0,0){\line(4,-1){12}}
\put(6,2){\tiny$\labela$}\put(6,-3){\tiny$\labelb$} \fi % case 3
}}
%
\def\tbranch#1#2#3:#4,#5,#6.{
\branchwidth=#1
\xcoord=\expandafter\number\csname nodex#3\endcsname
\ycoord=\expandafter\number\csname nodey#3\endcsname
\edef\coord{(\number\xcoord,\number\ycoord)}
\advance\xcoord by 12
\expandafter\xdef\csname nodex#4\endcsname{\number\xcoord}
\expandafter\xdef\csname nodex#5\endcsname{\number\xcoord}
\expandafter\xdef\csname nodex#6\endcsname{\number\xcoord}
\ifcase\branchwidth
\advance\ycoord by 24\coordstep=-24 \or % case 0
\advance\ycoord by 12\coordstep=-12 \or % case 1
\advance\ycoord by 6\coordstep= -6 \or % case 2
\advance\ycoord by 3\coordstep= -3 \fi % case 3
\expandafter\xdef\csname nodey#4\endcsname{\number\ycoord}
\advance\ycoord by \coordstep
\expandafter\xdef\csname nodey#5\endcsname{\number\ycoord}
\advance\ycoord by \coordstep
\expandafter\xdef\csname nodey#6\endcsname{\number\ycoord}
\expandafter\put\coord{
\put(0,0){\circle*1}\put(0,1.5){\makebox(0,0){\scriptsize #2}}
\ifcase\branchwidth
\put(0,0){\line(1,2){12}}\put(0,0){\line(1,0){12}}\put(0,0){\line(1,-2){12}}
\put(5,12){\tiny$\labela$}\put(5,0.6){\tiny$\labelb$}\put
(5,-12){\tiny$\labelc$} \or % case 0
\put(0,0){\line(1,1){12}}\put(0,0){\line(1,0){12}}\put(0,0){\line(1,-1){12}}
\put(6,7){\tiny$\labela$}\put(6,0.5){\tiny$\labelb$}\put
(6,-8){\tiny$\labelc$} \or % case 1
\put(0,0){\line(2,1){12}}\put(0,0){\line(1,0){12}}\put(0,0){\line(2,-1){12}}
\put(6,3.6){\tiny$\labela$}\put(6,0.5){\tiny$\labelb$}\put
(6,-5){\tiny$\labelc$} \or % case 2
\put(0,0){\line(4,1){12}}\put(0,0){\line(1,0){12}}\put(0,0){\line(4,-1){12}}
\put(6,2){\tiny$\labela$}\put(6,0.25){\tiny$\labelb$}\put
(6,-3){\tiny$\labelc$} \fi % case 3
}}
%
\def\leaf#1#2#3.{
\xcoord=\expandafter\number\csname nodex#3\endcsname
\ycoord=\expandafter\number\csname nodey#3\endcsname
\edef\coord{(\number\xcoord,\number\ycoord)}
\expandafter\put\coord{
\put(0,0){\circle*1}\put(0,1.5){\makebox(0,0){\scriptsize #1}}
\put(2,-0.6){\makebox(0,0)[l]{#2}}}}
%
%-------- end of trees.sty -------------
Technical Details
Message-ID:
<1990Jul12.151603.15930@lth.se>Path:
arc1-ca.pugleaf.net!utzoo!attcan!uunet!aplcen!uakari.primate.wisc.edu!zaphod.mps.ohio-state.edu!usc!snorkelwacker!bloom-beacon!eru!luth!sunic!lth.se!newsuserReferences:
<50460@iuvax.cs.indiana.edu>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!aplcen!uakari.primate.wisc.edu!zaphod.mps.ohio-state.edu!usc!snorkelwacker!bloom-beacon!eru!luth!sunic!lth.se!newsuser From: peter@dit.lth.se (Peter Vanroose) Newsgroups: comp.tex,comp.text.tex Subject: Drawing trees in LaTeX (was: Re: Digital circuits in LaTeX.) Summary: Drawing trees with LaTeX' picture environment Keywords: LaTeX, picture, trees Message-ID: <1990Jul12.151603.15930@lth.se> Date: 12 Jul 90 15:16:03 GMT References: <50460@iuvax.cs.indiana.edu> Sender: newsuser@lth.se (LTH network news server) Organization: Dep. of Information Theory, Lund Institute of Technology, Sweden Lines: 144