Design, Geek stuff, LaTeX, Tutorials

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.

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.

Advertisements

14 thoughts on “Creating UML Sequence Diagrams with TikZ in LaTeX”

  1. Nice! What are your modifications to the package code? I recommend to make redefinitions in your preamble or in your own .sty file, using renewcommand etc. instead of changing the original package file, so that a LaTeX document looks the same on all computers, with all TeX distributions and on all operating systems.

    1. Thanks Stefan. You are absolutely right. Unfortunately, my skills with LaTeX are well below the level required to fully understand the pgf-umlsd package code. So, I only managed to change some parts of the code that I could understand. It includes some stuff like manually positioning the labels to ‘near end’ or ‘near start’ instead of the default ‘midway’. However, the changes are kind of unstable.

  2. Nice blog right here! Additionally your site quite a bit up fast! What host are you using? Can I get your associate hyperlink to your host? I want my web site loaded up as fast as yours lol

  3. Podemos decorar superficies con papel usando la técnica del decoupage, que tratamos extensamente en su sección correspondiente.

  4. I noticed your page’s ranking in google’s search results is very low.
    You are loosing a lot of traffic. You need hi
    authority backlinks to rank in top 10. I know – buying
    them is too expensive. It’s better to own them. I know how to do that,
    simply google it:
    Polswor’s Backlinks Source

Comments are closed.