draw uml with latex&metauml

I've used and enjoyed the benefits of reversion control system for several years. RCS makes my life a lot easier. And it pushes me to highly prefer text format files over binary files, because text files can be managed by RCS more easily. metauml is a metapost library for creating uml diagrams, in text format.

Here are the steps to use it on ubuntu:
  1. install texlive with: sudo apt-get install texlive
  2. install metauml containing in metapost for tex: sudo apt-get install texlive-metapost
Texlive is also available for windows.
After all these tools have being installed, we can start our first uml diagram in latex.
Suppose we have following classes, and want to draw class diagram for them.
 1 class Point
 2 {
 3 public:
 4     int x;
 5     int y;
 6 };
 7 
 8 class Shape
 9 {
10     public:
11         virtual int get_circumference() = 0;
12         virtual ~shape();
13 };
14 
15 class Circle : public Shape
16 {
17     private:
18         Point center;
19         int radius;
20     public:
21         int get_circumference();
22 };


We create below metapost file, save it as class_diagram.mp, and use "mpost class_diagram.mp" command to generate a postscript file, class_diagram.1.
 1 input metauml;
 2 beginfig(1);
 3     %define classes
 4     Class.Point("Point")("+x: int""+y: int")();
 5     Class.Shape("Shape")()("+get_circumference(): int");
 6     Class.Circle("Circle")("-center: Point""-radius:  int")("+get_circumference(): int");
 7
 8     %layout classes
 9     topToBottom(50)(Point, Circle);
10     leftToRight(50)(Circle, Shape);
11 
12     %draw classes
13     drawObjects(Point, Shape, Circle);
14 
15     %link classes
16     link(inheritance)(Circle.e -- Shape.w);
17     link(composition)(Point.s -- Circle.n);
18 endfig;
19 end
20 

 Then we create below tex file as a container document for the postscript. And use "pdflatex uml.tex" to generate the final pdf file.
 1 /documentclass{article}
 2 
 3 % The following is needed in order to make the code compatible
 4 % with both latex/dvips and pdflatex.
 5 /ifx/pdftexversion/undefined
 6 /usepackage[dvips]{graphicx}
 7 /else
 8 /usepackage[pdftex]{graphicx}
 9 /DeclareGraphicsRule{*}{mps}{*}{}
10 /fi
11 
12 /title{MetaUML example}
13 /author{Raymond Wen}
14 
15 /begin{document}
16 
17 /maketitle
18 
19 /section{Example}
20 /includegraphics{class_diagram.1}
21 
22 /end{document}

A thing worth noticing is the latex document is of A4 size by default, which is not possible to contain a complex uml diagram. We can use " /paperwidth = 1024pt", " /paperheight = 1024pt", "/textheight = 800pt" command to create a document of arbitrary size. Here is more information about latex page layout.
The final diagram is shown below:

The advantages of using tex file to draw uml diagram includes:
  1. We can concentrate on the content of the uml, rather than its layout
  2. The diagram can be easily version controlled and compared
  3. It's easier to make modifications
  4. It's totally free
  5. The output file format can be easily changed
  6. It's possible to use/write tools to generate uml diagram automatically
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值