Debian Codegraph @

texmf-dist/doc/latex/ltmermaid/mermaid-doc.tex

raw
1 \documentclass[11pt,a4paper]{article}
2 \usepackage{array}
3 \usepackage{tabularx}
4 \usepackage{float}
5 \usepackage{mermaid}
6 \usepackage[hidelinks]{hyperref}
7 \usepackage{bxghost}
8 \usepackage{bxtexlogo}
9 \usepackage{listings}
10 \usepackage{xcolor}
11
12 \lstset{
13 basicstyle={\ttfamily},
14 identifierstyle={\small},
15 commentstyle={\small\itshape},
16 keywordstyle={\small\bfseries},
17 ndkeywordstyle={\small},
18 stringstyle={\small\ttfamily},
19 frame={tb},
20 breaklines=true,
21 columns=[l]{fullflexible},
22 numbers=left,
23 xrightmargin=0pt,
24 xleftmargin=1.5em,
25 numberstyle={\scriptsize},
26 stepnumber=1,
27 numbersep=0.5em,
28 lineskip=-0.5ex
29 }
30
31 \makeatletter
32 \def\m@syu@space@char{^^`}
33
34 \def\m@syu@string#1{%
35 \@tfor\m@syu@member:=#1\do{%
36 \ifx\m@syu@member\m@syu@space@char
37 \textvisiblespace
38 \else
39 \ifx\m@syu@member\empty
40 \textvisiblespace
41 \else\m@syu@member\fi
42 \fi}%
43 }
44
45 \def\m@syu@removespace#1{%
46 \def\m@syu@removedspace{}%
47 \@tfor\m@syu@member:=#1\do{%
48 \ifx\m@syu@member\empty
49 \edef\m@syu@removedspace{\m@syu@removedspace\m@syu@member\m@syu@space@char}%
50 \else
51 \edef\m@syu@removedspace{\m@syu@removedspace\m@syu@member}%
52 \fi}%
53 }
54
55 \newcommand{\cmd}[2][\texttt]{#1{\symbol{92}\m@syu@string{#2}}}
56 \makeatother
57
58 \newcolumntype{L}{>{\raggedright\arraybackslash}X}
59
60 \title{The \texttt{mermaid} package (\texttt{mermaid.sty})}
61 \author{安藤 遼哉(Ryoya Ando)\\\url{https://ryoya9826.github.io/}}
62 \date{\today}
63
64 \begin{document}
65 \maketitle
66
67 \begin{abstract}
68 \noindent
69 \texttt{mermaid.sty} invokes the Mermaid CLI via \texttt{shellesc} and \verb|\write18|, renders Mermaid diagrams during a \LaTeXe{} run, and embeds them as PDF.
70 It can be used with engines such as \upLaTeX, \pdfLaTeX, and \LuaLaTeX.
71 Rendering Mermaid diagrams requires the Mermaid CLI (\texttt{mmdc}, etc.) and headless Chromium (Puppeteer).
72 Compile with shell escape enabled (\verb|-shell-escape|).
73 \end{abstract}
74
75 \section{Operating conditions}
76
77 \noindent
78 \begin{tabularx}{\textwidth}{@{}>{\raggedright\arraybackslash}p{0.22\textwidth}L@{}}
79 \hline
80 \textbf{Item} & \textbf{Condition} \\
81 \hline
82 Format &
83 \LaTeXe \\
84 Shell escape &
85 \texttt{-shell-escape} (invoke the CLI via \texttt{shellesc} / \verb|\write18|) \\
86 External tool &
87 Mermaid CLI (\texttt{mmdc}) \\
88 \hline
89 \end{tabularx}
90
91 \medskip
92
93 If the Mermaid CLI is not present in your environment, \texttt{mermaid.sty} does not auto-install \texttt{mmdc} or similar.
94 A typical Mermaid CLI installation uses \textbf{Node.js} with \texttt{npm} (or \texttt{npx}).
95 If you set the package option \texttt{Renderer} to a command that includes \textbf{\texttt{npx -y}}, for example
96 \texttt{npx -y @mermaid-js/mermaid-cli \ldots}, then \texttt{npx} may fetch and install packages as needed
97 (\textbf{network access} may be required).
98
99 When compiling, unless the \texttt{Renderer} option names another command, the default renderer \texttt{mmdc}
100 must be visible in the same environment (if you set \texttt{Renderer}, that command must be visible instead).
101 The Mermaid CLI depends on headless Chromium (Puppeteer); see the Mermaid CLI documentation for details.
102
103 Log output uses \texttt{\string\typeout} with the \texttt{[mermaid]} prefix. If something goes wrong, check the \texttt{.err} files under \texttt{mermaid/}.
104
105 \section{License and source}
106
107 \LaTeX{} Project Public License (LPPL) 1.3c or later.\\
108 Source and issues: \url{https://github.com/ryoya9826/ltMermaid}
109
110 \section{Usage}
111
112 \subsection{Minimal document}
113
114 \begin{lstlisting}
115 \documentclass{article}
116 \usepackage{mermaid}
117
118 \begin{document}
119 \begin{mermaid}
120 flowchart LR
121 A --> B
122 \end{mermaid}
123 \end{document}
124 \end{lstlisting}
125
126 Compile example:
127 \begin{verbatim}
128 pdflatex -shell-escape yourfile.tex
129 lualatex -shell-escape yourfile.tex
130 uplatex -shell-escape yourfile.tex && dvipdfmx yourfile.dvi
131 \end{verbatim}
132
133 \subsection{Layout adjustments (optional)}
134
135 By default, graphics included via \texttt{adjustbox} are scaled. Example:
136 \begin{lstlisting}
137 \MermaidAdjustBoxOpts{max width=0.8\linewidth,center}
138 \MermaidAdjustBoxOpts{max width=0.9\linewidth,center,valign=T}
139 \end{lstlisting}
140
141 \subsection{Beamer}
142
143 The package can also be used with the \texttt{beamer} class. Every frame that contains a \texttt{mermaid}
144 environment must use the \texttt{fragile} option (the environment depends on \texttt{fancyvrb}).
145 Example: \verb|\begin{frame}[fragile]{Figure}|.
146
147 \begin{lstlisting}
148 \documentclass{beamer}
149 \usepackage{mermaid}
150
151 \begin{document}
152 \begin{frame}[fragile]{Mermaid}
153 \begin{mermaid}
154 flowchart LR
155 A --> B
156 \end{mermaid}
157 \end{frame}
158 \end{document}
159 \end{lstlisting}
160
161 \section{Package options (optional)}
162
163 {\small
164 \begin{itemize}
165 \item \textbf{\texttt{Renderer}}:
166 Prefix for the renderer command. If omitted, \texttt{mmdc} is the default.
167 You can specify it explicitly, for example \texttt{npx -y @mermaid-js/mermaid-cli}.
168 \end{itemize}
169 \par}
170
171
172 \section{User commands}
173
174 {\small
175 \begin{itemize}
176 \item \textbf{\cmd{MermaidRendererOptions}\texttt{\{...\}}:}
177 Additional CLI arguments before \texttt{-i} / \texttt{-o} (when PDF fit is enabled, merged after the built-in \texttt{-f}).
178 \item \textbf{\cmd{MermaidNoPdfFit}:}
179 Disables \texttt{-f} / \texttt{--pdfFit} for \texttt{mmdc} (by default this is \emph{enabled}).
180 \item \textbf{\cmd{MermaidAdjustBoxOpts}\texttt{\{...\}}:}
181 Full list of \texttt{adjustbox} keys around \cmd{includegraphics} (default:
182 \texttt{max width=0.9}\cmd{linewidth}\texttt{,center}).
183 \item \textbf{\cmd{MermaidGraphicsOpts}\texttt{\{...\}}:}
184 Additional keys for \cmd{includegraphics} (rotation, \texttt{trim}, etc.).
185 Width is usually set with \cmd{MermaidAdjustBoxOpts}.
186 \item \textbf{\cmd{mermaidfile}\texttt{[<includegraphics options>]\{filename\}}:}
187 Render a Mermaid diagram from an external file (\texttt{.mmd}) instead of inline code. The diagram is rendered and included just like the \texttt{mermaid} environment, and an optional argument may be used to pass keys directly to \cmd{includegraphics}.
188 \end{itemize}
189 \par}
190
191 \subsection{Including diagrams from files}
192
193 You can use \cmd{mermaidfile}\texttt{[<includegraphics options>]\{filename\}} to render a Mermaid diagram from an external file. Example:
194
195 \begin{lstlisting}
196 \mermaidfile[width=0.8\linewidth]{diagram-001.mmd}
197 \end{lstlisting}
198
199 The optional argument is forwarded directly to \cmd{includegraphics}, so valid keys include \texttt{width}, \texttt{height}, \texttt{angle}, \texttt{trim}, \texttt{keepaspectratio}, and similar graphics options.
200
201 \section{Output files}
202
203 Intermediate \texttt{.mmd} files and rendered \texttt{.pdf} files are written under the \texttt{mermaid/}
204 directory in the directory from which you run the compiler. File names are of the form
205 \texttt{\jobname-mermaid-1.mmd}, \texttt{\jobname-mermaid-1.pdf}, \ldots, using \texttt{\jobname} (the job name) and a running index per diagram.
206
207 \section{Diagram examples}
208
209 \textbf{Left:} source typed into the \texttt{mermaid} environment.
210 \textbf{Right:} rendered result.
211
212 \begin{figure}[H]
213 \begin{minipage}[t]{0.42\textwidth}
214 \vspace{0pt}
215 \begin{lstlisting}[basicstyle=\footnotesize\ttfamily]
216 \begin{mermaid}
217 flowchart TB
218 subgraph client["Client tier"]
219 WEB["Browser / SPA"]
220 CLI["CLI / batch"]
221 end
222 subgraph edge["Edge"]
223 GW{{API Gateway}}
224 end
225 subgraph svc["Service tier"]
226 AUTH["Auth"]
227 API["Business API"]
228 WORK["Workers"]
229 end
230 subgraph store["Data"]
231 DB[("PostgreSQL")]
232 CACHE[("Redis")]
233 QUEUE["Job queue"]
234 end
235 WEB --> GW
236 CLI --> GW
237 GW --> AUTH
238 GW --> API
239 API --> WORK
240 API --> DB
241 API --> CACHE
242 WORK --> QUEUE
243 WORK --> DB
244 \end{mermaid}
245 \end{lstlisting}
246 \end{minipage}%
247 \hfill
248 \begin{minipage}[t]{0.54\textwidth}
249 \vspace{0pt}\centering
250 \begin{mermaid}
251 flowchart TB
252 subgraph client["Client tier"]
253 WEB["Browser / SPA"]
254 CLI["CLI / batch"]
255 end
256 subgraph edge["Edge"]
257 GW{{API Gateway}}
258 end
259 subgraph svc["Service tier"]
260 AUTH["Auth"]
261 API["Business API"]
262 WORK["Workers"]
263 end
264 subgraph store["Data"]
265 DB[("PostgreSQL")]
266 CACHE[("Redis")]
267 QUEUE["Job queue"]
268 end
269 WEB --> GW
270 CLI --> GW
271 GW --> AUTH
272 GW --> API
273 API --> WORK
274 API --> DB
275 API --> CACHE
276 WORK --> QUEUE
277 WORK --> DB
278 \end{mermaid}
279 \end{minipage}
280 \caption{Multi-layer architecture (subgraphs and node shapes)}
281 \end{figure}
282
283 \begin{figure}[H]
284 \begin{minipage}[t]{0.42\textwidth}
285 \vspace{0pt}
286 \begin{lstlisting}[basicstyle=\footnotesize\ttfamily]
287 \begin{mermaid}
288 sequenceDiagram
289 autonumber
290 actor U as User
291 participant B as Browser
292 participant A as Auth API
293 participant S as Business API
294 participant D as DB
295 U->>B: Log in
296 B->>+A: POST /token
297 A->>D: Verify user
298 D-->>A: Row
299 A-->>-B: JWT
300 B->>+S: GET /orders (Bearer)
301 S->>A: Validate token
302 A-->>S: Claims
303 S->>D: SELECT
304 D-->>S: Rows
305 S-->>-B: 200 JSON
306 B-->>U: List view
307 \end{mermaid}
308 \end{lstlisting}
309 \end{minipage}%
310 \hfill
311 \begin{minipage}[t]{0.54\textwidth}
312 \vspace{0pt}\centering
313 \begin{mermaid}
314 sequenceDiagram
315 autonumber
316 actor U as User
317 participant B as Browser
318 participant A as Auth API
319 participant S as Business API
320 participant D as DB
321 U->>B: Log in
322 B->>+A: POST /token
323 A->>D: Verify user
324 D-->>A: Row
325 A-->>-B: JWT
326 B->>+S: GET /orders (Bearer)
327 S->>A: Validate token
328 A-->>S: Claims
329 S->>D: SELECT
330 D-->>S: Rows
331 S-->>-B: 200 JSON
332 B-->>U: List view
333 \end{mermaid}
334 \end{minipage}
335 \caption{Sequence diagram (numbering, asynchronous arrows, multiple participants)}
336 \end{figure}
337
338 \begin{figure}[H]
339 \begin{minipage}[t]{0.42\textwidth}
340 \vspace{0pt}
341 \begin{lstlisting}[basicstyle=\footnotesize\ttfamily]
342 \begin{mermaid}
343 stateDiagram-v2
344 [*] --> Draft: Create
345 Draft --> Review: Submit
346 Review --> Draft: Send back
347 Review --> Approved: Approve
348 Approved --> Published: Publish
349 Published --> Archived: End
350 Review --> Rejected: Reject
351 Rejected --> [*]
352 Archived --> [*]
353 \end{mermaid}
354 \end{lstlisting}
355 \end{minipage}%
356 \hfill
357 \begin{minipage}[t]{0.54\textwidth}
358 \vspace{0pt}\centering
359 \begin{mermaid}
360 stateDiagram-v2
361 [*] --> Draft: Create
362 Draft --> Review: Submit
363 Review --> Draft: Send back
364 Review --> Approved: Approve
365 Approved --> Published: Publish
366 Published --> Archived: End
367 Review --> Rejected: Reject
368 Rejected --> [*]
369 Archived --> [*]
370 \end{mermaid}
371 \end{minipage}
372 \caption{State transitions (\texttt{stateDiagram-v2})}
373 \end{figure}
374
375 \section{Revision history}
376
377 \begin{description}
378 \item[Version 1.0c (2026-05-06)] Various bug fixes.
379 \item[Version 1.0b (2026-04-29)] Added \cmd{mermaidfile} support, Makefile build/test targets, and \texttt{l3build} regression testing.
380 \item[Version 1.0a (2026-04-20)] Renamed sample documents.
381 \item[Version 1.0 (2026-04-16)] Stable release.
382 \end{description}
383
384 \end{document}