14

I want to create diagrams. Preferable in LaTeX. Normally, I use the PlantUML language.

Columnist
  • 145
  • 1
    You have at least two options: 1) Build your diagrams with the software of your choice, save them as PDF, PNG and include them via https://en.wikibooks.org/wiki/LaTeX/Importing_Graphics. 2) See https://tex.stackexchange.com/questions/875/typesetting-uml-class-diagrams. 3)? I do not know whether PlantUML supports a TeX, or TikZ output (which probably would be preferable as this leads to typographic consistency). – CampanIgnis Apr 23 '18 at 23:07
  • You also have the uml ans pst-umlwhich might help (I don't about plantUML). Both rely on pstricks. Also a metauml package, which relies on metapost. – Bernard Apr 23 '18 at 23:14
  • 3
    From http://plantuml.com/latex: Starting from version 7997, PlantUML allows to generate diagrams into LaTeX, thanks to Tikz package. Note that this is still beta, and many things don't probably work. Since we do not want to spend time on features not used, we will wait for users to report bugs here. You just have to use the flag -tlatex with the command line, or format="latex" with the ANT task. – Marijn Apr 24 '18 at 15:43

4 Answers4

13

It is possible to use the PlantUML language direcly inside LaTeX using the plantuml package.

Here is a minimal example:

\documentclass{scrartcl}
\usepackage{plantuml}
\begin{document}
\begin{plantuml}
  @startuml
  Alice -> Bob: Hello
  Alice <- Bob: Hi!
  @enduml
\end{plantuml}
\end{document}

For building LaTeX with PlantUML the PLANTUML_JAR environment variable has to be set and Java must be installed.

To build this document, run lualatex --shell-escape documentname

The result should look like this:

Result with a PlantUML diagram

A more detailed example can be found here: https://koppor.github.io/plantuml/

Prerequisites

# to get tikz, install pgf
tlmgr install pgf
tlmgr install koma-script
tlmgr install xkeyval
tlmgr install adjustbox
tlmgr install collectbox
tlmgr install luacode
tlmgr install luatexbase
2

I use the website https://www.planttext.com/ to export to/download as SVG (which btw includes the plantuml code as XML comment) and after that save as PDF with inkscape. This can be used with \includegraphics

2

You could also write the plantuml diagrams localy and convert them via plantuml -teps your-file to lossless eps files. These can be directly included in latex via the \includegraphics command.

This way you do not have to go the way via inkscape.

Ohmen
  • 123
1

I have worked out a solution that works with pdflatex as well. It produces vector graphics. I tested it with Ubuntu Linux 20.04 LTS and TeX Live. Should work with macOS as well.

My code requires, that you have installed:

  • PlantUML (thus, also Java)
  • Inkscape (converting svg to pdf)

Due to calling external commands, you have to use the -shell-escape option to compile:

pdflatex -shell-escape my-file.tex

Depending of how you installed PlantUML, you might have to call it like this:

\immediate\write18{java -jar /home/NAME/Downloads/plantuml.jar -tsvg #1.txt}

... instead of ...

\immediate\write18{plantuml -tsvg #1.txt}

Here is the full code as minimal example:

\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx} % enables us to use graphics
\usepackage{fancyvrb} % enables us to write to disk
\usepackage{float}    % enables us to use [H] for figures

% Define an verbatim environment for PlantUML: \newenvironment{plantuml}[1]{\VerbatimOut{#1.txt}}{\endVerbatimOut}

% Process a PlantUML code: \newcommand{\process}[3]{% % Call PlantUML to produce a svg vector graphcis: \immediate\write18{plantuml -tsvg #1.txt} % Call Inkscape to convert the svg to a pdf (pdflatex cannot use svg): \immediate\write18{inkscape #1.svg --export-filename=#1.pdf} % Include the pdf: \begin{figure}[H] \includegraphics[width=#2]{#1.pdf} \caption{#3} \end{figure} % Remove all intermediate files: \immediate\write18{rm #1.txt #1.svg #1.pdf} }

\begin{document}

% Note: timeline1 gets used as filename. % You must re-use this name for the % corresponding \process command! \begin{plantuml}{timeline1} @startgantt [Prototype design] lasts 15 days [Test prototype] lasts 10 days @endgantt \end{plantuml} \process{timeline1}{0.8\textwidth}{The proposed timeline.}

\begin{plantuml}{activity1} @startuml :Hello world; :This is on defined on several lines; @enduml \end{plantuml} \process{activity1}{5cm}{An activity diagram.}

\end{document}

I hope this is helpful to someone.

  • 1
    It's nice that the PlantUML source goes into LaTeX. Configuring the environment properly is always complex. I have configured PlantUML on Windows 10 to generate PDF -tpdf following https://plantuml.com/pdf and it doesn't require inkscape. Your solution could work with that, too. – Fuhrmanator Jul 28 '20 at 15:37