Debian Codegraph @

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

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