Creating UML Sequence Diagrams with TikZ in LaTeX

Update: If you are interested in getting a concise intro to LaTeX along with my tips on best practices, checkout my course (for just $12.99) on Udemy here.

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.

[caption id=”attachment_743” align=”aligncenter” width=”375”] Sequence Diagram using TikZ (click to enlarge)[/caption]

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.

[sourcecode language=”latex”]
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}
[/sourcecode]

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.