Chapter 2
Class diagrams

2.1 Packages, classes, attributes and operations

2.1.1 To define a package

You can define a package with the umlpackage environment:

  \begin{tikzpicture}  \begin{umlpackage}[x=0, y=0]{package-name}  \end{umlpackage}  \end{tikzpicture}


Both options x and y allow to define the package position in the figure. The default value for both of them is 0.

  \begin{tikzpicture}  \begin{umlpackage}[x=0, y=0, type=context]{package-name}  \end{umlpackage}  \end{tikzpicture}


Option type

2.1.2 To define a class

You can define a class with the umlclass command:

  \begin{tikzpicture}  \umlclass{namespace::A}{    + n : uint \\ \umlstatic{-- i : int} \\ \# r : const float    }{    + setA(i : int) : void \\ \umlvirt{\# getA() : A}  }  \end{tikzpicture}


The class name is defined with the first argument or the umlclass command. As you can see, you can use double dots in it.

The attributes of a class are defined with the second argument of the umlclass command. You write the attributes list using \\ as a delimiter. The operations of a class are defined with the third argument of the umlclass command.

To define a static attribute or a static operation, you can use the umlstatic command. In a similar way, the umlvirt command is used to define a virtual operation.

For empty classes (that contains no attributes and no operations), you can use a shortcut command umlemptyclass:

  \begin{tikzpicture}  \umlemptyclass{namespace::class-name}  \end{tikzpicture}


You may also prefer drawing a single rectangle node instead of a 3 parts rectangle node. In this case, you may use the simple option of umlclass or the shortcut command umlsimpleclass that also takes only the class name for argument and the same options as the command umlclass:

  \begin{tikzpicture}  \umlsimpleclass{namespace::class-name}  \end{tikzpicture}


The umlclass command (and its shortcuts) also takes options.

Name of the node defining a class

To name a class, you may want use special characters, such as _, or double dots (:) when giving namespace. Internal mechanism of TikZ-UML name the class node with the class name. But to name a TikZ node, backslashes and double dots are forbidden. As far as double dots are concerned, string substitutions are done on the class name to define the node name. But for _, it does not work so easily. That is why you can directly name a node with the option name:

  \begin{tikzpicture}  \umlclass[x=0,y=0, name=classname]{namespace::class\_name}{}{}  \end{tikzpicture}


To define coordinates of the class node
  \begin{tikzpicture}  \umlclass{namespace::A}{    + n : uint \\ \umlstatic{-- i : int} \\ \# r : const float    }{    + setA(i : int) : void \\ \umlvirt{\# getA() : A}  }  \umlsimpleclass[x=1, y=-3]{B}  \end{tikzpicture}


Both options x and y allow to define the class position in the figure. 2 cases: if the class is defined inside a package, the class position is relative to the package; on the contrary, the class position is relative to the figure. The default value for both options is 0.

To define the width of a class

The default width of a class is 10ex. You can use the width option to specify an other value:

  \begin{tikzpicture}  \umlemptyclass[width=15ex]{class15}  \umlemptyclass[y=-2, width=30ex]{class30}  \end{tikzpicture}


To define type and tagged values of a class

There is different types of classes: class, interface, typedef, enum. You can specify it with the type option (the default value is class):

  \begin{tikzpicture}  \umlemptyclass[type=interface]{class-name}  \end{tikzpicture}


pict The type is written between and above the class name, excepted the class type (default behavior), and the abstract type, where the class name is written in italic style instead:

  \begin{tikzpicture}  \umlemptyclass[type=abstract]{class-name}  \end{tikzpicture}


pict Shortcuts exist for each value of the type option: umlabstract, umltypedef, umlenum, umlinterface. For these shortcuts, option type is forbidden.

pict You can also draw interfaces with a circular node. To do so, you can use the simple option to the umlinterface command, or use the shortcut umlsimpleinterface

  \begin{tikzpicture}  \umlsimpleinterface[x=0,y=0]{I}  \end{tikzpicture}


To add tagged values to a class, you can use the option tags:

  \begin{tikzpicture}  \umlemptyclass[tags={v1.2}]{class-name}  \end{tikzpicture}


To specify template parameters

For a template class, you can use the template option to specify the template parameters list:

  \begin{tikzpicture}  \umlemptyclass[template={T,U}]{class-name}  \end{tikzpicture}


2.2 To define a relation between classes

2.2.1 General command

Each class or package is draw as a node sharing the same name. To define a relation between two classes, you just need to specify the source class name, the target class name and a set of options specific to the relation:

  \begin{tikzpicture}  \umlemptyclass{A1}  \umlemptyclass[x=3, y=-3]{A2}  \umluniaggreg[arg2=a, mult2=1, pos2=0.9]{A1}{A2}  \umluniassoc[geometry=-|, arg1=x, mult1=1, pos1=1.9, arg2=y, mult2=*, pos2=0.2]{A1}{A2}  \umlunicompo[arg=z, mult=1..*, pos=0.8, angle1=-90, angle2=-140, loopsize=2cm]{A2}{A2}  \end{tikzpicture}


From a UML semantic point of view, there are 12 different relations. Every type of relation is defined in TikZ-UML :

A dependency:
You can use the umldep command


An association:
You can use the umlassoc command


A unidirectional association:
You can use the umluniassoc command


An aggregation:
You can use the umlaggreg command


A unidirectional aggregation
You can use the umluniaggreg command


A composition:
You can use the umlcompo command


A unidirectional composition:
You can use the umlunicompo command


An import:
You can use the umlimport command


An inheritance:
You can use the umlinherit command


An implementation:
You can use the umlimpl command


A nesting:
You can use the umlnest command


A realization:
You can use the umlreal command


These 12 shortcuts are based on the same scheme (the umlrelation command) and take theoretically the same set of options. Nevertheless, some options concern only part of them.

2.2.2 To define the geometry of a relation

As you may have seen in previous examples, you can specify the geometric shape of a relation with the geometry option. It needs a value among the following list: - - (straight line), -| (horizontal then vertical), |- (vertical then horizontal), -|- (horizontal chicane) ou |-| (vertical chicane). These values are very inspired from TikZ philosophy.

It seems that this option is used very often. That is why a shortcut of the umlrelation command has been defined each possible value of the geometry option:

shortcut of umlrelation with geometry=-|
shortcut of umlrelation with geometry=|-
shortcut of umlrelation with geometry=-|-
shortcut of umlrelation with geometry=|-|

pict For each of these 4 shortcuts, the geometry option is forbidden.

pict There is no shortcut for the value - -: this is the default value for the umlrelation command.

2.2.3 To adjust the geometry of a relation

When the geometry is built with 2 segments, you can define the coordinates of the auto-built point, named control node. Then, instead of using umlrelation, you should use the umlCNrelation command, or one of its 12 shortcuts:

  \begin{tikzpicture}  \umlemptyclass{A1}  \umlemptyclass[x=3,y=-3]{A2}  \umluniaggreg[geometry=-|]{A1}{A2}  \umlCNuniassoc{A1}{4,0}{A2}  \end{tikzpicture}


When the geometry is built with 3 segments, the relative position of the middle segment between classes is defined by the midlle of the classes nodes. You can adjust this parameter with the weight option:

  \begin{tikzpicture}  \umlemptyclass{A1}  \umlemptyclass[x=4, y=-1]{A2}  \umlemptyclass[y=-2]{A3}  \umlassoc[geometry=-|-]{A1}{A2}  \umluniaggreg[geometry=-|-, weight=0.3]{A3}{A2}  \end{tikzpicture}


In some cases, this option is not very convenient, because it needs to compute the option value to give. There is another possibility by uusing the arm1 and arm2 options, that control the size of the first and last segment respectively. Let’s see the 2 following examples using respectively the weight option and the arm1 option:

  \begin{tikzpicture}  \umlemptyclass{A1}  \umlemptyclass[x=4, y=1]{A2}  \umlemptyclass[x=6, y=3]{A3}    \umlHVHdep[weight=0.375]{A1}{A2}  \umlHVHdep[weight=0.25]{A1}{A3}  \end{tikzpicture}


  \begin{tikzpicture}  \umlemptyclass{A1}  \umlemptyclass[x=4, y=1]{A2}  \umlemptyclass[x=6, y=3]{A3}    \umlHVHdep[arm1=1.5cm]{A1}{A2}  \umlHVHdep[arm1=1.5cm]{A1}{A3}  \end{tikzpicture}


The arm1 and arm2 options also take negative values. How does it work then ? A positive value means an arm oriented to the right direction (to the right or to the top), whereas a negative valuemeans an arm oriented to the opposite direction, that allows you to draw other 3-segments relations:

  \begin{tikzpicture}  \umlemptyclass{A1}  \umlemptyclass[x=4, y=2]{A2}    \umlHVHdep[arm2=-2cm]{A1}{A2}  \umlHVHdep[arm2=2cm]{A1}{A2}  \end{tikzpicture}


2.2.4 To define informations about attributes of a relation

A relation means a dependency between two classes and represents an attribute in most of the cases. You can define its name with the arg1 option or the arg2 option, and its multiplicity with the mult1 option or the mult2 option:

  \begin{tikzpicture}  \umlemptyclass{A1}  \umlemptyclass[x=5]{A2}  \umlassoc[arg1=arg1, mult1=mult1, arg2=arg2, mult2=mult2]{A1}{A2}  \end{tikzpicture}


For unidirectional relations, you should use only arg2 and mult2 options. That is why shortcuts have been defined, namely the arg option and the mult option respectively.

In addition, when you define the name and the multiplicity of an attribute, you may prefer use the all-in-one following options attr1, attr2 and attr:

  \begin{tikzpicture}  \umlemptyclass{A1}  \umlemptyclass[x=5]{A2}  \umlassoc[attr1=arg1|mult1, attr2=arg2|mult2]{A1}{A2}  \end{tikzpicture}


This has an advantage: the semantic of the two values has disappeared and you can switch them for convenience:

  \begin{tikzpicture}  \umlemptyclass{A1}  \umlemptyclass[x=5]{A2}  \umlassoc[attr1=mult1|arg1, attr2=mult2|arg2]{A1}{A2}  \end{tikzpicture}


2.2.5 To place information about attributes of a relation

You can place information seen in previous section with the following options: pos1, pos2 and pos. The umlrelation command determine by itself if name and multiplicity should be written on left and right or on top and bottom of the arrow, according to the geometry and their placement. For those who know TikZ enough, the mechanism is based on auto and swap options.

  \begin{tikzpicture}  \umlemptyclass{A1}  \umlemptyclass[x=2, y=-2]{A2}  \umlemptyclass[x=-2, y=-2.5]{A3}  \umlassoc[geometry=-|, arg1=x, mult1=1, pos1=0.2, arg2=y, mult2=*, pos2=1.9]{A1}{A2}  \umlassoc[geometry=|-|, arg1=a, mult1=1, pos1=0.5, arg2=b, mult2=*, pos2=1.5]{A1}{A3}  \end{tikzpicture}


You may have noticed that the range of values of the position depends on the number of segments composing the arrow. For a straight line, position has to be between 0 (source class) and 1 (target class). If there are 2 segments, then position has to be between 0 et 2 (target class), the value 1 corresponding to the control node. Otherwise, position has to be between 0 et 3, values 1 and 2 corresponding to the first and second control node respectively.

2.2.6 To adjust the alignment of information about attributes of a relation

Name and multiplicity of an attribute, when they are written on top and bottom of the relation, are centered by default. You can define an other alignment. The options align1, align2 and align are used to have ragged right or ragged left text:

  \begin{tikzpicture}  \umlemptyclass{A1}  \umlemptyclass[x=4, y=-3]{A2}  \umlassoc[geometry=-|-, arg1=arg1, mult1=mult1, pos1=0.1, align1=left, arg2=arg2, mult2=mult2, pos2=2.9, align2=right]{A1}{A2}  \end{tikzpicture}


2.2.7 To define and place the stereotype of a relation

The stereotype of a relation is a keyword contained between and . You can define it with the option stereo and place it with the option pos stereo.

  \begin{tikzpicture}  \umlemptyclass{A1}  \umlemptyclass[x=4, y=-3]{A2}  \umlassoc[geometry=-|-, stereo=vector, pos stereo=1.2]{A1}{A2}  \end{tikzpicture}


2.2.8 To modify the anchor points of a relation

The default behavior of a relation is to start from the center anchor of the source class node and to end to the center anchor of the target class node. You can adjust this with the options anchor1 and anchor2.

  \begin{tikzpicture}  \umlemptyclass{A}  \umlemptyclass[x=4,y=2]{B}  \umldep[geometry=-|]{A}{B}  \umlassoc[geometry=-|, anchor1=30, anchor2=300, name=assoc1]{A}{B}  \umlassoc[geometry=-|, anchor1=-30, anchor2=-60, name=assoc2]{A}{B}  \end{tikzpicture}


You give angular values in degree and they can be negative. The internal mechanism of TikZ uses modulos. The value 0 is east, 90 is north, 180 (or -180) is west, et 270 (or -90) is south. The following figure illustrates this option and its angular meaning on 2 examples of rectangular nodes, (class nodes for instance). You can notice that border anchors (to use TikZ vocabulary) depend on node dimensions.


You will very often define anchor1 and anchor2 simultaneously. In this case, you can use the all-in-one option anchors:

  \begin{tikzpicture}  \umlemptyclass{A}  \umlemptyclass[x=4,y=2]{B}  \umldep[geometry=-|]{A}{B}  \umlassoc[geometry=-|, anchors=30 and 300, name=assoc1]{A}{B}  \umlassoc[geometry=-|, anchors=-30 and -60, name=assoc2]{A}{B}  \end{tikzpicture}


2.2.9 To define a recursive relation

You can define recursive relations, namely a relation from a class to itself. Then, the geometry option is disabled, but 3 specific options are available: angle1 determines the start angle, angle2 determines the end angle, and loopsize controls the size of the loop.

  \begin{tikzpicture}  \umlemptyclass{A1}  \umlassoc[arg=x, mult=1, pos=0.6, angle1=-90, angle2=-140, loopsize=2cm]{A1}{A1}  \umlassoc[arg=y, mult=1..*, pos=0.6, angle1=-90, angle2=-140, loopsize=4cm]{A1}{A1}  \umlassoc[arg=z, mult=0..*, pos=0.8, angle1=-90, angle2=0, loopsize=2cm]{A1}{A1}  \end{tikzpicture}


When you use recursive relations, you will notice that you will need the 3 options simultaneously. This is the reason why a compact form is defined, the recursive option, and the following syntax:

  \begin{tikzpicture}  \umlemptyclass{A1}  \umlassoc[arg=x, mult=1, pos=0.6, recursive=-90|-140|2cm]{A1}{A1}  \umlassoc[arg=y, mult=1..*, pos=0.6, recursive=-90|-140|4cm]{A1}{A1}  \umlassoc[arg=z, mult=0..*, pos=0.8, recursive=-90|0|2cm]{A1}{A1}  \end{tikzpicture}


2.2.10 Name of auto-built points of a relation

In order to understand the purpose of giving a name to a relation, one should explain how arrows are defined.

To build an arrow, we need to define control nodes, and a name for each one. The only way to identify a relation is to give a name using a id counter. This counter is incremented each time we define a relation in a picture. Let’s suppose the relation has the id i. The name of the relation, called relname in the following, is: relation-i

The first defined node is the middle of the class nodes. It is called relname-middle. To simplify, we will not deal with the placement of the argument and its multiplicity here. So, there are 3 cases:

  1. If the arrow is a straight line or a recursive line, it is renamed in relname-1.


  2. If the arrow has one right angle, the node placed at the angle is named relname-2, that is enough to draw the arrow. 2 other nodes are defined, placed at the middle of each arc and named respectively relname-1 and relname-3.


  3. If the arrow has 2 right angles, they are defined with relname-middle, that is enough to draw the arrow. Nodes placed at the angles are named respectively relname-2 and relname-4. 3 other nodes are defined, at the middle of each arc, named respectively relname-1, relname-3, and relname-5.


This default behavior is not easy to use, because the value of the counter is not defined by the user, and depends on the order of definition of the relations in the picture. This is the reason why you can define relname thanks to the name option. In the two following sections, you will see when this option is useful.

2.2.11 To draw an intersection point between relations

When you draw a diagram, it occurs that relations cross other ones or share arcs. Let’s take two crossing arrows. Can both start points go graphically to both end points ? If yes, you will want to draw a point at the intersection of the arrows, and this point should be a control node of one the the relations. To define the point, you can use the umlpoint command.

  \begin{tikzpicture}  \umlemptyclass{A1}  \umlemptyclass[x=4, y=-1]{A2}  \umlemptyclass[y=-2]{A3}  \umlassoc[geometry=-|-, name=assoc]{A1}{A2}  \umluniaggreg[geometry=-|-, weight=0.3]{A3}{A2}  \umlpoint{assoc-4}  \end{tikzpicture}


2.2.12 Advanced styling of a relation

On a relation, you can use every TikZ option, thickness options for instance:

  \begin{tikzpicture}  \umlemptyclass{A1}  \umlemptyclass[right=3cm of A1]{A2}  \umluniaggreg[anchors=10 and 170]{A1}{A2}  \umluniaggreg[anchors=-10 and -170, very thick]{A1}{A2}  \end{tikzpicture}


2.2.13 N-ary associations

Sometimes, you need to draw a relation between more than two classes, namely a n-ary association. To do so, you have to draw the main node of such a relation, ans link it to every class it has to be linked:

  \begin{tikzpicture}  \umlemptyclass{A}  \umlemptyclass[x=6]{B}  \umlemptyclass[x=3, y=3]{C}  \umlNarynode[x=3, name=naryassoc, below]{label}  \umlassoc[attr1=a|*]{A}{naryassoc}  \umlassoc[mult1=0..1]{B}{naryassoc}  \umlassoc[mult=*]{naryassoc}{C}  \end{tikzpicture}


The umlNarynode command accepts the following options:

pict To use advanced positioning, do not forget to load TikZ library positioning

2.3 Comments / constraints note

A note is a text comment attached to a class or a relation. The umlnote command needs the name of the node as argument:

  \begin{tikzpicture}  \umlemptyclass{A1}  \umlnote[x=3]{A1}{Je suis une note attachee a la classe A1}  \umlnote[x=2,y=-3, width=5cm]{A1}{Je suis une note plus large attachee a la classe A1}  \end{tikzpicture}


Here again, you can give the name of a control node of a relation to attach the note. Giving a name to the relation will be very useful:

  \begin{tikzpicture}  \umlemptyclass{A}  \umlemptyclass[x=4]{B}  \umluniassoc[arg=bb, mult=1, pos=0.95, align=right, name=uniassoc]{A}{B}  \umlnote[x=2,y=-3]{uniassoc-1}{Je suis une note attachee a la relation uniassoc}  \end{tikzpicture}


Notes have 2 uses: comments and contraints (generally in OCL format).

The umlnote command has the following options:

x, y
These 2 options define the coordinates of the note.
This option defines the width of the note. For TikZ users, it encapsulates the text width option
weight, arm, anchor1, anchor2, anchors
These options has the same behavior as for umlrelation, arm being equivalent to arm1, namely attached to the note.
  \begin{tikzpicture}  \umlemptyclass{A}  \umlemptyclass[x=4]{B}  \umluniassoc[arg=bb, mult=1, pos=0.95, align=right, name=uniassoc]{A}{B}  \umlnote[y=-3, geometry=|-|, anchor1=70, arm=0.5cm]{uniassoc-1}{Je suis une note attachee a la relation uniassoc}  \end{tikzpicture}


For a note, you can also use the geometry option, as for umlrelation. Default value is –. For other values, aliases have been defined: umlHVnote, umlVHnote, umlHVHnote and umlVHVnote.

pict For each of these aliases, the geometry option is forbidden.

2.4 Association class

Drawing an association class is very easy with TikZ-UML . It is just a class and a dependency between this class and a built point of another relation. The umlassocclass makes it for you:

  \begin{tikzpicture}  \umlemptyclass{A}  \umlemptyclass[x=6, y=-3]{B}    \umlHVassoc[name=assoc, attr=t|*, pos=1.9]{A}{B}  \umlassocclass[geometry=|-|,x=2, y=-3]{C}{assoc-1}{}{}  \end{tikzpicture}


As the command is like a class and a relation, you can use the following options: x, y, width (default is 10ex), type (default is class), template coming from the umlclass environment, and name, geometry (default is - -), weight, arm, anchor1, anchor2, and anchors coming from the umlrelation command.

2.5 Advanced features for positioning

umlpackage, umlclass and their aliases, umlnote and umlassocclass can accept every option key defined for nodes in TikZ . In this section, you will see how some of them can be used for advanced features.

2.5.1 Horizontal and vertical alignment

In a class diagram, classes have different width and height. For a graphical purpose, you may want to align them horizontally or vertically. Let’s take the following example:

  \begin{tikzpicture}  \umlsimpleclass{A}  \umlemptyclass[x=-2, y=-3]{B}  \umlclass[x=2, y=-3]{C}{i : int \\ r : double}{}  \umlVHVinherit{B}{A}  \umlVHVinherit{C}{A}  \end{tikzpicture}


The y coordinate defines the center of the class node. It will be better in this example to have classes B and C top-aligned. A solution is to define manually the y value for C, but it is not very convenient. You may prefer use the anchor option. If you specify anchor=north, the y coordinate will define the top center anchor of the node, instead of the center. You may take a look at the differences between both codes.

  \begin{tikzpicture}  \umlsimpleclass{A}  \umlemptyclass[x=-2, y=-2, anchor=north]{B}  \umlclass[x=2, y=-2, anchor=north]{C}{i : int \\ r : double}{}  \umlVHVinherit[arm2=-1.2cm]{B}{A}  \umlVHVinherit[arm2=-1.2cm]{C}{A}  \end{tikzpicture}


In a similar way, you may use anchor=east to right align classes, anchor=west to left align classes or anchor=south to bottom align classes.

For empty packages, association classes and notes, you can also use the mechanism.

2.5.2 Relative positioning

Using the x-y coordinate system may be very hard in big diagrams, when you have to change position of elements in order to fit the diagram to the page. Relative positioning may be useful in this case, namely advanced syntax of options above, left, below, right, above left, below left, below right and above right provided by the TikZ library positioning.

Let’s take the previous example, you can define B by its cordinates (-2,-2) or by saying that B is 2cm below and 2cm left of A. You can also define C by saying it is 4cm right of B. Notice that because of the top alignment of B and C, the latter is defined 4cm right of B.north.

  \begin{tikzpicture}  \umlsimpleclass{A}  \umlemptyclass[below left=2cm and 2cm of A, anchor=north]{B}  \umlclass[right=4cm of B.north, anchor=north]{C}{i : int \\ r : double}{}  \umlVHVinherit[arm2=-1.2cm]{B}{A}  \umlVHVinherit[arm2=-1.2cm]{C}{A}  \end{tikzpicture}


For empty packages, association classes and notes, you can also use the mechanism.

2.6 To change preferences

Thanks to the tikzumlset command, you can change default preferences for packages, classes and notes. The available options are:

allows you to set default text color for every drawn object (=black by default),
allows you to define default edge color for every drawn object (=black by default),
fill class:
allows you to define the default background color of a class node (=yellow!20 by default),
fill template:
allows you to define the default background color of a template node (=yellow!2 by default),
fill package:
allows you to define the default background color of a package (=blue!20 by default),
fill note:
allows you to define the default background color for a note (=green!20 by default),
allows you to define the default font style for every drawn object (=\small by default).
allows you to set the default first coordinate (=0 by default)
allows you to set the default second coordinate (=0 by default)
package type:
allows you to set the default package type (=package by default)
class width:
allows you to set the default class width (=10ex by default)
simple interface width:
allows you to set the default width of a simple interface (=4ex by default)
class type:
allows you to set the default class type (=class by default)
narynode width:
allows you to set the default n-ary node width (=6ex by default)
relation geometry:
allows you to set the default geometry of a relation (=– by default)
relation angle1:
allows you to set the default angle1 of a relation (=-30 by default)
relation angle2:
allows you to set the default angle2 of a relation (=30 by default)
relation loopsize:
allows you to set the default loopsize of a relation (=3em by default)
relation weight:
allows you to set the default weight of a relation (=0.5 by default)
relation pos1:
allows you to set the default pos1 of a relation (=0.2 by default)
relation pos2:
allows you to set the default pos2 of a relation (=0.8 by default)
relation pos stereo:
allows you to set the default pos stereo of a relation (=0.5 by default)
note width:
allows you to set the default note width (=3cm by default)

Furthermore, relation commands has the style option taking a TikZ style name as value.

Let’s see the definition of the umlinherit command:

\tikzstyle{tikzuml inherit style}=[color=\tikzumlDefaultDrawColor, -open triangle 45]  \newcommand{\umlinherit}[3][]{\umlrelation[style={tikzuml inherit style}, #1]{#2}{#3}}

You can easily define a command on this model by defining a particular style.

2.7 Examples

2.7.1 Example from introduction, step by step

We will now build step by step the picture seen in introduction to understand the behavior of each used command.

Definition of packages p, sp1 and sp2

The package p is placed at (0,0) (default), and the sub-packages sp1 and sp2 respectively at (0,0) and (10,-6).

  \begin{tikzpicture}  \begin{umlpackage}{p}
    \umlvirt{setB(b : B) : void} \\ getB() : B}  \end{umlpackage}


Definition of classes A, B, C, D and their attributes and operations

The class A is placed at (0,0) in the sub-package sp1 and has a template parameter: T. The class B is placed 3 units below A, still in the sub-package sp1. The interface C is placed at (0,0) in the sub sp2. The class D is placed at (2,-11) in the package p.

Class A has two attributes. Class B has one attribute and two operations (one is virtual). Class C has two attributes. Classe D has one attribute.

  \begin{tikzpicture}  \begin{umlpackage}{p}
  \begin{umlpackage}{sp1}  \umlclass[template=T]{A}{    n : uint \\ t : float  }{}  \umlclass[y=-3]{B}{    d : double  }{
    \umlvirt{setB(b : B) : void} \\ getB() : B}  \end{umlpackage}
  \begin{umlpackage}[x=10,y=-6]{sp2}  \umlinterface{C}{    n : uint \\ s : string
  \end{umlpackage}  \umlclass[x=2,y=-10]{D}{    n : uint


Definition of relations

We define an association between classes C and B, a unidirectional composition between classes D and C, an import relation named "import" between sub-packages sp2 and sp1 (with modification of anchors), a recursive aggregation on class D and an inheritnce between classes D and B. On thses relations, we will specify argument names and multiplicities. You can notice the value given to place these elements on each arrow according to the geometry.


  \end{umlpackage}    \umlassoc[geometry=-|-, arg1=tata, mult1=*, pos1=0.3, arg2=toto, mult2=1, pos2=2.9, align2=left]{C}{B}  \umlunicompo[geometry=-|, arg=titi, mult=*, pos=1.7, stereo=vector]{D}{C}  \umlimport[geometry=|-, anchors=90 and 50, name=import]{sp2}{sp1}


Definition of notes

We add a note attached to class B and a note attached to the import relation.


  \umlaggreg[arg=tutu, mult=1, pos=0.8, angle1=30, angle2=60, loopsize=2cm]{D}{D}  \umlinherit[geometry=-|]{D}{B}  \umlnote[x=2.5,y=-6, width=3cm]{B}{Je suis une note qui concerne la classe B}  \umlnote[x=7.5,y=-2]{import-2}{Je suis une note qui concerne la relation dimport}  \end{tikzpicture}


Setting style

We illustrate the use of the tikzumlset command by changing colors associated to class and font. We can also change colors of a given class with draw, text and fill options.

\tikzumlset{fill class=red!20, fill template=violet!10, font=\bfseries\footnotesize}  \begin{tikzpicture}  ...  \umlclass[x=2,y=-11, fill=orange!50, draw=white, text=red]{D}{    n: uint    }{}  ...  \end{tikzpicture}


2.7.2 To define a specialization of a class

A specialization of a classe is an inheritance from a templace class in which one of the template parameters is defined. To draw this relation, you will use the umlreal command , and its stereo option:

  \begin{tikzpicture}  \umlemptyclass[template=T]{A}  \umlemptyclass[template={T,U}, x=5, y=-2]{B}  \umlreal[stereo={U $\rightarrow$ int}]{A}{B}  \end{tikzpicture}


2.8 Priority rules of options and known bugs

  1. The geometry option has always the priority on the others options. It means for instance that if it has a non-default value, then angle1, angle2 and loopsize options, defining recursive relations, will be ignored.
  2. As far as a template class is concerned, there are cases in which a relation about it will not be drawn correctly, as in the picture below, where the aggregation symbol is hidden by the template parameter:
      \begin{tikzpicture}  \umlemptyclass[template=T]{A}  \umlemptyclass[x=4,y=2]{B}  \umluniaggreg{A}{B}  \end{tikzpicture}


    To solve this problem, you van link the arrow between the template part of class A and class B, by adding the suffix -template and adjusting the start anchor (the -30 value is correct here):

      \begin{tikzpicture}  \umlemptyclass[template=T]{A}  \umlemptyclass[x=4,y=2]{B}  \umluniaggreg[anchor1=-30]{A-template}{B}  \end{tikzpicture}


  3. If you define a class with a name having the: character in it – typically when you give the namespace of the class – it may have a conflict with the french (or frenchb or francais) option of the babel package. Indeed, these options add a white-space before the: character if the writer forgot it, that is a problem for the access operator::. If we take the example of class definition, we should obtain:
      \begin{tikzpicture}  \umlclass{namespace::A}{    + n : uint \\ \umlstatic{-- i : int} \\ \# r : const float    }{    + setA(i : int) : void \\ \umlvirt{\# getA() : A}  }  \end{tikzpicture}

    SVG-Viewer needed.

    The solution is to use a specific macro given by these options of babel package you have to use in the preamble of your document:

  4. The automatic placement of argument names and multiplicity on a relation can be surprising when you can to deactivate it. Let’s take the example shown in introduction. If we focus on the association relation and its attributes toto and tata, toto is above, tata is below. If we justify to the right the tata attribute (and change its position to 0.1), positions of tata and its multiplicity exchange.