| 1 |
%% Copyright (C) 2018-2025 Oliver Kopp and contributors |
| 2 |
%% |
| 3 |
%% SPDX-License-Identifier: LPPL-1.3c+ |
| 4 |
\NeedsTeXFormat{LaTeX2e}\relax |
| 5 |
\ProvidesPackage{plantuml} |
| 6 |
[2025/05/13 v0.6.0 |
| 7 |
Embed PlantUML diagrams in latex documents.] |
| 8 |
|
| 9 |
% Required by PlantUML LaTeX output |
| 10 |
\RequirePackage{tikz} |
| 11 |
|
| 12 |
% Enable checking for active -shell-escape |
| 13 |
% Source: https://tex.stackexchange.com/a/88620/9075 |
| 14 |
\RequirePackage{pdftexcmds} |
| 15 |
|
| 16 |
% Prepare writing contents of a self-defined environment to a file |
| 17 |
% Source: https://tex.stackexchange.com/a/130298/9075 |
| 18 |
\RequirePackage{fancyvrb} |
| 19 |
|
| 20 |
\RequirePackage{l3keys2e} |
| 21 |
\RequirePackage{xparse} |
| 22 |
|
| 23 |
\RequirePackage{ifthen} |
| 24 |
|
| 25 |
\RequirePackage{adjustbox} |
| 26 |
|
| 27 |
\newcounter{PlantUmlFigureNumberSVG} |
| 28 |
\def\UMLcountUp{\stepcounter{PlantUmlFigureNumberSVG} \def\PlantUMLJobname{PlantUML\thePlantUmlFigureNumberSVG}} |
| 29 |
|
| 30 |
% Code snippet from Joseph Wright (https://ctan.org/home/josephwright) |
| 31 |
% Source: https://tex.stackexchange.com/questions/418670/avoid-quotation-marks-when-using-jobname-or-currfilename |
| 32 |
% according to https://tex.stackexchange.com/users/73/joseph-wright, his code on the site is placed in the public domain |
| 33 |
\newcommand*{\myjobname}{} |
| 34 |
\newcommand*{\setmyjobname}{\expanded{\noexpand\setmyjobnameaux |
| 35 |
\jobname"\jobname"\relax}} |
| 36 |
\newcommand*{\setmyjobnameaux}{} |
| 37 |
\def\setmyjobnameaux#1"#2"#3\relax{\def\myjobname{#2}} |
| 38 |
\setmyjobname |
| 39 |
|
| 40 |
\def\PlantUMLJobname{\myjobname\thePlantUmlFigureNumberSVG} |
| 41 |
|
| 42 |
\ExplSyntaxOn |
| 43 |
\keys_define:nn { plantuml } { |
| 44 |
output .choices:nn = { |
| 45 |
{latex, png, svg} |
| 46 |
{ \tl_gset:NV \l_plantuml_mode \l_keys_choice_tl } |
| 47 |
}, |
| 48 |
output .initial:n = latex |
| 49 |
} |
| 50 |
\ProcessKeysOptions { plantuml } |
| 51 |
\ExplSyntaxOff |
| 52 |
|
| 53 |
%hint from https://tex.stackexchange.com/a/86355/9075 |
| 54 |
\makeatletter |
| 55 |
\def\maxwidth#1{\ifdim\Gin@nat@width>#1 #1\else\Gin@nat@width\fi} |
| 56 |
\makeatother |
| 57 |
|
| 58 |
\ExplSyntaxOn |
| 59 |
\let\PlantUmlMode\l_plantuml_mode |
| 60 |
\ExplSyntaxOff |
| 61 |
|
| 62 |
\newcounter{PlantUmlFigureNumber} |
| 63 |
|
| 64 |
\ifluatex |
| 65 |
\RequirePackage{luacode} |
| 66 |
\directlua{ |
| 67 |
local lfs = require("lfs") |
| 68 |
local currentdir = lfs.currentdir():gsub("\\", "/") |
| 69 |
tex.sprint("\\newcommand\\CurrentDirectory{" .. currentdir .. "/}") |
| 70 |
} |
| 71 |
\else |
| 72 |
\RequirePackage[usefamily=bash]{pythontex} |
| 73 |
\newcommand{\CurrentDirectory}{} |
| 74 |
\fi |
| 75 |
|
| 76 |
\makeatletter |
| 77 |
\ifcase\pdf@shellescape |
| 78 |
\message{No shell escape. PlantUML cannot be called. Start pdflatex/lualatex with -shell-escape.} |
| 79 |
\newenvironment{plantuml}{% |
| 80 |
No shell escape. PlantUML cannot be called. Start pdflatex/lualatex with -shell-escape. |
| 81 |
}{} |
| 82 |
\or |
| 83 |
\ifluatex |
| 84 |
\directlua{ |
| 85 |
local plantUmlJar = os.getenv("PLANTUML_JAR") |
| 86 |
if not plantUmlJar then |
| 87 |
texio.write_nl("Environment variable PLANTUML_JAR not set.") |
| 88 |
end |
| 89 |
} |
| 90 |
\fi |
| 91 |
\NewDocumentEnvironment{plantuml}{}{% |
| 92 |
\VerbatimOut{"\CurrentDirectory\PlantUMLJobname-plantuml.txt"}} |
| 93 |
{% |
| 94 |
\endVerbatimOut |
| 95 |
\ifluatex |
| 96 |
\directlua{ |
| 97 |
local jobname=\luastring{\PlantUMLJobname} |
| 98 |
local plantUmlMode=\luastring{\PlantUmlMode} |
| 99 |
require("plantuml.lua") |
| 100 |
convertPlantUmlToTikz(jobname, plantUmlMode) |
| 101 |
} |
| 102 |
\else |
| 103 |
\stepcounter{PlantUmlFigureNumber} |
| 104 |
%TODO: Execute python here |
| 105 |
\typeout{*** plantuml only works with lualatex ***} |
| 106 |
\fi |
| 107 |
\ifthenelse{\equal{\PlantUmlMode}{latex}}{ |
| 108 |
\begin{adjustbox}{max width=\linewidth} |
| 109 |
\input{\PlantUMLJobname-plantuml.tex} |
| 110 |
\end{adjustbox} |
| 111 |
}{ |
| 112 |
\includegraphics[width=\maxwidth{\textwidth}]{\PlantUMLJobname-plantuml.\PlantUmlMode} |
| 113 |
\UMLcountUp |
| 114 |
} |
| 115 |
} |
| 116 |
\or |
| 117 |
\message{Restricted shell escape. PlantUML cannot be called. Start pdflatex/lualatex with -shell-escape.} |
| 118 |
\newenvironment{plantuml}{% |
| 119 |
Restricted shell escape. PlantUML cannot be called. Start pdflatex/lualatex with -shell-escape. |
| 120 |
}{} |
| 121 |
\fi |
| 122 |
\makeatother |