Debian Codegraph @

texmf-dist/doc/metapost/mptrees/mptrees-doc.tex

raw
1 \documentclass[11pt,a4paper,english]{article}
2
3 % Code with metapost output thanks to YannD at texnique.fr
4 % https://texnique.fr/osqa/questions/11076/comment-obtenir-le-code-metapost-et-la-figure-dans-un-document-avec-luamplib
5
6 % To compile:
7 % lualatex repere-doc.tex
8
9 \usepackage{mathtools}
10 \usepackage{unicode-math}
11 \setmainfont{STIX Two Text}
12 \setmathfont{STIX Two Math}
13 \usepackage{fontawesome5}
14
15 \usepackage{geometry}
16 \geometry{hmargin=2cm,vmargin={1.5cm,1.8cm},includefoot}
17
18 \usepackage[bottom]{footmisc}
19
20 \usepackage{mflogo}
21
22 \usepackage[output-decimal-marker={,}]{siunitx}
23 \usepackage{esvect}
24 \usepackage{nicefrac}
25
26 \usepackage{multicol}
27 \setlength{\multicolsep}{3pt}
28 \setlength{\columnsep}{0pt}
29
30 \usepackage[toc]{multitoc}
31
32 \usepackage{enumitem}
33
34 \usepackage[svgnames]{xcolor}
35
36 \usepackage{graphicx}
37
38 \usepackage{url}
39 \usepackage{verbatim}
40 \usepackage{fancyvrb}
41
42 \usepackage{array}
43 \usepackage{colortbl}
44 \usepackage{tabularx}
45 \renewcommand{\tabularxcolumn}[1]{>{\arraybackslash}m{#1}}
46 \newcolumntype{M}[1]{>{\arraybackslash}m{#1}}
47 \usepackage{longtable}
48 \usepackage{tabularray}
49
50 \usepackage{luamplib}
51 \everymplib{verbatimtex \leavevmode etex;
52 input mptrees;
53 beginfig(1);}
54 \everyendmplib{endfig;}
55 \mplibnumbersystem{decimal}
56
57 \usepackage{tcolorbox}
58 \tcbuselibrary{listings,skins}%,documentation}
59
60 \usepackage{babel}
61
62
63 \usepackage{marginnote}
64
65 \usepackage[colorlinks=true,urlcolor=blue]{hyperref}
66 %\hypersetup{colorlinks=true,allcolors=red,linkcolor=red}
67
68 \usepackage{listings}
69
70 \lstset{columns=flexible,%
71 basicstyle=\color{darkgray}\ttfamily,
72 literate={é}{{\'e}}1,
73 keywordstyle=\color{black}\bfseries}
74
75 \lstdefinestyle{mpost}{language={MetaPost},
76 morekeywords={tree}
77 }
78
79 \makeatletter
80 \tcbset{%
81 listing metapost/.code={%
82 \def\tcbuselistingtext@input{%
83 \begin{mplibcode}
84 input \jobname.listing;
85 \end{mplibcode}}%
86 }
87 }
88 \makeatother
89
90 \definecolor{CoulParam}{rgb}{0.4,0,0.5}
91
92 \definecolor{TestC}{rgb}{0.963,0.971,0.994}
93 \colorlet{FondCommandes}{black!60!yellow!10!white}
94
95 \tcbset{boites/.style={left=1em,right=1em,
96 colback=white,
97 boxrule=0pt,
98 colframe=white,
99 fonttitle=\bfseries\ttfamily,
100 colbacktitle=FondCommandes,
101 before title={\hspace{-1em}},
102 arc=0pt,
103 after=\medskip,
104 }}
105
106 \DeclareTColorBox{rpobjet}{ v m }{
107 boites,
108 title={#1},
109 coltitle=blue,
110 colbacktitle=blue!5!white,
111 after title={\hfill #2},
112 }
113
114 \DeclareTColorBox{mptparam}{ v m v}{
115 boites,
116 title={#1},
117 coltitle=CoulParam,
118 colbacktitle=CoulParam!5!white,
119 after title={\hfill #2, {\normalfont default :} #3},
120 }
121
122 \DeclareTColorBox{rpdeclaration}{ v }{
123 boites,
124 title={#1},
125 coltitle=black,
126 colbacktitle=black!5!white,
127 }
128
129 \DeclareTColorBox{rplabel}{ v }{
130 boites,
131 title={#1},
132 coltitle=red,
133 colbacktitle=red!50!black!5!white,
134 }
135
136 \DefTblrTemplate{contfoot-text}{default}{}
137 \DefTblrTemplate{conthead-text}{default}{}
138 \DefTblrTemplate{caption-tag}{default}{}
139 \DefTblrTemplate{caption-sep}{default}{}
140 \DefTblrTemplate{caption-text}{default}{}
141 \NewDocumentEnvironment{rpparam}{ +b}
142 {\par\medskip\begin{center}
143 \begin{longtblr}[label=none,caption={}]{
144 rowhead=2,
145 verb,
146 hlines={2pt,white},
147 columns={c},
148 row{1}=white,
149 row{2-Z}={bg=CoulParam!5!white,m},
150 column{4}={wd=8cm,fg=black,j},
151 cell{3-Z}{1-3}={cmd=\testparam},
152 cell{3-Z}{1}={cmd=\bfseries\testparam}
153 }
154 \SetCell[c=4]{c} Paramètres \\
155 Nom & Type & Défaut & \\
156 #1
157 \end{longtblr}
158 \end{center}\bigskip}
159 {}
160
161
162
163 \tcbset{mot/.style={colback=FondCommandes,
164 verbatim,
165 arc=0pt,
166 outer arc=0pt,
167 top=0pt,bottom=0pt,left=0mm,right=0mm,
168 boxrule=0pt}}
169
170 \DeclareTotalTCBox{\rpdec}{v}{mot,coltext=black}{#1}
171 \DeclareTotalTCBox{\rpfig}{v}{mot,coltext=blue}{#1}
172 \DeclareTotalTCBox{\rplab}{v}{mot,coltext=red}{#1}
173 \DeclareTotalTCBox{\rppar}{v}{mot,coltext=CoulParam}{#1}
174
175 \NewDocumentCommand{\testparam}{v}{\textcolor{CoulParam}{\texttt{#1}}}
176 \NewDocumentCommand{\desstab}{v}{\textbf{\texttt{#1}}}
177
178 \NewDocumentCommand{\decfig}{m}{\raisebox{-.45\height}{#1}}
179
180
181
182 \DeclareTCBListing[auto counter]{exemple}{ !O{} }{
183 enhanced,
184 title=Example \thetcbcounter,
185 coltitle=black,
186 fonttitle=\bfseries,
187 attach boxed title to top left,
188 boxed title style={left=0pt,
189 boxrule=0pt,
190 colframe=white,
191 colback=white,
192 coltext=black},
193 colback=red!25!yellow!10!white,
194 left=1mm,top=0mm,bottom=0mm,
195 halign lower=center,
196 listing metapost,
197 listing outside text,
198 % listing only,
199 sidebyside gap=1mm,
200 after skip=2em,
201 before upper={\texttt{beginfig(\thetcbcounter);}\par\vspace{-0.5\baselineskip}},
202 after upper={\par\vspace{-0.5\baselineskip}\texttt{endfig;}},
203 listing options={style=mpost
204 },
205 #1}
206
207
208 \DeclareTCBListing{codelatex}{ !O{} }{
209 enhanced,
210 notitle,
211 colback=LightSteelBlue,
212 left=2mm,top=0mm,bottom=0mm,
213 listing only,
214 listing options={language={[LaTeX]TeX}
215 },
216 #1}
217
218 \DeclareTCBListing{codempost}{ !O{} }{
219 enhanced,
220 notitle,
221 colback=red!25!yellow!10!white,
222 left=2mm,top=0mm,bottom=0mm,
223 after skip=1em,
224 listing only,
225 listing options={style=mpost
226 },
227 #1}
228
229
230 \newcommand{\btet}{%
231 \begin{tcolorbox}[fontupper=\ttfamily,
232 nobeforeafter,
233 %tcbox raise base,
234 arc=0pt,
235 outer arc=0pt,
236 width=1.5cm,
237 top=0pt,bottom=0pt,left=0mm,right=0mm,
238 leftrule=0pt,rightrule=0pt,toprule=0.3mm,bottomrule=0.3mm,
239 boxsep=0.5mm,
240 colback=red!10!white,colframe=red!50!black]
241 \scriptsize begintree;\par endtree;
242 \end{tcolorbox}%
243 }
244
245
246 \begin{document}
247 \title{Documentation of \texttt{mptrees.mp}}
248 \date{\today}
249 \author{Olivier \textsc{Péault}%
250 \footnote{E-mail : \href{mailto:[email protected]}{\texttt{[email protected]}}}}
251 \maketitle
252
253 \setcounter{tocdepth}{2}
254
255 \setlength{\columnsep}{25pt}
256 \tableofcontents
257 \setlength{\columnsep}{10pt}
258
259
260 \newcommand{\ijc}{[i][j]}
261
262 \reversemarginpar
263
264
265
266 \part{Trees}
267
268 \section{Overview}
269
270 The first aim of this package is to simplify the drawing of probability trees (or other kind of "tree") with \MP. It provides one main command and several parameters to control the output.
271
272 It can be used in standalone files with two compilations (\verb|latexmp| package is loaded) but also with Lua\LaTeX{} and \verb|luamplib| package.
273
274
275 \begin{rpobjet}{tree[<i>][<j>](<dim1>,<dim2>,...)(<ev1>,<prob1>,<ev2>,<prob2>,...)}{picture}
276 Probability tree located in column \verb|i| and row \verb|j| (see figure below). \verb|dim1|, \verb|dim2|,... can be numerics or pairs and control the dimension of the tree. \verb|ev1|, \verb|prob1|... can be strings or pictures and will be printed (using \verb|latexmp| if strings) at the end of the edge (the event) and above the edge (the probability).
277
278
279 \end{rpobjet}
280
281
282 \begin{center}
283 \begin{mplibcode}
284 draw tree[1][1](3cm,2cm)("$A$","$p$","$\overline{A}$","$q$");
285 draw tree[2][1](3cm,1cm)("$B$","$\nicefrac{1}{4}$","$\overline{B}$","$\nicefrac{3}{4}$");
286 draw tree[2][2](3cm,1cm)("$B$","$0.5$","$\overline{B}$","$0.5$");
287 draw tree[3][2](3cm,1cm)("$C$","$c$","$\overline{C}$","$1-c$");
288 draw tree[4][2](3cm,1cm)("$D$","$a$","$\overline{D}$","$b$");
289 drawoptions(dashed evenly);
290 draw (Orig_arbre[2][1]+(0,0.5cm))--(Orig_arbre[2][2]-(0,0.5cm))--(Orig_arbre[2][2]-(3.5cm,0.5cm))--(Orig_arbre[2][1]+(-3.5cm,0.5cm))--cycle withcolor red;
291 label.bot(textext("\texttt{tree[1][1]}"),0.5[Orig_arbre[2][2]-(0,0.5cm),Orig_arbre[2][2]-(3.5cm,0.5cm)]) withcolor red;
292 draw (Orig_arbre[3][1]+(0,0.5cm))--(Orig_arbre[3][2]-(0,0.5cm))--(Orig_arbre[2][1]-(0,1cm))--(Orig_arbre[2][1]+(0,1cm))--cycle withcolor 0.5green;
293 label.top(textext("\texttt{tree[2][1]}"),0.5[Orig_arbre[3][1]+(0,0.5cm),Orig_arbre[2][1]+(0,1cm)]) withcolor 0.5green;
294 draw (Orig_arbre[3][3]+(0,0.5cm))--(Orig_arbre[3][4]-(0,0.5cm))--(Orig_arbre[2][2]-(0,1cm))--(Orig_arbre[2][2]+(0,1cm))--cycle withcolor 0.5green;
295 label.bot(textext("\texttt{tree[2][2]}"),0.5[Orig_arbre[3][4]-(0,0.5cm),Orig_arbre[2][2]-(0,1cm)]) withcolor 0.5green;
296 draw (Orig_arbre[4][1]+(0,0.5cm))--(Orig_arbre[4][2]-(0,0.5cm))--(Orig_arbre[3][2]-(0,1cm))--(Orig_arbre[3][2]+(0,1cm))--cycle withcolor blue;
297 label.bot(textext("\texttt{tree[3][2]}"),0.5[Orig_arbre[4][2]-(0,0.5cm),Orig_arbre[3][2]-(0,1cm)]) withcolor blue;
298 draw (Orig_arbre[5][1]+(0,0.5cm))--(Orig_arbre[5][2]-(0,0.5cm))--(Orig_arbre[4][2]-(0,1cm))--(Orig_arbre[4][2]+(0,1cm))--cycle withcolor red;
299 label.bot(textext("\texttt{tree[4][2]}"),0.5[Orig_arbre[5][2]-(0,0.5cm),Orig_arbre[4][2]-(0,1cm)]) withcolor red;
300 \end{mplibcode}
301
302 \end{center}
303
304
305 \marginnote[\btet]{}Note that you can use these commands inside any \verb|beginfig();...endfig;| but sometimes, for some constructions, they need to be enclosed between \verb|begintree| and \verb|endtree| commands. Such commands are indicated with a margin note.
306
307 \section{Trees}
308 \subsection{Different kinds of trees}
309
310 \begin{rpobjet}{tree[<i>][<j>](<width>,<vspace>)(<ev1>,<prob1>,<ev2>,<prob2>,...)}{picture}
311 Regular tree where \verb|width| is the horizontal width of the tree and \verb|vspace| the vertical space between two consecutive nodes.
312 \end{rpobjet}
313
314
315
316
317
318 \mplibcodeinherit{enable}
319
320 \begin{mplibcode}
321 string dim[];dim[4]:="4 cm";dim[3]:="3 cm";dim[2]:="2 cm";dim[1]:="1 cm";dim[15]:="1.5 cm";dim[25]:="2.5 cm";dim[11]:="-1 cm";dim[22]:="-2 cm";dim[7]:="7cm";
322 \end{mplibcode}
323
324 \everyendmplib{drawoptions(withcolor red);drawdblarrow ((0,0)--(4cm,0)) shifted (0,2.25cm);label.top(textext(dim[4]),(2cm,2.25cm));drawdblarrow ((0,0)--(0,2.5cm)) shifted (Orig_arbre[2][2]-(5cm,0));label.lft(textext(dim[25]),(-1cm,0));drawoptions(withcolor 0.5green);drawdblarrow ((0,0)--(3cm,0)) shifted (Orig_arbre[2][1]+(0,1cm));label.top(textext(dim[3]),(6cm,2.25cm));drawdblarrow ((0,0)--(0,1.5cm)) shifted (Orig_arbre[3][2]+(0.25cm,0));label.rt(textext(dim[15]),Orig_arbre[3][2]+(0.25cm,0.75cm));drawdblarrow ((0,0)--(0,1cm)) shifted (Orig_arbre[3][4]+(0.25cm,0));label.rt(textext(dim[1]),Orig_arbre[3][4]+(0.25cm,0.5cm));drawdblarrow ((0,0)--(0,1cm)) shifted (Orig_arbre[3][5]+(0.25cm,0));label.rt(textext(dim[1]),Orig_arbre[3][5]+(0.25cm,0.5cm));endfig;}
325
326 \begin{exemple}[listing above text]
327 draw tree[1][1](4cm,2.5cm)("$A_1$","$\nicefrac{1}{3}$","$A_2$","$\nicefrac{2}{3}$");
328 draw tree[2][1](3cm,1.5cm)("$B$","$\nicefrac{1}{4}$","$C$","$\nicefrac{3}{4}$");
329 draw tree[2][2](3cm,1cm)("$D$","$p$","$E$","$q$","$F$","$r$");
330 \end{exemple}
331
332 \begin{rpobjet}{tree[<i>][<j>](<width>,<vsp1>,<vsp2>,...)(<ev1>,<p1>,<ev2>,<p2>,...)}{picture}
333 Tree where \verb|width| is the horizontal width of the tree while each \verb|vsp| indicates the vertical space between the node and the origin of the tree.
334
335 \end{rpobjet}
336
337 \everyendmplib{
338 drawoptions(withcolor 0.5green);
339 draw Orig_arbre[2][1] -- (Orig_arbre[2][1]+(4.5cm,0)) dashed evenly;
340 drawdblarrow ((0,0)--(3cm,0)) shifted (Orig_arbre[2][1]+(0,2.25cm));label.top(textext(dim[3]),(5cm,3.25cm));
341 drawarrow ((0,-2cm)--(0,0)) shifted (Orig_arbre[3][1]+(0.25cm,0));label.rt(textext(dim[2]),Orig_arbre[3][1]+(0.25cm,-0.7cm));
342 drawarrow ((0,-1cm)--(0,0)) shifted (Orig_arbre[3][2]+(0.5cm,0));label.rt(textext(dim[1]),Orig_arbre[3][2]+(0.5cm,-0.5cm));
343 drawarrow ((0,1cm)--(0,0)) shifted (Orig_arbre[3][3]+(0.75cm,0));label.rt(textext(dim[11]),Orig_arbre[3][3]+(0.75cm,0.5cm));
344 drawarrow ((0,0)--(0,-2cm)) shifted (Orig_arbre[3][4]+(0.25cm,0));label.rt(textext(dim[22]),Orig_arbre[3][4]+(0.25cm,-1cm));
345 endfig;}
346
347 \begin{exemple}[listing above text]
348 draw tree[1][1](3cm,2cm)("$A$","$p$","$\overline{A}$","$q$");
349 draw tree[2][1](3cm,2cm,1cm,-1cm)("$B$","$p$","$C$","$q$","$D$","$r$");
350 draw tree[2][2](3cm,0cm,-2cm)("$E$","$0.5$","$F$","$0.5$");
351 \end{exemple}
352
353
354 \begin{rpobjet}{tree[<i>][<j>](<pair1>,<pair2>,...)(<ev1>,<prob1>,<ev2>,<prob2>,...)}{picture}
355 Tree where \verb|pair1|, \verb|pair2|... indicate the coordinates of each node from the origin of the tree.
356 \end{rpobjet}
357
358 \everyendmplib{
359 drawoptions(withcolor 0.5green);
360 draw Orig_arbre[2][1] -- (Orig_arbre[2][1]+(5cm,0)) dashed evenly;
361 drawarrow ((0,0)--(3cm,0)) shifted (Orig_arbre[2][1]+(0,2.25cm));label.top(textext(dim[3]),(5cm,3.25cm));
362 drawarrow ((0,-2cm)--(0,0)) shifted (Orig_arbre[3][1]+(0.25cm,0));label.rt(textext(dim[2]),Orig_arbre[3][1]+(0.25cm,-0.7cm));
363 drawarrow ((0,0)--(4cm,0)) shifted (Orig_arbre[2][1]+(0,-1.25cm));label.bot(textext(dim[4]),(5.5cm,-0.25cm));
364 drawarrow ((0,0)--(0,-1cm)) shifted (Orig_arbre[3][2]+(0,1cm));label.rt(textext(dim[11]),Orig_arbre[3][2]+(0,0.5cm));
365 endfig;}
366
367 \begin{exemple}[listing above text]
368 draw tree[1][1](3cm,2cm)("$A$","$p$","$\overline{A}$","$1-p$");
369 draw tree[2][1]((3cm,2cm),(4cm,-1cm))("$B$","$q$","$C$","$r$");
370 \end{exemple}
371
372 \everyendmplib{endfig;}
373
374
375 \subsection{Simple trees}
376 \begin{rpobjet}{stree[<i>][<j>](...)(<ev1>,<ev2>)}{picture}
377 Same as previous except that there are no probabilities.
378 \end{rpobjet}
379
380
381 \begin{exemple}[listing above text]
382 draw stree[1][1](100,50)("$A$","$B$","$C$");
383 draw stree[2][1](80,25)("$A$","$B$");
384 draw stree[2][2](80,25)("$A$","$B$");
385 draw stree[2][3](80,25)("$A$","$B$");
386 \end{exemple}
387
388 \subsection{Start and end labels}
389 \begin{rpobjet}{startlabel(<s>)}{picture}
390 Prints \verb|s| (can be a string or a picture) at the origin of the tree.
391 \end{rpobjet}
392
393
394 \begin{exemple}[lefthand ratio = 0.65]
395 draw startlabel("$S$");
396 draw tree[1][1](3cm,2cm)("$A$","$p$","$B$","$q$");
397 \end{exemple}
398
399 \begin{rpobjet}{endlabel[<i>][<j>](<s>)}{picture}
400 Prints \verb|s| at the end of a branch. The space between the previous label ans \verb|s| is controlled by the numeric \verb|endlabelspace| which defaults to \verb|1cm|.
401 \end{rpobjet}
402
403 \begin{exemple}[listing above text]
404 draw startlabel("$S$");
405 draw tree[1][1](3cm,2cm)("$A$","$p$","$B$","$q$");
406 draw tree[2][2](2cm,1cm)("$A$","$p$","$B$","$q$");
407 draw endlabel[2][1]("$SA$");
408 draw endlabel[3][1]("$SBA$");
409 draw endlabel[3][2]("$SBB$");
410 \end{exemple}
411
412 \section{Direction}
413
414 \begin{mptparam}{dirtree}{numeric}{0}
415 All trees are construct horizontally by default. \verb|ditree| indicates the angle in degrees between the horizontal and the main direction of the tree.
416 \end{mptparam}
417
418 \begin{exemple}[listing above text]
419 dirtree:=135;
420 draw tree[1][1](3cm,2cm)("$A_1$","$a_1$","$A_2$","$a_2$");
421 draw tree[2][1](3cm,1cm)("$B$","$b$","$C$","$c$");
422 draw tree[2][2](3cm,1cm)("$D$","$p$","$E$","$q$");
423 \end{exemple}
424
425
426 \begin{exemple}[listing above text]
427 dirtree:=-60;
428 draw tree[1][1](3cm,2cm)("$A_1$","$a_1$","$A_2$","$a_2$");
429 draw tree[2][1](3cm,1cm)("$B$","$b$","$C$","$c$");
430 draw tree[2][2](3cm,1cm)("$D$","$p$","$E$","$q$");
431 \end{exemple}
432
433
434 \begin{mptparam}{dirlabel}{numeric}{0}
435 All the trees are viewed as ``horizontal" trees, so the space between two subtrees is horizontal too. With \verb|dirtree|, the whole (horizontal) tree is rotated. But if the tree is designed vertically, spacing is wrong. In this case, one can use \verb|dirlabel| to indicate the orientation of the tree.
436 \end{mptparam}
437
438
439 \begin{exemple}[lefthand ratio=0.65]
440 draw tree[1][1]((-1cm,2cm),(1cm,2cm))
441 ("$A$","$p$","$B$","$q$");
442 draw tree[2][1]((-0.5cm,2cm),(0.5cm,2cm))
443 ("$C$","$c$","$D$","$d$");
444 draw tree[2][2]((-0.5cm,2cm),(0.5cm,2cm))
445 ("$E$","$e$","$F$","$f$");
446 \end{exemple}
447
448 \begin{exemple}[lefthand ratio=0.65]
449 dirlabel:=90;
450 draw tree[1][1]((-1cm,2cm),(1cm,2cm))
451 ("$A$","$p$","$B$","$q$");
452 draw tree[2][1]((-0.5cm,2cm),(0.5cm,2cm))
453 ("$C$","$c$","$D$","$d$");
454 draw tree[2][2]((-0.5cm,2cm),(0.5cm,2cm))
455 ("$E$","$e$","$F$","$f$");
456 \end{exemple}
457
458 \section{Dealing with alignment}
459
460 \begin{mptparam}{shiftev}{numeric}{-1}
461 The origin of each tree is located at the right side of the bounding box of the previous event name. Thus different subtrees may begin at different places. The numeric \verb|shiftev|, if positive, indicates the fixed horizontal space between the end of the edges and the beginning of following subtrees.
462
463 It can be used inside the first set of parameters of the tree (see example below) or as a global variable.
464 \end{mptparam}
465
466 \begin{exemple}[listing above text]
467 draw tree[1][1](80,120)("$A$","$0.5$","$\overline{A}$","$0.5$");
468 draw tree[2][1](70,40)("Yes","$p$","No","$q$","Maybe","$r$");
469 draw tree[2][2](70,40,"shiftev:=1.5cm")("Yes","$p$","No","$q$","Maybe","$r$");
470 draw tree[3][1](50,20)("$B$","$b$","$C$","$c$");
471 draw tree[3][2](50,20)("$B$","$b$","$C$","$c$");
472 draw tree[3][3](50,20)("$B$","$b$","$C$","$c$");
473 draw tree[3][4](50,20)("$B$","$b$","$C$","$c$");
474 draw tree[3][5](50,20)("$B$","$b$","$C$","$c$");
475 draw tree[3][6](50,20)("$B$","$b$","$C$","$c$");
476 \end{exemple}
477
478 \begin{mptparam}{abscoord}{boolean}{false}
479 With the boolean \verb|abscoord| set to \verb|true|, all the coordinates are given from the origin of the \emph{first} tree instead of the origin of the subtree, which makes easier the alignment of all the subtrees.
480 \end{mptparam}
481
482
483 \everyendmplib{drawoptions(withcolor red);
484 drawdblarrow ((0,0)--(7cm,0)) shifted (0,2cm);label.top(textext(dim[7]),(3.5cm,2cm));
485 drawdblarrow ((0,0)--(0,1.5cm)) shifted (-0.5cm,0);label.lft(textext("1.5 cm"),(-0.5cm,0.75cm));
486 endfig;}
487
488 \begin{exemple}[listing above text]
489 abscoord:=true;
490 draw tree[1][1](3cm,2cm)("$A$","$p$","Blabla","$q$");
491 draw tree[2][1]((7cm,1.5cm),(7cm,0.5cm))("$A$","$p$","$B$","$q$");
492 draw tree[2][2]((7cm,-0.5cm),(7cm,-1.5cm))("$A$","$p$","$B$","$q$");
493 \end{exemple}
494
495 \everyendmplib{endfig;}
496
497 \section{Parameters}
498
499 All following parameters can be changed globally before drawing the tree or changed locally inside the first set of parameters:
500
501 \begin{codempost}[width=0.7\linewidth,left skip=1cm]
502 scaleev:=2;
503 draw tree[1][1](3cm,2cm)(...);
504 draw tree[2][1](3cm,2cm)(...);
505 \end{codempost}
506
507 or
508
509 \begin{codempost}[width=0.7\linewidth,left skip=1cm]
510 draw tree[1][1](3cm,2cm,"scaleev:=2")(...);
511 draw tree[2][1](3cm,2cm)(...);
512 \end{codempost}
513
514 In the fisrt case, \verb|scaleev| is changed globally while in the second case, the change only applies to the first tree.
515
516
517 \subsection{Event}
518
519 \begin{mptparam}{scaleev}{numeric}{1}
520 Numeric controlling the scale of the label at the end of the edge (the event).
521 \end{mptparam}
522
523
524 \begin{exemple}[lefthand ratio=0.65]
525 scaleev:=2;
526 draw stree[1][1](3cm,2cm)("$A$","$B$");
527 \end{exemple}
528
529 \begin{mptparam}{nodeformat}{string}{""}
530 String that indicates how the events are printed (the shape of path around the event). Possible values are (for now) \verb|"bbox"|, \verb|"circle"|, \verb|"superellipse"|.
531
532 See below for personalised values.
533 \end{mptparam}
534
535 \begin{exemple}[lefthand ratio=0.65]
536 nodeformat:="bbox";
537 draw stree[1][1](3cm,2cm)("$A$","$B$");
538 \end{exemple}
539
540 \begin{mptparam}{nodelinecolor}{color}{black}
541 Color of the path around the node
542 \end{mptparam}
543
544 \begin{mptparam}{nodebgcolor}{color}{white}
545 Color of the background of the region delimited by the previous path.
546 \end{mptparam}
547
548 \begin{mptparam}{nodefgcolor}{color}{black}
549 Color of the text.
550 \end{mptparam}
551
552
553
554 \begin{exemple}[lefthand ratio=0.65]
555 nodeformat:="superellipse";
556 nodelinecolor:=(0.8,0,0);
557 nodebgcolor:=(1,0.5,0.5);
558 nodefgcolor:=white;
559 draw stree[1][1](3cm,2cm)("$A$","$B$");
560 \end{exemple}
561
562
563 \begin{exemple}[lefthand ratio=0.65]
564 nodefgcolor:=(0.7,0.4,0.7);
565 draw stree[1][1](3cm,2cm)("$A$","$B$");
566 \end{exemple}
567
568 \begin{rpobjet}{nodeshape(expr A,B)}{cycle path}
569 It is also possible to redefine the path defining the node. Simply rewrite the \verb|nodeshape| macro, which must take a picture as parameter and whose result must be a cycle path.
570
571 The parameter passed to the macro is the figure representing the drawing of the event (normally the name of the event). You may use it, or not, in the macro definition.
572 \end{rpobjet}
573
574 \begin{exemple}[lefthand ratio=0.6]
575 vardef nodeshape(expr p)=
576 fullcircle xscaled 1cm yscaled 0.6cm
577 enddef;
578
579 draw stree[1][1](3cm,3cm)("$A$","$B$");
580 \end{exemple}
581
582
583 \begin{exemple}[lefthand ratio=0.7]
584 vardef nodeshape(expr p)=
585 llcorner p..ulcorner p..urcorner p..lrcorner p--cycle
586 enddef;
587
588 nodebgcolor:=0.7white;
589 draw stree[1][1](3cm,3cm)("$A$","$B$");
590 \end{exemple}
591
592
593
594 \subsection{Leaves}
595 \marginnote[\btet]{}
596 You may want to format the leaves in a different way from the nodes. A tree using the following parameters must be enclosed in a \verb|begintree;|...\verb|endtree;| ``environment".
597
598 \begin{mptparam}{leaveformat}{string}{""}
599 String that indicates how the events are printed (the shape of path around the event). Possible values are (for now) \verb|"bbox"|, \verb|"circle"|, \verb|"superellipse"| and \verb|"none"|.
600 \end{mptparam}
601
602 \begin{exemple}[listing above text]
603 begintree;
604 leaveformat:="bbox";
605 draw stree[1][1](100,45)("$A$","$B$");
606 draw stree[2][1](80,30)("$C$","$D$");
607 draw stree[3][2](65,20)("$E$","$F$");
608 endtree;
609 \end{exemple}
610
611 \begin{mptparam}{leavelinecolor}{color}{black}
612 Color of the path around the leave
613 \end{mptparam}
614
615 \begin{mptparam}{leavebgcolor}{color}{white}
616 Color of the background of the region delimited by the previous path.
617 \end{mptparam}
618
619 \begin{mptparam}{leavefgcolor}{color}{black}
620 Color of the text.
621 \end{mptparam}
622
623 \begin{exemple}[listing above text]
624 begintree;
625 nodeformat:="circle";
626 nodelinecolor:=(0.8,0,0); nodebgcolor:=(1,0.5,0.5); nodefgcolor:=white;
627 leaveformat:="bbox";
628 leavebgcolor:=(0.3,1,1); leavefgcolor:=red;
629 draw stree[1][1](100,45)("$A$","$B$");
630 draw stree[2][1](80,30)("$C$","$D$");
631 draw stree[3][2](65,20)("$E$","$F$");
632 endtree;
633 \end{exemple}
634
635 Note that \verb|nodeformat| applies to both nodes and leaves. To avoid formatting the leaves, use the value \verb|"none"| for \verb|leaveformat|.
636
637 \begin{exemple}[listing above text]
638 begintree;
639 nodeformat:="circle";
640 nodelinecolor:=(0.8,0,0); nodebgcolor:=(1,0.5,0.5); nodefgcolor:=white;
641 leaveformat:="none";
642 draw stree[1][1](100,45)("$A$","$B$");
643 draw stree[2][1](80,30)("$C$","$D$");
644 draw stree[3][2](65,20)("$E$","$F$");
645 endtree;
646 \end{exemple}
647
648 \subsection{Probability}
649
650 \begin{mptparam}{probformat}{string}{""}
651 String that indicates how the probabilities are printed (the shape of path around the probability). Possible values are (for now) \verb|"bbox"|, \verb|"circle"|, \verb|"superellipse"|.
652 \end{mptparam}
653
654 \begin{exemple}[lefthand ratio=0.65]
655 probformat:="bbox";
656 draw tree[1][1](3cm,2cm)("$A$","$p$","$B$","$q$");
657 \end{exemple}
658
659 \begin{mptparam}{problinecolor}{color}{black}
660 Color of the path around the probability
661 \end{mptparam}
662
663 \begin{mptparam}{probbgcolor}{color}{white}
664 Color of the background of the region delimited by the previous path.
665 \end{mptparam}
666
667 \begin{mptparam}{probfgcolor}{color}{black}
668 Color of the text.
669 \end{mptparam}
670
671
672 \begin{exemple}[lefthand ratio=0.65]
673 probformat:="circle";
674 problinecolor:=(0.8,0,0);
675 probbgcolor:=(1,0.5,0.5);
676 probfgcolor:=white;
677 draw tree[1][1](3cm,2cm)("$A$","$p$","$B$","$q$");
678 \end{exemple}
679
680 \begin{exemple}[lefthand ratio=0.65]
681 probfgcolor:=blue;
682 draw tree[1][1](3cm,2cm)("$A$","$p$","$B$","$q$");
683 \end{exemple}
684
685
686 \begin{mptparam}{scaleprob}{numeric}{0.85}
687 Numeric controlling the scale of the label above the edge (the probability).
688 \end{mptparam}
689
690 \begin{exemple}[lefthand ratio=0.65]
691 scaleprob:=1.5;
692 draw tree[1][1](3cm,2cm)("$A$","$p$","$B$","$q$");
693 \end{exemple}
694
695
696 \begin{mptparam}{posprob}{numeric}{0.6}
697 Numeric controlling the position of the label above the edge.
698 \end{mptparam}
699
700 \begin{exemple}[lefthand ratio=0.65]
701 posprob:=0.8;
702 draw tree[1][1](3cm,2cm)("$A$","$p$","$B$","$q$");
703 \end{exemple}
704
705
706 \begin{mptparam}{typeprob}{numeric}{1}
707 Numeric controlling how the label is printed. Values can be 1 (the label is printed above the edge), 2 (the label is printed on the edge), 3 (the label is printed above the edge and rotated) or 4 (the label is printed on the edge and rotated).
708 \end{mptparam}
709
710 \begin{exemple}[lefthand ratio=0.7]
711 typeprob:=2;
712 draw tree[1][1](3cm,2cm)("$A$","$p$","$B$","$1-p$");
713 \end{exemple}
714
715 \begin{exemple}[lefthand ratio=0.7]
716 typeprob:=3;
717 draw tree[1][1](3cm,2cm)("$A$","$p$","$B$","$1-p$");
718 \end{exemple}
719
720 \begin{exemple}[lefthand ratio=0.7]
721 typeprob:=4;
722 draw tree[1][1](3cm,2cm)("$A$","$p$","$B$","$1-p$");
723 \end{exemple}
724
725
726
727 \begin{mptparam}{proboffset}{numeric}{3bp}
728 Numeric controlling the amount by which the label above the edge is offset.
729 \end{mptparam}
730
731 \begin{exemple}[lefthand ratio=0.75]
732 draw tree[1][1](3cm,3cm)("$A$","$p+q+r$","$B$","$s$");
733 \end{exemple}
734
735
736
737 \begin{exemple}[lefthand ratio=0.75]
738 proboffset:=6bp;
739 draw tree[1][1](3cm,3cm)("$A$","$p+q+r$","$B$","$s$");
740 \end{exemple}
741
742
743 \subsection{Edge}
744 \begin{mptparam}{linewidth}{numeric}{0.5bp}
745 Width of the lines.
746 \end{mptparam}
747
748 \begin{mptparam}{linecolor}{color}{black}
749 Color of the lines.
750 \end{mptparam}
751
752 \begin{exemple}[lefthand ratio=0.65]
753 linewidth:=1.5;
754 linecolor:=blue;
755 draw tree[1][1](3cm,2cm)("$A$","$p$","$B$","$q$");
756 \end{exemple}
757
758 \begin{mptparam}{endedgeshift}{numeric}{0}
759 Vertical space added at the end of the edge. Useful when various edges end at the same point.
760 \end{mptparam}
761
762 \begin{exemple}[lefthand ratio=0.65]
763 draw startlabel("$S$");
764 draw tree[1][1]((3cm,-1cm))("$A$","$p$");
765 \end{exemple}
766
767
768 \begin{exemple}[lefthand ratio=0.65]
769 endedgeshift:=10;
770 draw startlabel("$S$");
771 draw tree[1][1]((3cm,-1cm))("$A$","$p$");
772 \end{exemple}
773
774 \begin{mptparam}{edgearrow}{boolean}{false}
775 When the boolean \verb|edgearrow| is set to true, edges end with an arrow.
776 \end{mptparam}
777
778 \begin{exemple}[lefthand ratio=0.65]
779 edgearrow:=true;
780 draw tree[1][1](3cm,2cm)("$A$","$p$","$B$","$q$");
781 \end{exemple}
782
783 \begin{mptparam}{branchtype}{string}{"segment"}
784 String which indicates the shape of the edge. Possible values are \verb|segment|, \verb|curve|, \verb|broken|.
785
786 Note that double quotes have to be replaced by single quotes when this parameter is changed locally inside the \verb|tree| macro.
787
788
789 See below for personalised values.
790
791 \end{mptparam}
792
793
794 \begin{mptparam}{tenscurve}{numeric}{0}
795 If string \verb|branchtype| is set to \verb|curve|, \verb|tenscurve| indicates the ``tension". When sets to \verb|1|, the curve is a segment.
796 \end{mptparam}
797
798 \begin{exemple}[lefthand ratio=0.6]
799 branchtype:="curve";
800 draw stree[1][1](3cm,3cm)("$A$","$B$");
801 \end{exemple}
802
803
804 \begin{exemple}[lefthand ratio=0.6]
805 draw stree[1][1](3cm,3cm,"branchtype:='curve'")
806 ("$A$","$B$");
807 \end{exemple}
808
809
810
811 \begin{exemple}[lefthand ratio=0.6]
812 branchtype:="curve";
813 tenscurve:=0.5;
814 draw stree[1][1](3cm,3cm)("$A$","$B$");
815 \end{exemple}
816
817
818 \begin{mptparam}{brokenlineratio}{numeric}{0.2}
819 If string \verb|branchtype| is set to \verb|broken|, \verb|brokenlineratio| indicates the ratio between the length of the first segment of the broken line and the total length of the horizontal space.
820 \end{mptparam}
821
822 \begin{exemple}[lefthand ratio=0.6]
823 branchtype:="broken";
824 draw stree[1][1](3cm,3cm)("$A$","$B$");
825 \end{exemple}
826
827 \begin{exemple}[lefthand ratio=0.6]
828 branchtype:="broken";
829 posprob:=0.8;
830 brokenlineratio:=0.5;
831 draw stree[1][1](3cm,3cm)("$A$","$B$");
832 \end{exemple}
833
834
835 \begin{exemple}[lefthand ratio=0.6]
836 branchtype:="broken";
837 posprob:=0.8;
838 brokenlineratio:=0.75;
839 draw stree[1][1](3cm,3cm)("$A$","$B$");
840 \end{exemple}
841
842 \begin{rpobjet}{edgeshape(expr A,B)}{path}
843 It is possible to completely redefine the path defining the edge. Simply rewrite the \verb|edgeshape| macro, which must take two pairs as parameters and whose result must be a path.
844
845 The first parameter represents the start point and the second the end point.
846
847 \end{rpobjet}
848
849 \begin{exemple}[lefthand ratio=0.6]
850 vardef edgeshape(expr S,E)=
851 save a;numeric a;a=angle(E-S);
852 S--((0.25cm,0) rotated (a+30) shifted 0.5[S,E])
853 --((0.25cm,0) rotated (a-150) shifted 0.5[S,E])
854 --E
855 enddef;
856
857 draw stree[1][1](3cm,3cm)("$A$","$B$");
858 \end{exemple}
859
860
861 \section{Regular trees}
862 \subsection{Ordinary regular trees}
863
864 \begin{rpobjet}{regulartree(<n>)(<l>,<h>)(<ev1>,<prob1>,<ev2>,<prob2>,...)}{picture}
865 Tree describing the repetition of $n$ identical and independent random experiments. \verb|l| is the horizontal length of the first edges and \verb|h| is the vertical space between two leaves.
866 \end{rpobjet}
867
868 \begin{mptparam}{scalebranch}{numeric}{0.8}
869 Ratio between edges width of consecutive level.
870 \end{mptparam}
871
872 \begin{exemple}[lefthand ratio=0.6]
873 draw regulartree(2)(3cm,0.7cm)
874 ("$A$","$p$","$B$","$q$","$C$","$r$");
875 \end{exemple}
876
877 Note that you can change variable values inside the first set of parameters.
878
879 \begin{exemple}[lefthand ratio=0.6]
880 draw regulartree(2)(3cm,0.7cm,"typeprob:=2")
881 ("$A$","$p$","$B$","$q$","$C$","$r$");
882 \end{exemple}
883
884
885 \subsection{Binomial trees}
886
887 \begin{rpobjet}{bernoulliprocess(<n>)(<l>,<h>)(<ev1>,<prob1>,<ev2>,<prob2>)}{picture}
888 Tree describing the Bernoulli process with $n$ trials. \verb|l| is the horizontal length of the first edges and \verb|h| is the vertical space between two final nodes. If the last set of parameters is omitted, the values are set according to the following parameters.
889 \end{rpobjet}
890
891
892 \begin{rpobjet}{bernoulliprocessL(<n>)(<L>,<H>)(<ev1>,<prob1>,<ev2>,<prob2>)}{picture}
893 Same as above where \verb|L| is the whole width of the tree and \verb|H| its height.
894 \end{rpobjet}
895
896
897
898 Several parameters control the output:
899
900 \begin{mptparam}{bernoullisuccessevent}{string}{"$S$"}
901 String printed at every node representing a success.
902 \end{mptparam}
903
904 \begin{mptparam}{bernoullifailureevent}{string}{"$\overline{S}$"}
905 String printed at every node representing a failure.
906 \end{mptparam}
907
908 \begin{mptparam}{bernoullisuccessprob}{string}{"$p$"}
909 String printed above every edge representing a success.
910 \end{mptparam}
911
912 \begin{mptparam}{bernoullifailureprob}{string}{"$q$"}
913 String printed above every edge representing a failure.
914 \end{mptparam}
915
916 \begin{mptparam}{bernoulliscalebranch}{numeric}{0.8}
917 Ratio between width of consecutive edges.
918 \end{mptparam}
919
920
921
922
923
924 \begin{exemple}[lefthand ratio=0.45]
925 draw bernoulliprocess(3)(3cm,0.7cm)();
926 \end{exemple}
927
928
929 \begin{exemple}[lefthand ratio=0.45]
930 draw bernoulliprocess(3)
931 (3cm,0.7cm,"typeprob:=2;")
932 ("$A$","$0.7$","$B$","$0.3$");
933 \end{exemple}
934
935 \begin{exemple}[lefthand ratio=0.45]
936 typeprob:=4;
937 bernoullisuccessevent:="$A$";
938 bernoullifailureevent:="$B$";
939 bernoullisuccessprob:="$0.7$";
940 bernoullifailureprob:="$0.3$";
941 draw bernoulliprocess(3)(3cm,0.7cm)();
942 \end{exemple}
943
944
945 \begin{rpobjet}{binomialtree(<n>)(<l>,<h>)}{picture}
946 Tree describing the binomial distribution with $n$ trials. \verb|l| is the length of the first edges and \verb|h| is the space between two final nodes. It uses \verb|bernoullisuccesprob| and \verb|bernoullifailureprob| but \verb|bernoulliscalebranch| is set to \verb|1|.
947 \end{rpobjet}
948
949
950 \begin{rpobjet}{binomialtreeL(<n>)(<L>,<H>)}{picture}
951 Same as above where \verb|L| is the whole width of the tree and \verb|H| its height.
952 \end{rpobjet}
953
954
955
956
957 \begin{exemple}[listing above text]
958 draw binomialtree(4)(3cm,1.5cm);
959 \end{exemple}
960
961
962
963 \section{``Calculated'' trees}
964 \marginnote[\btet]{}The following commands are experimental and need to be enclosed in a \verb|begintree;|...\verb|endtree;| ``environment".
965
966 \begin{rpobjet}{tree[<i>][<j>]()(<ev1>,<prob1>,<ev2>,<prob2>,...)}{picture}
967 When the first set of parameters is left empty, the dimensions of the tree are calculated. The calculations use the parameters described below.
968 \end{rpobjet}
969
970
971 \begin{rpobjet}{stree[<i>][<j>]()(<ev1>,<ev2>,...)}{picture}
972 Same as above for ``simple'' trees.
973 \end{rpobjet}
974
975
976 \begin{exemple}[listing above text]
977 begintree;
978 draw startlabel("$S$");
979 draw stree[1][1]()("$A$","$B$","$C$");
980 draw stree[2][1]()("$D$","$E$","$F$");
981 draw stree[2][3]()("$D$","$E$");
982 draw stree[3][2]()("$G$","$H$");
983 draw stree[3][4]()("$G$","$H$");
984 endtree;
985 \end{exemple}
986
987 \begin{mptparam}{widthbranch}{numeric}{3.5cm}
988 Horizontal width of the first level tree.
989 \end{mptparam}
990
991 \begin{mptparam}{gapnode}{numeric}{0.7cm}
992 Minimal vertical space between two nodes of the last level trees.
993 \end{mptparam}
994
995 \begin{mptparam}{scalebranch}{numeric}{0.8}
996 Ratio between edges width of consecutive level.
997 \end{mptparam}
998
999
1000 \begin{exemple}[listing above text]
1001 begintree;
1002 widthbranch:=4cm;
1003 scalebranch:=1;
1004 gapnode:=1cm;
1005 draw startlabel("$S$");
1006 draw stree[1][1]()("$A$","$B$","$C$");
1007 draw stree[2][1]()("$D$","$E$","$F$");
1008 draw stree[2][3]()("$D$","$E$");
1009 draw stree[3][2]()("$G$","$H$");
1010 draw stree[3][4]()("$G$","$H$");
1011 endtree;
1012 \end{exemple}
1013
1014 \section{Examples}
1015
1016 \begin{exemple}[listing above text]
1017 u:=0.4cm;
1018 branchtype:="curve";
1019 dirlabel:=90;
1020 abscoord:=true;
1021 endlabelspace:=0.5cm;
1022 draw startlabel("S");
1023 draw stree[1][1]((-5.5u,4u),(5.5u,8u))("NP","VP");
1024 draw stree[2][1]((-8.5u,12u),(-2.5u,8u))("A","NP");
1025 draw stree[2][2]((3.5u,12u),(7.5u,12u))("V","Adv");
1026 draw stree[3][2]((-4.5u,12u),(-0.5u,12u))("A","N");
1027 draw endlabel[3][1]("Colorless");
1028 draw endlabel[4][1]("green");
1029 draw endlabel[4][2]("ideas");
1030 draw endlabel[3][3]("sleep");
1031 draw endlabel[3][4]("furiously");
1032 \end{exemple}
1033
1034
1035
1036 \begin{exemple}[listing above text]
1037 u:=1cm;
1038 branchtype:="broken";
1039 dirlabel:=-90;
1040 abscoord:=true;
1041 scaleev:=2;
1042 label.top(textext("\Large Tree diagram of $(2x+1)(x-3)$"),(0,1cm));
1043 draw startlabel("$\times$");
1044 draw stree[1][1]((-2u,-1.5u),(2u,-1.5u))("$+$","$-$");
1045 draw stree[2][1]((-3u,-3.5u),(-1u,-3.5u))("$\times$","$1$");
1046 draw stree[2][2]((1u,-3.5u),(3u,-3.5u))("$x$","$3$");
1047 draw stree[3][1]((-4u,-5.5u),(-2u,-5.5u))("$2$","$x$");
1048 \end{exemple}
1049
1050
1051 \begin{exemple}[listing above text]
1052 posprob:=0.5;
1053 typeprob:=3;
1054 shiftev:=1.5cm;
1055 edgearrow:=true;
1056 u:=0.2cm;
1057 vardef paral = ((2,-2)--(6,2)--(0,2)--(-4,-2)--cycle) scaled u enddef;
1058 vardef rhombus = ((3,0)--(0,6)--(-3,0)--(0,-6)--cycle) scaled u enddef;
1059 vardef rectangle = ((3,5)--(-3,5)--(-3,-5)--(3,-5)--cycle) scaled u enddef;
1060 vardef square = ((3,3)--(-3,3)--(-3,-3)--(3,-3)--cycle) scaled u enddef;
1061 draw startlabel(paral);
1062 draw tree[1][1](5cm,4cm)(rhombus,"Diagonals perpendicular",%
1063 rectangle,"Diagonals of equal length");
1064 endedgeshift:=5;
1065 draw tree[2][1]((5cm,-2cm))("","Diagonals of equal length");
1066 draw tree[2][2]((5cm,2cm))(square,"Diagonals perpendicular");
1067 \end{exemple}
1068
1069 \begin{exemple}[listing above text]
1070 dirtree:=-90;
1071 branchtype:="curve"; tenscurve:=0.75;
1072 linewidth:=1; linecolor:=(0.2,0.2,0.7);
1073 widthbranch:=1cm; scalebranch:=0.9;
1074 gapnode:=1cm;
1075 leaveformat:="bbox";
1076 nodeformat:="superellipse"; nodebgcolor:=(0.6,0.6,1);
1077 begintree;
1078 label.top(textext("\Large Huffman tree (source Wikipedia)"),(0,1cm));
1079 draw startlabel("36");
1080 draw stree[1][1]()("20","16");
1081 draw stree[2][1]()("12","8");
1082 draw stree[2][2]()("8","8");
1083 draw stree[3][1]()("' '|7","5");
1084 draw stree[3][2]()("4","4");
1085 draw stree[3][3]()("4","'a'|4");
1086 draw stree[3][4]()("4","'e',4");
1087 draw stree[4][2]()("'f'|3","2");
1088 draw stree[4][3]()("'s'|2","'h'|2");
1089 draw stree[4][4]()("2","'i'|2");
1090 draw stree[4][5]()("'m'|2","'t'|2");
1091 draw stree[4][7]()("2","'n'|2");
1092 draw stree[5][2]()("'l'|1","'r'|1");
1093 draw stree[5][5]()("'p'|1","'x'|1");
1094 draw stree[5][9]()("'u'|1","'o'|1");
1095 endtree;
1096 \end{exemple}
1097
1098 \part{Graphs}
1099 \section{Overview}
1100 This package can also be used to draw graphs. In this case, you need to define the nodes by their coordinates and indicate the edges to be drawn. Various commands allow you to draw graphs quickly, but it is also possible to finely control the drawing of each node and edge.
1101
1102 \section{Nodes}
1103 \subsection{Definition}
1104
1105 \begin{rpdeclaration}{defnodes(<pair1>,<pair2>,...)}
1106 The easiest way to define the nodes : just put a list of pairs. This command does not draw anything, it just defines an array of pairs corresponding to the coordinates. By default the array name is \verb|A[]|.
1107 \end{rpdeclaration}
1108
1109 \begin{mptparam}{defnodename}{string}{"A"}
1110 Default node array name.
1111 \end{mptparam}
1112
1113 \begin{mptparam}{nodename}{string}{"array"}
1114 String indicating how the nodes are stored and labels are drawns:
1115 \begin{itemize}[nosep]
1116 \item with \verb|nodename="array"|, nodes are stored in \verb|A[]| and labelled \verb|A1|, \verb|A2|,...
1117 \item with \verb|nodename="Alph"|, nodes are stored and labelled \verb|A|, \verb|B|,...
1118 \item with \verb|nodename="alph"|, nodes are stored and labelled \verb|a|, \verb|b|,...
1119 \item with \verb|nodename="arabic"|, nodes are stored in \verb|A[]|,... but are labelled $1$, $2$,...
1120 \end{itemize}
1121 \end{mptparam}
1122
1123 \begin{rpdeclaration}{defnode(<token>,<pair>)}
1124 Nodes can be defined one by one. \verb|<token>| is the node's name and \verb|<pair>| its coordinates.
1125 \end{rpdeclaration}
1126
1127 \subsection{Drawing}
1128
1129 \begin{rplabel}{drawnodes.<pos>(<nodei>,<nodej>,...)}
1130 Draw all the indicated nodes. These nodes can be designated either by name or by rank. If the list is empty, all the nodes are drawn. \verb|pos| can be empty or \verb|lft|, \verb|rt|, etc. or an angle.
1131 \end{rplabel}
1132
1133 \begin{exemple}[lefthand ratio = 0.6]
1134 defnodes((0cm,0cm),(2cm,0),(4cm,0));
1135 drawnodes(1,3);
1136 \end{exemple}
1137
1138 \begin{exemple}[lefthand ratio = 0.6]
1139 defnodes((0cm,0cm),(2cm,0),(4cm,0));
1140 drawnodes(A1,A3);
1141 \end{exemple}
1142
1143 \begin{exemple}[lefthand ratio = 0.6]
1144 defnodes((0cm,0cm),(2cm,0),(4cm,0));
1145 drawnodes();
1146 \end{exemple}
1147
1148
1149 \begin{exemple}[lefthand ratio = 0.6]
1150 defnodename:="N";
1151 defnodes((0cm,0cm),(2cm,0),(4cm,0));
1152 drawnodes();
1153 \end{exemple}
1154
1155
1156 \begin{exemple}[lefthand ratio = 0.6]
1157 nodename:="Alph";
1158 defnodes((0cm,0cm),(2cm,0),(4cm,0));
1159 drawnodes();
1160 \end{exemple}
1161
1162 \begin{exemple}[lefthand ratio = 0.6]
1163 nodename:="alph";
1164 defnodes((0cm,0cm),(2cm,0),(4cm,0));
1165 drawnodes();
1166 \end{exemple}
1167
1168 \begin{exemple}[lefthand ratio = 0.6]
1169 nodename:="arabic";
1170 defnodes((0cm,0cm),(2cm,0),(4cm,0));
1171 drawnodes();
1172 \end{exemple}
1173
1174 \begin{mptparam}{printnodename}{boolean}{true}
1175 When set to false, node names are not printed.
1176 \end{mptparam}
1177
1178 \begin{exemple}[lefthand ratio = 0.6]
1179 printnodename:=false;
1180 defnodes((0cm,0cm),(2cm,0),(4cm,0));
1181 drawnodes();
1182 \end{exemple}
1183
1184
1185 \begin{mptparam}{nodeformat}{string}{""}
1186 String that indicates how the nodes are printed (the shape of path around the node). Possible values are (for now) \verb|"bbox"|, \verb|"circle"|, \verb|"superellipse"| and \verb|"square"|.
1187
1188 See below for personalised values.
1189 \end{mptparam}
1190
1191
1192 \begin{mptparam}{nodewidth}{numeric}{0.6cm}
1193 Node width...
1194 \end{mptparam}
1195
1196 \begin{exemple}[lefthand ratio = 0.6]
1197 nodeformat:="square";
1198 nodewidth:=1cm;
1199 defnodes((0cm,0cm),(2cm,0),(4cm,0));
1200 drawnodes();
1201 \end{exemple}
1202
1203 \begin{exemple}[lefthand ratio = 0.6]
1204 nodewidth:=0.2cm;
1205 defnodes((0cm,0cm),(2cm,0),(4cm,0));
1206 drawnodes.bot();
1207 \end{exemple}
1208
1209 \begin{mptparam}{naturalwidth}{boolean}{false}
1210 When set to true, the width of the node adapts to the content.
1211 \end{mptparam}
1212
1213 \begin{exemple}[lefthand ratio = 0.6]
1214 defnodename:="AA";
1215 naturalwidth:=true;
1216 defnodes((0cm,0cm),(2cm,0),(4cm,0));
1217 drawnodes();
1218 \end{exemple}
1219
1220 \begin{mptparam}{nodelinewidth}{numeric}{1}
1221 Line width of the path around the node.
1222 \end{mptparam}
1223
1224 \begin{mptparam}{nodelinecolor}{color}{black}
1225 Color of the path around the node.
1226 \end{mptparam}
1227
1228 \begin{mptparam}{nodebgcolor}{color}{white}
1229 Color of the background of the region delimited by the previous path.
1230 \end{mptparam}
1231
1232 \begin{mptparam}{nodefgcolor}{color}{black}
1233 Color of the text.
1234 \end{mptparam}
1235
1236
1237 \begin{exemple}[lefthand ratio = 0.6]
1238 nodelinecolor:=(0.8,0,0);
1239 nodebgcolor:=(1,0.5,0.5);
1240 nodefgcolor:=white;
1241 nodeformat:="superellipse";
1242 defnodes((0cm,0cm),(2cm,0),(4cm,0));
1243 drawnodes();
1244 \end{exemple}
1245
1246
1247 \begin{rpobjet}{nodeshape(expr A,B)}{(cycle) path}
1248 In the same way as for trees, it is also possible to redefine the path defining the node. Simply rewrite the \verb|nodeshape| macro, which must take a picture as parameter and whose result must be a cycle path.
1249
1250 The parameter passed to the macro is the figure representing the drawing of the event (normally the name of the event). You may use it, or not, in the macro definition.
1251
1252 Note that in the path definition, the coordinates $(0,0)$ correspond to the node coordinates.
1253
1254 \end{rpobjet}
1255
1256 \begin{exemple}[lefthand ratio = 0.65]
1257 vardef nodeshape(expr p)=
1258 fullcircle xscaled 1.5nodewidth yscaled 0.8nodewidth
1259 enddef;
1260
1261 defnodes((0cm,0cm),(2cm,2cm),(4cm,0));
1262 drawnodes();
1263 \end{exemple}
1264
1265
1266 \begin{exemple}[lefthand ratio = 0.65]
1267 vardef nodeshape(expr p)=
1268 pair c,h,v;
1269 h:=lrcorner p - llcorner p;
1270 v:=ulcorner p - llcorner p;
1271 c:=center p;
1272 (c-h -- c+v -- c+h -- c-v -- cycle) shifted -c
1273 enddef;
1274
1275 defnodes((0cm,0cm),(2cm,2cm),(4cm,0));
1276 drawnodes();
1277 \end{exemple}
1278
1279
1280
1281
1282 \begin{rpdeclaration}{drawnodes(<nodes>) withbgcolor <color>}
1283 While \verb|withcolor| applies to lines and labels, \verb|withbgcolor| applies to the filling of nodes.
1284 \end{rpdeclaration}
1285
1286 \begin{exemple}[lefthand ratio = 0.63]
1287 nodelinecolor:=(0.8,0,0);
1288 nodebgcolor:=(1,0.5,0.5);
1289 nodefgcolor:=white;
1290 nodeformat:="superellipse";
1291 defnodes((0cm,0cm),(2cm,0),(4cm,0));
1292 drawnodes() withcolor black withbgcolor (0.6,0.6,1);
1293 \end{exemple}
1294
1295
1296 \begin{rplabel}{drawnode.<pos>(<node>,<label>)}
1297 The nodes can be drawn one by one, specifying the text to be displayed if necessary.
1298 \end{rplabel}
1299
1300 \begin{exemple}[lefthand ratio = 0.6]
1301 defnodes((0cm,0cm),(2cm,0),(4cm,0));
1302 drawnode[60](1);
1303 drawnode(2,"$\sqrt{2}$") withbgcolor 0.7white;
1304 drawnode.bot(A3,"Node");
1305 \end{exemple}
1306
1307 \section{Edges}
1308 \subsection{Undirected edges}
1309
1310 \begin{rplabel}{drawedges.<pos>(<(Na,Nb)>,<string1>,<(Nc,Nd)>,<string2>,...)(<angle1>,<angle2>)}
1311 Draw undirected edges between Node Na and node Nb, between node Nc and node Nd,... Na, Nb, Nc, Nd... must be integers. You can't refer to the nodes with their name.
1312
1313 If present, the string is printed at the position indicated in relation to the middle of the edge (by default).
1314
1315 By default, the edge is a segment. If an angle is specified, it is added to the angle at the start of the edge and subtracted from the angle at the end of the edge. If two angles are specified, they are added to the start and end angles respectively.
1316 \end{rplabel}
1317
1318
1319 \begin{exemple}[lefthand ratio = 0.6]
1320 defnodes((0cm,0cm),(2cm,2cm),(4cm,0));
1321 drawnodes();
1322 drawedges((1,2),(3,1))();
1323 \end{exemple}
1324
1325 \begin{exemple}[lefthand ratio = 0.6]
1326 defnodes((0cm,0cm),(2cm,2cm),(4cm,0));
1327 drawnodes();
1328 drawedges((1,2),(3,1),"$p$")();
1329 \end{exemple}
1330
1331 \begin{exemple}[lefthand ratio = 0.6]
1332 defnodes((0cm,0cm),(2cm,2cm),(4cm,0));
1333 drawnodes();
1334 drawedges.bot((1,2),(3,1),"$p$")();
1335 \end{exemple}
1336
1337 \begin{exemple}[lefthand ratio = 0.6]
1338 defnodes((0cm,0cm),(2cm,2cm),(4cm,0));
1339 drawnodes();
1340 drawedges((1,2),(3,1))(30);
1341 \end{exemple}
1342
1343 \begin{exemple}[lefthand ratio = 0.6]
1344 defnodes((0cm,0cm),(2cm,2cm),(4cm,0));
1345 drawnodes();
1346 drawedges((1,2),(3,1))(40,10);
1347 \end{exemple}
1348
1349
1350
1351 \begin{rplabel}{drawedge.<pos>(<Na>,<Nb>)(<angle1>,<angle2>,<string>)}
1352 The edges can be drawn one by one.In this case, Na, Nb can be the node name. The label or weight must be indicated in the second set of parameters.
1353 \end{rplabel}
1354
1355 \begin{exemple}[lefthand ratio = 0.6]
1356 defnodes((0cm,0cm),(2cm,2cm),(4cm,0));
1357 drawnodes();
1358 drawedge(1,2)();
1359 drawedge.bot(1,3)("$p$");
1360 drawedge(2,3)(30,"2");
1361 \end{exemple}
1362
1363 \subsection{Directed edges}
1364
1365 \begin{rplabel}{drawdiredges.<pos>(<(Na,Nb)>,<string1>,<(Nc,Nd)>,<string2>,...)(<angle1>,<angle2>)}
1366 Draw directed edges from Node Na to node Nb, from node Nc to node Nd,... Na, Nb, Nc, Nd... must be integers. You can't refer to the nodes with their name.
1367
1368 If present, the string is printed at the position indicated in relation to the middle of the edge (by default).
1369
1370 By default, the edge is a segment. If an angle is specified, it is added to the angle at the start of the edge and subtracted from the angle at the end of the edge. If two angles are specified, they are added to the start and end angles respectively.
1371 \end{rplabel}
1372
1373
1374 \begin{exemple}[lefthand ratio = 0.6]
1375 defnodes((0cm,0cm),(2cm,2cm),(4cm,0));
1376 drawnodes();
1377 drawdiredges((1,2),"35",(3,1),"$p$")();
1378 \end{exemple}
1379
1380 \begin{exemple}[lefthand ratio = 0.6]
1381 defnodes((0cm,0cm),(2cm,2cm),(4cm,0));
1382 drawnodes();
1383 drawdiredges.bot((1,2),(3,1),"$p$")(30);
1384 \end{exemple}
1385
1386 \begin{rplabel}{drawdiredge.<pos>(<Na>,<Nb>)(<angle1>,<angle2>,<string>)}
1387 The directed edges can be drawn one by one. In this case, Na, Nb can be the node name. The label or weight must be indicated in the second set of parameters.
1388 \end{rplabel}
1389
1390 \begin{exemple}[lefthand ratio = 0.6]
1391 defnodes((0cm,0cm),(2cm,2cm),(4cm,0));
1392 drawnodes();
1393 drawdiredge(1,2)();
1394 drawdiredge.bot(1,3)("$p$");
1395 drawdiredge(2,3)(30,"2");
1396 \end{exemple}
1397
1398 \subsection{Loops}
1399 \begin{rplabel}{draw(dir)edge.<pos>(<Na>,<Na>)(<angle1>,<angle2>,<string>)}
1400 Previous commands can be used to draw loops. If present, \verb|angle1| indicates the angle of the loop with horizontal direction. Default value is 90.
1401 \end{rplabel}
1402
1403 \begin{exemple}[lefthand ratio = 0.6]
1404 defnodes((0cm,0cm),(2cm,2cm),(4cm,0));
1405 drawnodes();
1406 drawdiredge(1,1)();
1407 drawedge(3,3)(-150);
1408 drawdiredge.bot(2,2)(-90,"a");
1409 \end{exemple}
1410
1411
1412 \subsection{Parameters}
1413 \begin{mptparam}{edgelinewidth}{numeric}{1}
1414 Width of the edge.
1415 \end{mptparam}
1416
1417 \begin{exemple}[lefthand ratio = 0.6]
1418 edgelinewidth:=2;
1419 defnodes((0cm,0cm),(2cm,2cm),(4cm,0));
1420 drawnodes();
1421 drawedges((1,2),(3,1),"$p$")();
1422 \end{exemple}
1423
1424 \begin{mptparam}{nodeedgeoffset}{numeric}{0}
1425 Space between the node and the edge.
1426 \end{mptparam}
1427
1428 \begin{exemple}[lefthand ratio = 0.6]
1429 nodeedgeoffset:=0.3cm;
1430 defnodes((0cm,0cm),(2cm,2cm),(4cm,0));
1431 drawnodes();
1432 drawedges((1,2),(3,1),"$p$")();
1433 \end{exemple}
1434
1435 \begin{mptparam}{startedgeshift}{numeric}{0}
1436 Space added at the beginning of the edge.
1437 \end{mptparam}
1438
1439 \begin{mptparam}{endedgeshift}{numeric}{0}
1440 Space added at the end of the edge.
1441 \end{mptparam}
1442
1443 \begin{mptparam}{edgeshift}{numeric}{0}
1444 Space added at both start and end of the edge.
1445 \end{mptparam}
1446
1447 \begin{exemple}[lefthand ratio = 0.6]
1448 defnodes((0cm,0cm),(2cm,2cm),(4cm,0));
1449 drawnodes();
1450 drawedges((1,2),(1,3),"$p$")();
1451 startedgeshift:=-0.3cm;endedgeshift:=-0.1cm;
1452 drawedges((1,3))() withcolor blue;
1453 edgeshift:=0.2cm;
1454 drawedges((1,2))() withcolor blue;
1455 \end{exemple}
1456
1457
1458 \begin{mptparam}{probformat}{string}{""}
1459 String that indicates how the weights are printed (the shape of the path). Possible values are (for now) \verb|"bbox"|, \verb|"circle"|, \verb|"superellipse"| and \verb|"square"|.
1460
1461 \end{mptparam}
1462
1463
1464 \begin{mptparam}{problinecolor}{color}{black}
1465 Color of the path around the weight.
1466 \end{mptparam}
1467
1468 \begin{mptparam}{probgcolor}{color}{white}
1469 Color of the background of the region delimited by the previous path.
1470 \end{mptparam}
1471
1472 \begin{mptparam}{probfgcolor}{color}{black}
1473 Color of the text.
1474 \end{mptparam}
1475
1476 \begin{exemple}[lefthand ratio = 0.6]
1477 problinecolor:=(0.8,0,0);
1478 probbgcolor:=(1,0.5,0.5);
1479 probfgcolor:=white;
1480 probformat:="square";
1481 defnodes((0cm,0cm),(2cm,2cm),(4cm,0));
1482 drawnodes();
1483 drawedges((1,2),"35",(3,1),"$p$")();
1484 \end{exemple}
1485
1486 \begin{mptparam}{posprob}{numeric}{0.5}
1487 Position of the weight on the edge.
1488 \end{mptparam}
1489
1490 \begin{exemple}[lefthand ratio = 0.6]
1491 defnodes((0cm,0cm),(2cm,2cm),(4cm,0));
1492 posprob:=0.75;
1493 drawnodes();
1494 drawedges((1,2),"35",(3,1),"$p$")();
1495 \end{exemple}
1496
1497 \begin{mptparam}{edgearrowpos}{numeric}{1}
1498 Position of arrowhead on the edge.
1499 \end{mptparam}
1500
1501 \begin{exemple}[lefthand ratio = 0.6]
1502 defnodes((0cm,0cm),(2cm,2cm),(4cm,0));
1503 edgearrowpos:=0.5;
1504 drawnodes();
1505 drawdiredges.bot((1,2),(3,1),"$p$")();
1506 \end{exemple}
1507
1508 \begin{mptparam}{loopangle}{numeric}{90}
1509 Loop direction. It can be specified in the command parameters.
1510 \end{mptparam}
1511
1512 \begin{exemple}[lefthand ratio = 0.6]
1513 defnodes((0cm,0cm),(2cm,2cm),(4cm,0));
1514 loopangle:=-90;
1515 drawnodes();
1516 drawdiredges((1,1),(2,2),(3,3))();
1517 \end{exemple}
1518
1519 \begin{exemple}[lefthand ratio = 0.6]
1520 defnodes((0cm,0cm),(2cm,2cm),(4cm,0));
1521 drawnodes();
1522 drawdiredges((1,1),(2,2),(3,3))(-90);
1523 \end{exemple}
1524
1525 \begin{mptparam}{loopstartangle}{numeric}{30}
1526 Angle between the path direction at node center and loop direction.
1527 \end{mptparam}
1528
1529 \begin{exemple}[lefthand ratio = 0.6]
1530 defnodes((0cm,0cm),(2cm,2cm),(4cm,0));
1531 loopstartangle:=90;
1532 drawnodes();
1533 drawdiredges((1,1),(2,2),(3,3))(-90);
1534 \end{exemple}
1535
1536
1537 \begin{mptparam}{loopsize}{numeric}{1.5nodesize}
1538 Loop size.
1539 \end{mptparam}
1540
1541 \begin{exemple}[lefthand ratio = 0.6]
1542 defnodes((0cm,0cm),(2cm,0cm),(4cm,0));
1543 loopsize:=1.5cm;
1544 drawnodes();
1545 drawdiredges((1,1),(2,2),(3,3))();
1546 \end{exemple}
1547
1548 \begin{rpobjet}{edgeshape(expr A,B)}{path}
1549 In the same way as for trees, it is also possible to completely redefine the path defining the edge. Simply rewrite the \verb|edgeshape| macro, which must take two pairs as parameters and whose result must be a path.
1550
1551 The first parameter represents the start point and the second the end point.
1552
1553 \end{rpobjet}
1554
1555 \begin{exemple}[lefthand ratio = 0.6]
1556 vardef edgeshape(expr A,B)=
1557 save a;numeric a;a=angle(B-A);
1558 A--((0.25cm,0) rotated (a+30) shifted 0.5[A,B])
1559 --((0.25cm,0) rotated (a-150) shifted 0.5[A,B])
1560 --B
1561 enddef;
1562
1563 defnodes((0cm,0cm),(2cm,2cm),(4cm,0));
1564 drawnodes();
1565 drawedge(1,2)();
1566 drawdiredge(3,1)();
1567 \end{exemple}
1568
1569
1570 \section{Complete graphs}
1571
1572 \begin{rplabel}{draw(dir)graph.<pos>(<(Na,Nb)>,<string1>,<(Nc,Nd)>,<string2>,...)(<angle1>,<angle2>)}
1573 These commands are shortcuts for drawing both nodes and edges.
1574 \end{rplabel}
1575
1576
1577 \begin{exemple}[lefthand ratio = 0.6]
1578 defnodes((0cm,0cm),(2cm,2cm),(4cm,0));
1579 drawgraph((1,2),"35",(3,1),"$p$")();
1580 \end{exemple}
1581
1582 \begin{exemple}[lefthand ratio = 0.6]
1583 defnodes((0cm,0cm),(2cm,2cm),(4cm,0));
1584 nodewidth:=0.2cm;
1585 drawdirgraph.bot((1,2),(3,1),(2,2))(30);
1586 \end{exemple}
1587
1588
1589 \section{Grids}
1590 To facilitate node placement, use the following command to draw a grid.
1591
1592 \begin{rplabel}{drawgrid(<xmin>,<ymin>,<xmax>,<ymax>)(<unit>,<step>)}
1593 Command for drawing a grid. Minimum values are optional and equal to 0 by default. The unit and step values are also optional and default to 1 cm and 1 respectively.
1594 \end{rplabel}
1595
1596 \begin{exemple}[lefthand ratio = 0.6]
1597 drawgrid(4,4)();
1598 defnodes((0cm,0cm),(2cm,2cm),(4cm,0));
1599 drawnodes();
1600 drawedges((1,2),(3,1))();
1601 \end{exemple}
1602
1603 \begin{exemple}[lefthand ratio = 0.6]
1604 drawgrid(-1,-1,5,3)(1cm,0.5);
1605 defnodes((0cm,0cm),(2cm,2cm),(4cm,0));
1606 drawnodes();
1607 drawedges((1,2),(3,1))();
1608 \end{exemple}
1609
1610 \begin{exemple}[lefthand ratio = 0.6]
1611 u:=0.75cm;
1612 drawgrid(-1,-1,5,3)(u);
1613 defnodes((0,0),(2u,2u),(4u,0));
1614 drawnodes();
1615 drawedges((1,2),(3,1))();
1616 \end{exemple}
1617
1618
1619 \section{Examples}
1620
1621 \subsection*{Weighted graph}
1622 \begin{exemple}[listing above text]
1623 u:=0.8cm;
1624 nodewidth:=0.15cm;
1625 nodebgcolor:=black;
1626 printnodename:=false;
1627 defnodes((5u,0),(0,3u),(2u,4u),(7u,4u),(3u,6u),(2u,7u),(12u,7u),(7u,8u),
1628 (10u,8u),(9.5u,9.5u));
1629 drawnodes();
1630 drawedges((1,2),"9",(1,3),"9",(1,4),"8",(1,7),"18")();
1631 drawedges((2,3),"3",(2,6),"6")();
1632 drawedges((3,4),"9",(3,5),"2",(3,6),"4")();
1633 drawedges((4,5),"8",(4,7),"10",(4,8),"7",(4,9),"9")();
1634 drawedges((5,6),"2",(5,8),"9")();
1635 drawedges((6,8),"9")();
1636 drawedges((7,9),"3",(7,10),"4")();
1637 drawedges((8,9),"5",(8,10),"4")();
1638 drawedges((9,10),"1")();
1639 \end{exemple}
1640
1641
1642 \subsection*{Minimum spanning tree}
1643 \begin{exemple}[listing above text]
1644 u:=0.8cm;
1645 nodewidth:=0.15cm;
1646 nodebgcolor:=black;
1647 printnodename:=false;
1648 defnodes((5u,0),(0,3u),(2u,4u),(7u,4u),(3u,6u),(2u,7u),(12u,7u),(7u,8u),
1649 (10u,8u),(9.5u,9.5u));
1650 drawnodes();
1651 drawoptions(withcolor 0.7white);
1652 drawedges((1,2),"9",(1,3),"9",(1,7),"18")();
1653 drawedges((2,6),"6")();
1654 drawedges((3,4),"9",(3,6),"4")();
1655 drawedges((4,7),"10",(4,9),"9")();
1656 drawedges((5,8),"9")();
1657 drawedges((6,8),"9")();
1658 drawedges((7,10),"4")();
1659 drawedges((8,9),"5")();
1660 drawoptions(withcolor black);
1661 edgelinewidth:=2;
1662 drawedges((1,4),"8",(2,3),"3",(3,5),"2",(4,5),"8",(5,6),"2",(4,8),"7",
1663 (8,10),"4",(9,10),"1",(7,9),"3")();
1664 \end{exemple}
1665
1666
1667 \subsection*{Graph colouring}
1668
1669 \begin{exemple}[lefthand ratio = 0.6]
1670 printnodename:=false;
1671 nodewidth:=0.3cm;
1672 for i=1 upto 7:
1673 defnodes(((2cm,0) rotated (360*i/7)));
1674 endfor
1675 nodebgcolor:=red;
1676 drawnodes(2,6);
1677 nodebgcolor:=blue;
1678 drawnodes(1,4);
1679 nodebgcolor:=green;
1680 drawnodes(3,5,7);
1681 drawedges((1,5),(1,6),(1,7),(2,3),(2,4),(2,5),
1682 (3,4),(4,5),(4,6),(4,7),(6,7))();
1683 \end{exemple}
1684
1685 \bigskip
1686 With Lua\LaTeX{} and \verb|luamplib| package you can use \verb|\includegraphics| inside a \MP{} code:
1687
1688 \begin{exemple}[listing above text]
1689 u:=0.5cm;
1690 nodewidth:=0.4cm;
1691 nodename:="arabic";
1692 printnodename:=false;
1693 draw btex \includegraphics[width=10cm]{france-region.jpg} etex;
1694 % picture from https://capcarto.fr/telechargements/
1695 draw grid(20,18)(u);
1696 defnodes((4.5u,12.5u),(7.5u,14.5u),(5u,10u),(9.5u,16.5u),(10u,13u),
1697 (15u,13u),(9u,12u),(12u,11u),(6u,8u),(11u,7u),(9u,3u),
1698 (15u,5u),(19u,1u));
1699 nodebgcolor:=red;
1700 drawnodes(7,4,11,13);
1701 nodebgcolor:=green;
1702 drawnodes(2,10,6);
1703 nodebgcolor:=blue;
1704 drawnodes(5,3,12);
1705 nodebgcolor:=(1,1,0);
1706 drawnodes(1,8,9);
1707 drawedges((1,2),(1,3),(2,3),(2,4),(2,5),(2,7),(3,7),(3,9),(4,5),
1708 (4,6),(5,6),(5,7),(5,8),(6,8),(7,8),(7,9),(7,10),(8,10),
1709 (9,10),(9,11),(10,11),(10,12),(11,12))();
1710 \end{exemple}
1711
1712
1713
1714 \subsection*{Markov chain}
1715
1716 \begin{exemple}[lefthand ratio = 0.6]
1717 scaleprob:=0.8;
1718 u:=1.6cm;
1719 nodebgcolor:=0.8white;
1720 defnodes((0,0),(2u,0),(u,1.5u));
1721 drawnodes();
1722 drawoptions(withcolor red);
1723 drawdiredges((1,2),"$0.5$",(1,3),"$0.3$")(-25);
1724 drawdiredge(1,1)("$0.2$",210);
1725 drawoptions(withcolor blue);
1726 drawdiredges((2,1),"$0.1$",(2,3),"$0.3$")(-25);
1727 drawdiredge(2,2)("$0.6$",-30);
1728 drawoptions(withcolor 0.5green);
1729 drawdiredges((3,1),"$0.7$",(3,2),"$0.2$")(-25);
1730 drawdiredge(3,3)("$0.1$");
1731 \end{exemple}
1732
1733 \subsection*{Hypercube $Q_3$}
1734
1735 \begin{exemple}[lefthand ratio = 0.65]
1736 u:=3cm;
1737 nodewidth:=0.3cm;
1738 edgelinewidth:=2;
1739 pair p; p:=(0.4u,0.4u);
1740 defnodes((0,0),(u,0),(0,u),(u,u));
1741 defnodes((0,0)+p,(u,0)+p,(0,u)+p,(u,u)+p);
1742 nodebgcolor:=black;nodelinecolor:=black;
1743 drawnode.lrt(6,"101");
1744 nodebgcolor:=red;nodelinecolor:=red;
1745 drawnode.llft(1,"000");drawnode.lrt(2,"001");
1746 drawnode.rt(4,"011");drawnode.lrt(5,"100");
1747 nodebgcolor:=blue;nodelinecolor:=blue;
1748 drawnode.lft(3,"010");drawnode.ulft(7,"110");
1749 drawnode.urt(8,"111");
1750 drawedges((1,3),(3,4),(2,6),(4,8),(6,8),(5,6),(5,7))();
1751 drawedges((3,7),(7,8))() withcolor blue;
1752 drawedges((1,2),(1,5),(2,4))() withcolor red;
1753 \end{exemple}
1754
1755 \subsection*{Three utilities problem}
1756
1757 With Lua\LaTeX{}, \verb|luamplib| and \verb|fontawesome5| packages.
1758
1759 \begin{exemple}[lefthand ratio = 0.7]
1760 u:=1.2cm;
1761 defnodes((u,2u),(2u,2u),(3u,2u),(u,u),(2u,u),(3u,u));
1762 nodelinecolor:=white;
1763 drawnode(1,"\large \faHome");
1764 drawnode(2,"\large \faHome");
1765 drawnode(3,"\large \faHome");
1766 nodebgcolor:=black;nodefgcolor:=blue;
1767 drawnode(4,"\faWater");
1768 nodefgcolor:=red;drawnode(5,"\faBurn");
1769 nodefgcolor:=(1,1,0);drawnode(6,"\faBolt");
1770 drawedges((1,4),(2,4),(2,5),(2,6),(3,6))();
1771 drawedges((1,5),(5,3))(-115);
1772 vardef edgeshape(expr A,B)=
1773 save a;numeric a;a=angle(B-A);s:=if A>B: - fi 1;
1774 A..(0.7[A,B]+(0.35(B-A) rotated (s*90))){dir a}..B
1775 enddef;
1776 drawedges((4,3))();
1777 edgelinewidth:=2;drawedges((6,1))() withcolor (1,0.5,0);
1778 \end{exemple}
1779
1780 \subsection*{Various}
1781
1782 \begin{exemple}[lefthand ratio = 0.6]
1783 nodebgcolor:=0.7white;
1784 nodewidth:=0.2cm;
1785
1786 for i=1 upto 7:
1787 defnode(A[i],(2cm,0) rotated (360*i/7));
1788 drawnode[360*i/7](i);
1789 for j=1 upto i-1:
1790 drawedge(i,j)();
1791 endfor
1792 endfor
1793 \end{exemple}
1794
1795 \begin{exemple}[lefthand ratio = 0.6]
1796 nodebgcolor:=0.7white;
1797 nodewidth:=0.2cm;
1798 printnodename:=false;
1799
1800 u:=0.7cm;
1801 defnodes((3u,-3u));
1802 drawnode(1);
1803 for i=1 upto 4:
1804 for j=1 upto 4:
1805 defnodes((0,-2u) rotatedaround((0,-3u),90j)
1806 rotated 90i);
1807 endfor
1808 defnodes((3u,-3u) rotated (90i));
1809 drawnodes(5i-3,5i-2,5i-1,5i,5i+1);
1810 drawedges((5i-4,5i-3),(5i-3,5i-2),(5i-2,5i-1),
1811 (5i-1,5i),(5i,5i-3),(5i-1,5i+1))();
1812 endfor;
1813 \end{exemple}
1814
1815
1816 \begin{exemple}[listing above text,before lower=\footnotesize]
1817 u:=1.2cm;
1818 nodewidth:=0.4cm;
1819 defnodes((0,0.6u),(3u,0.6u),(5u,0),(8u,0),(10u,0.6u),(13u,0.6u),(0,-0.6u),
1820 (3u,-0.6u),(10u,-0.6u),(13u,-0.6u));
1821 nodeedgeoffset:=-0.2cm;
1822 nodebgcolor:=0.8white;
1823 edgelinewidth:=0.2cm;
1824 vardef edgeshape (expr A,B)=A{dir 0}..{dir 0}B enddef;
1825 drawedges((1,2))() withcolor red;
1826 drawedges((5,6))() withcolor red;
1827 drawedges((7,8))() withcolor blue;
1828 drawedges((9,10))() withcolor blue;
1829 endedgeshift:=0.1cm; drawedges((2,3))() withcolor red;
1830 endedgeshift:=-0.1cm; drawedges((8,3))() withcolor blue;
1831 endedgeshift:=0cm;
1832 startedgeshift:=0.1cm; drawedges((4,5))() withcolor red;
1833 startedgeshift:=-0.1cm;drawedges((4,9))() withcolor blue;
1834 edgeshift:=0.1cm; drawedges((3,4))() withcolor red;
1835 edgeshift:=-0.1cm; drawedges((3,4))() withcolor blue;
1836 drawnode.bot(1,"C.H.U.");
1837 drawnode.bot(2,"Berges de Maine");
1838 drawnode.bot(5,"Hôtel de Ville");
1839 drawnode.bot(6,"Foch - Maison bleue");
1840 drawnode.bot(7,"Doutre");
1841 drawnode.bot(8,"Place Molière");
1842 drawnode.bot(9,"Conservatoire");
1843 drawnode.bot(10,"Montaigne");
1844 vardef nodeshape(expr p)=
1845 (halfcircle shifted (0,0.5)--
1846 halfcircle scaled -1 shifted (0,-0.5)--
1847 cycle) scaled nodewidth
1848 enddef;
1849 drawnode.bot(3,"Saint-Serge");
1850 drawnode.bot(4,"Centre de Congrès");
1851 \end{exemple}
1852
1853 \begin{exemple}[listing above text]
1854 vardef nodeshape(expr p)=
1855 % From "Drawing with Metapost" by Toby Thurston
1856 % https://github.com/thruston/Drawing-with-Metapost/
1857 for i=1 upto 20:
1858 (0.4cm if odd(i): - else: + fi 3+ uniformdeviate 3,0) rotated (i*360/20) --
1859 endfor cycle
1860 enddef;
1861 vardef edgeshape(expr A,B)=
1862 save an;numeric an;an:=angle(B-A);
1863 A{dir (an + 60*(uniformdeviate 2-1))}..
1864 0.5[A,B]{dir (180+an)}..
1865 {dir (an + 60*(uniformdeviate 2-1))}B
1866 enddef;
1867 nodebgcolor:=(1,0.5,0);
1868 nodelinecolor:=0.6red;
1869 nodefgcolor:=0.6red;
1870 linecolor:=0.5green;
1871 edgelinewidth:=1.5;
1872 defnodes((0cm,0cm),(4cm,2cm),(8cm,1cm),(5cm,-3cm));
1873 drawnodes();
1874 drawedges((1,2),(2,3),(3,4),(1,4))();
1875 \end{exemple}
1876
1877 \end{document}