| 1 | # LaTeX Packages to Embed Mermaid Diagrams (`mermaid` / `ltmermaid`) |
| 2 | |
| 3 | Embed [Mermaid](https://mermaid.js.org/) diagrams in LaTeX. Diagram sources are written to disk, and the [Mermaid CLI](https://github.com/mermaid-js/mermaid-cli) (`mmdc`) renders vector PDFs, which are then included using `\includegraphics`. |
| 4 | |
| 5 | Originally, `ltmermaid` was created exclusively for LuaLaTeX. Subsequently, the `mermaid` package was developed as a LuaTeX-independent version to support other engines. Therefore, there are two packages provided in this repository: |
| 6 | |
| 7 | - `mermaid.sty` (`mermaid`) |
| 8 | The implementation to be used with pdfLaTeX / XeLaTeX / upLaTeX / LuaLaTeX, etc. It invokes the Mermaid CLI via the `shellesc` package (or directly via `\write18`). No Lua interpreter is required. |
| 9 | - `ltmermaid.sty` (`ltmermaid`) |
| 10 | An implementation dedicated to LuaLaTeX that provides the same macros (the `Renderer` option, `\Mermaid...` macros, and the `mermaid` environment) as the `mermaid` package above. The CLI is invoked via Lua's `os.execute`, and the package handles LuaLaTeX-specific behavior such as outputting to `mermaid/` under `-output-directory` / `TEXMF_OUTPUT_DIRECTORY`. |
| 11 | |
| 12 | Author: Ryoya Ando — [https://ryoya9826.github.io/](https://ryoya9826.github.io/) |
| 13 | License: [LPPL](https://www.latex-project.org/lppl.txt) version 1.3c |
| 14 | Source & issues: [https://github.com/ryoya9826/ltMermaid](https://github.com/ryoya9826/ltMermaid) |
| 15 | Contributors: [CONTRIBUTORS.md](CONTRIBUTORS.md) |
| 16 | |
| 17 | --- |
| 18 | |
| 19 | ## Requirements (`mermaid`) |
| 20 | |
| 21 | | Item | Requirement | |
| 22 | | ---------------- | -------------------------------------------------------------------------------- | |
| 23 | | TeX engine | upLaTeX / pdfLaTeX / LuaLaTeX, etc. (Lua interpreter is not required) | |
| 24 | | LaTeX format | LaTeX2e | |
| 25 | | Shell escape | `-shell-escape` (the CLI is invoked via `shellesc` / `\write18`) | |
| 26 | | External tool | Mermaid CLI (`mmdc`) | |
| 27 | |
| 28 | If the CLI is missing in the environment, this package does not install it automatically. If you pass a command containing `npx -y` (e.g., `npx -y @mermaid-js/mermaid-cli`) to `Renderer`, `npx` may download the package. Unless another command is specified via `Renderer`, `mmdc` must be on the `PATH` in the compilation environment. Headless Chromium (Puppeteer) is required. Please refer to the Mermaid CLI documentation for details. |
| 29 | |
| 30 | ### For `ltmermaid` |
| 31 | |
| 32 | The following points differ from `mermaid`: |
| 33 | |
| 34 | | Item | Requirement | |
| 35 | | ---------------- | ------------------------------------------------------------ | |
| 36 | | TeX engine | LuaLaTeX (`lualatex`) only | |
| 37 | | Shell escape | `lualatex -shell-escape` (the CLI is invoked via `os.execute`) | |
| 38 | |
| 39 | As stated above, the engine is limited to LuaLaTeX. In other respects, the handling of external tools and `Renderer` / `npx` is identical to `mermaid`. |
| 40 | |
| 41 | --- |
| 42 | |
| 43 | ## Usage (Minimal Example) |
| 44 | |
| 45 | ### For `mermaid` |
| 46 | |
| 47 | ```latex |
| 48 | \documentclass{article} |
| 49 | % Example if `mmdc` is not on PATH: |
| 50 | % \usepackage[Renderer={npx -y @mermaid-js/mermaid-cli}]{mermaid} |
| 51 | \usepackage{mermaid} |
| 52 | |
| 53 | \begin{document} |
| 54 | % Optional: cap width at 80\% of line; small diagrams aren't scaled up |
| 55 | % \MermaidAdjustBoxOpts{max width=0.8\linewidth,center} |
| 56 | % \MermaidAdjustBoxOpts{max width=0.9\linewidth,center,valign=T} |
| 57 | |
| 58 | \begin{mermaid} |
| 59 | flowchart LR |
| 60 | A --> B |
| 61 | \end{mermaid} |
| 62 | \end{document} |
| 63 | ``` |
| 64 | |
| 65 | Compilation examples: |
| 66 | |
| 67 | ```bash |
| 68 | pdflatex -shell-escape yourfile.tex |
| 69 | xelatex -shell-escape yourfile.tex |
| 70 | lualatex -shell-escape yourfile.tex |
| 71 | uplatex -shell-escape yourfile.tex && dvipdfmx yourfile.dvi |
| 72 | ``` |
| 73 | |
| 74 | ### For `ltmermaid` |
| 75 | |
| 76 | Change the package name to `ltmermaid` (the `mermaid` environment in the body remains the same). |
| 77 | |
| 78 | ```latex |
| 79 | \documentclass{article} |
| 80 | % \usepackage[Renderer={npx -y @mermaid-js/mermaid-cli}]{ltmermaid} |
| 81 | \usepackage{ltmermaid} |
| 82 | |
| 83 | \begin{document} |
| 84 | \begin{mermaid} |
| 85 | flowchart LR |
| 86 | A --> B |
| 87 | \end{mermaid} |
| 88 | \end{document} |
| 89 | ``` |
| 90 | |
| 91 | Compilation is only possible with LuaLaTeX: |
| 92 | |
| 93 | ```bash |
| 94 | lualatex -shell-escape yourfile.tex |
| 95 | ``` |
| 96 | |
| 97 | ### Using `\mermaidfile` |
| 98 | |
| 99 | The `\mermaidfile[<includegraphics options>]{filename}` command renders Mermaid source from an external `.mmd` file and includes the resulting PDF. |
| 100 | This works with both `mermaid` and `ltmermaid`. |
| 101 | |
| 102 | ```latex |
| 103 | \documentclass{article} |
| 104 | \usepackage{ltmermaid} % or mermaid |
| 105 | |
| 106 | \begin{document} |
| 107 | \mermaidfile[width=0.8\linewidth]{diagram-001.mmd} |
| 108 | \end{document} |
| 109 | ``` |
| 110 | |
| 111 | The optional argument is forwarded directly to `\includegraphics`, so any valid graphics key is accepted, such as `width`, `height`, `angle`, `trim`, or `keepaspectratio`. |
| 112 | |
| 113 | You can also pass local options to the inline environment: |
| 114 | |
| 115 | ```latex |
| 116 | \begin{mermaid}[width=0.8\linewidth] |
| 117 | flowchart LR |
| 118 | A --> B |
| 119 | \end{mermaid} |
| 120 | ``` |
| 121 | |
| 122 | --- |
| 123 | |
| 124 | ### Renderer and Macros |
| 125 | |
| 126 | Whether using `mermaid` or `ltmermaid`, the following specification methods are the same (just substitute the package name in `\usepackage`). |
| 127 | |
| 128 | | Mechanism | Purpose | |
| 129 | | ------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------ | |
| 130 | | `\usepackage[Renderer=...]{mermaid}`, etc. | The prefix of the execution command line. The default is `mmdc` if omitted. | |
| 131 | | `\MermaidRendererOptions{...}` | Extra CLI arguments passed before `-i` / `-o` (merged after the default `-f`). | |
| 132 | | `\MermaidNoPdfFit` | Disables the `-f` (`--pdfFit`) option for `mmdc` (default is enabled). | |
| 133 | | `\MermaidAdjustBoxOpts{...}` | Additional options for the [adjustbox](https://ctan.org/pkg/adjustbox) environment used for diagram placement (default is `max width=0.9\linewidth,center`). | |
| 134 | | `\MermaidGraphicsOpts{...}` | Extra options passed to `\includegraphics` (e.g. `angle` or `trim`). The width is typically adjusted with `\MermaidAdjustBoxOpts`. | |
| 135 | |
| 136 | --- |
| 137 | |
| 138 | ### Output Files |
| 139 | |
| 140 | #### For `mermaid` |
| 141 | |
| 142 | Intermediate `.mmd` and `.pdf` files are output to the `mermaid/` directory right under the compilation directory. The file names will be a concatenation of the job name and a sequential number, such as `\jobname-mermaid-1.mmd`. This does not follow `-output-directory` / `TEXMF_OUTPUT_DIRECTORY`. |
| 143 | |
| 144 | #### For `ltmermaid` |
| 145 | |
| 146 | They are basically output to `mermaid/` in the same way, but when `-output-directory` is used, they are placed in `$TEXMF_OUTPUT_DIRECTORY/mermaid/`. |
| 147 | |
| 148 | For full explanations and diagram examples, see `mermaid-doc.tex` / `mermaid-doc-ja.tex` (`mermaid`) and `ltmermaid-doc.tex` / `ltmermaid-doc-ja.tex` (`ltmermaid`). |
| 149 | |
| 150 | --- |
| 151 | |
| 152 | ## Revision History |
| 153 | |
| 154 | ### `mermaid.sty` |
| 155 | |
| 156 | - Version 1.0c (2026-05-06): Various bug fixes. |
| 157 | - Version 1.0b (2026-04-29): Added `\mermaidfile`support, Makefile build/test targets, and `l3build` regression testing |
| 158 | - Version 1.0a (2026-04-20): Renamed sample documents. |
| 159 | - Version 1.0 (2026-04-16): Stable release. |
| 160 | |
| 161 | ### `ltmermaid.sty` |
| 162 | |
| 163 | - Version 1.0c (2026-05-06): Various bug fixes. |
| 164 | - Version 1.0b (2026-04-29): Added `\mermaidfile`support, Makefile build/test targets, and `l3build` regression testing |
| 165 | - Version 1.0a (2026-04-20): Renamed sample documents. |
| 166 | - Version 1.0 (2026-04-16): Stable release. |
| 167 | - Version 0.2 (2026-04-13): Removed `MERMAID_MMDC` and `MERMAID_MMDC_OPTIONS`. |
| 168 | - Version 0.1 (2026-04-08): Initial release. |
| 169 | |
| 170 | --- |