recluze

Nauman (recluze) on computing, philosophy and more ...

Creating UML Sequence Diagrams With TikZ in LaTeX

I’ve been working on my LaTeX skills for some time. The goal is to move towards an all-latex solution for writing research papers, slide sets and other documents. I’m almost there. An important goal was to be able to create all sorts of figures within LaTeX. (Well, originally, the goal was to use open source softwares to create them but it turns out that LaTeX is really good at this stuff.) The package that I’m using for graphics creation is TikZ. Here we’ll cover how we can create sequence diagrams using TikZ and a plugin package.

Here’s what we’re planning on creating.

Sequence Diagram using TikZ (click to enlarge)

First, you need to get the pgf-umlsd.sty file from over here and put it in a folder. Then, create your minimal working example (main document) using the following code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,shadows}
\usepackage{pgf-umlsd}

\begin{document}

\begin{sequencediagram}
\newthread[white]{u}{User}
\newinst[3]{b}{Browser}
\newinst[3]{t}{TPM}
\newinst[3]{p}{TTP}

\begin{call}{u}{Init()}{b}{}
\end{call}

\begin{call}{u}{AIKAuthSecret}{b}{}

  \mess{b}{verifyAIKAuthSecret}{t}

  \begin{call}{b}{get AIK$_{pub}$}{t}{AIK$_{pub}$}
  \end{call}

\end{call}
  \begin{sdblock}{Loop}{}

    \begin{call}{u}{Do Something}{p}{AIK$_{pub}$}
    \end{call}
  \end{sdblock}
\end{sequencediagram}

\end{document}

As you can see, the pgf-umlsd package is really awesome. You first create a new thread using the syntax \newthread[color]{variable}{ClassName}. Then, you create instances using \newinst[distance]{variable}{InstanceName}. Afterwards, use call environment to specify calls. All you need to do is specify the caller, the message label, recipient and return label. Messages are similar and can be done through the \mess command. You can insert blocks using the sdblock environment. All it needs is a label and a description. A block will automatically surround everything within this environment. Oh, and calls can be nested.

If you’re like me and don’t like your object names underlined, you can pass the [underline=false] option to the pgf-umlsd package.

p.s. Your output may be a little bit different from mine because I modified the package file to suit my personal liking — just a bit though.

Comments