@cristianormazabal/triton-latex 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +186 -0
- package/dist/cli.cjs +41543 -0
- package/dist/cli.cjs.map +7 -0
- package/package.json +52 -0
- package/triton.sty +198 -0
package/README.md
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
# `@triton/latex` — Triton diagrams in LaTeX
|
|
2
|
+
|
|
3
|
+
Author Triton diagrams **inline in your `.tex` file** — write the diagram source
|
|
4
|
+
between `\begin{triton} … \end{triton}` and it renders to a **vector PDF** at
|
|
5
|
+
compile time, dropped in exactly like a `tikzpicture`. No pre-render step, no
|
|
6
|
+
figure files to manage.
|
|
7
|
+
|
|
8
|
+
```latex
|
|
9
|
+
\usepackage{triton}
|
|
10
|
+
|
|
11
|
+
\begin{triton}
|
|
12
|
+
flowchart LR
|
|
13
|
+
A[Start] --> B{Choice}
|
|
14
|
+
B -->|yes| C[Ship it]
|
|
15
|
+
B -->|no| D[Fix it]
|
|
16
|
+
D --> E[Review]
|
|
17
|
+
\end{triton}
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Compile with shell-escape:
|
|
21
|
+
|
|
22
|
+
```sh
|
|
23
|
+
tectonic -Z shell-escape -Z shell-escape-cwd=. mydoc.tex
|
|
24
|
+
pdflatex -shell-escape mydoc.tex
|
|
25
|
+
lualatex -shell-escape mydoc.tex
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
For environments where shell-escape is off (e.g. **Overleaf**), the same package
|
|
29
|
+
also supports a **precompile + `\includegraphics`** workflow — see
|
|
30
|
+
[Overleaf / no-shell-escape fallback](#overleaf--no-shell-escape-fallback).
|
|
31
|
+
|
|
32
|
+
## How inline rendering works
|
|
33
|
+
|
|
34
|
+
When LaTeX hits `\begin{triton}`, the package:
|
|
35
|
+
|
|
36
|
+
1. **Captures the body verbatim** (via `fancyvrb`'s `VerbatimOut`) to a temp file,
|
|
37
|
+
`\jobname.triton-src.triton` — brackets, indentation and all.
|
|
38
|
+
2. **Content-hashes** that file with `\pdf@filemdfivesum` (from `pdftexcmds`).
|
|
39
|
+
3. **Shells out** (`\write18`) to the Triton CLI to render it to
|
|
40
|
+
`\jobname.triton-cache/<hash>.pdf` — but only if that PDF doesn't already
|
|
41
|
+
exist, so unchanged diagrams are **not** re-rendered on subsequent runs.
|
|
42
|
+
4. **`\includegraphics`** the resulting vector PDF.
|
|
43
|
+
|
|
44
|
+
The render is **pure-JS, no system binaries** (no Inkscape, `rsvg-convert`, or
|
|
45
|
+
Chromium). Text becomes real vector glyphs in the embedded base-14 fonts
|
|
46
|
+
(Helvetica/Times/Courier), so there is no font-drift between dev, CI, and the PDF.
|
|
47
|
+
|
|
48
|
+
If shell-escape is **off** or the CLI is **missing**, the environment fails with a
|
|
49
|
+
clear `\PackageError` telling you exactly what to fix — it never silently emits a
|
|
50
|
+
blank.
|
|
51
|
+
|
|
52
|
+
## Setup
|
|
53
|
+
|
|
54
|
+
```sh
|
|
55
|
+
cd latex
|
|
56
|
+
pnpm install # pdfkit + svg-to-pdfkit (isolated from core triton)
|
|
57
|
+
node esbuild.mjs # → dist/cli.cjs (runs build:grammars first)
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
This builds the `triton-latex` CLI. Point the package at it once with
|
|
61
|
+
`\tritoncli` (or put `triton-latex` on `PATH` and skip this — it's the default):
|
|
62
|
+
|
|
63
|
+
```latex
|
|
64
|
+
\tritoncli{node /abs/path/to/latex/dist/cli.cjs}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Per-diagram sizing
|
|
68
|
+
|
|
69
|
+
A verbatim environment **cannot** carry an inline optional argument — peeking for
|
|
70
|
+
a `[` on the `\begin{triton}` line tokenises the line break that `fancyvrb` needs
|
|
71
|
+
to find the end of that line, which swallows the diagram's first line. (`minted`
|
|
72
|
+
sidesteps this only because of its mandatory `{language}` argument.) So
|
|
73
|
+
`\begin{triton}[width=…]` is **not** supported.
|
|
74
|
+
|
|
75
|
+
Instead, set the `\includegraphics` options for the **next** diagram just before
|
|
76
|
+
it, or change the default globally:
|
|
77
|
+
|
|
78
|
+
```latex
|
|
79
|
+
\tritonnext{width=0.55\linewidth} % applies to the NEXT \begin{triton} only
|
|
80
|
+
\begin{triton}
|
|
81
|
+
flowchart TD
|
|
82
|
+
P[Parse] --> L[Layout] --> R[Render]
|
|
83
|
+
\end{triton}
|
|
84
|
+
|
|
85
|
+
\tritonsetup{width=0.8\linewidth} % new global default for all later diagrams
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## One-liners
|
|
89
|
+
|
|
90
|
+
For a single line of source, `\tritoninline` takes a verbatim argument delimited
|
|
91
|
+
by any character (here `|`) and an optional `\includegraphics` key list:
|
|
92
|
+
|
|
93
|
+
```latex
|
|
94
|
+
\tritoninline[width=3cm]|flowchart LR; A --> B|
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## Macro reference
|
|
98
|
+
|
|
99
|
+
| Macro | Effect |
|
|
100
|
+
| --- | --- |
|
|
101
|
+
| `\begin{triton} … \end{triton}` | Inline authoring → render → include (needs shell-escape). |
|
|
102
|
+
| `\tritonnext{<opts>}` | `\includegraphics` options for the **next** inline diagram only. |
|
|
103
|
+
| `\tritoninline[<opts>]\|…\|` | One-line inline source (verbatim, any delimiter). |
|
|
104
|
+
| `\tritonsetup{<opts>}` | Default `\includegraphics` options (default `width=\linewidth`). |
|
|
105
|
+
| `\tritoncli{<cmd>}` | CLI invocation (default `triton-latex`). |
|
|
106
|
+
| `\tritontheme{<name>}` | Theme preset passed to the CLI for inline renders. |
|
|
107
|
+
| `\tritonscale{<n>}` | Scale passed to the CLI (default `1`). |
|
|
108
|
+
| `\tritoncachedir{<dir>}` | Render cache directory (default `\jobname.triton-cache`). |
|
|
109
|
+
| `\triton[<opts>]{<name>}` | **Precompile fallback:** `\includegraphics` of `<dir>/<name>.pdf`. |
|
|
110
|
+
| `\tritonfile[<opts>]{<name>}` | Explicit alias of the precompile include form. |
|
|
111
|
+
| `\tritonfig[<opts>]{<name>}{<caption>}` | Precompile include wrapped in a captioned `figure`. |
|
|
112
|
+
| `\tritondir{<path>}` | Directory holding precompiled `<name>.pdf` (default `triton-figures`). |
|
|
113
|
+
|
|
114
|
+
Depends only on `graphicx`, `fancyvrb`, and `pdftexcmds` — engine-agnostic
|
|
115
|
+
(pdfLaTeX / XeLaTeX / LuaLaTeX / tectonic).
|
|
116
|
+
|
|
117
|
+
## Example
|
|
118
|
+
|
|
119
|
+
[`examples/inline-demo.tex`](examples/inline-demo.tex) authors two diagrams inline
|
|
120
|
+
and renders them at compile time. From `examples/`:
|
|
121
|
+
|
|
122
|
+
```sh
|
|
123
|
+
tectonic -Z shell-escape -Z shell-escape-cwd=. inline-demo.tex
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
> **`triton.sty` discovery:** `tectonic` only searches the input file's own
|
|
127
|
+
> directory for local `.sty` files (it ignores `TEXINPUTS`). `examples/triton.sty`
|
|
128
|
+
> is therefore a symlink to `../triton.sty`. With a real package install (or
|
|
129
|
+
> `pdflatex`, which honours `TEXINPUTS`) the symlink is unnecessary.
|
|
130
|
+
|
|
131
|
+
## CLI
|
|
132
|
+
|
|
133
|
+
The same CLI powers both the inline environment and manual/precompile rendering:
|
|
134
|
+
|
|
135
|
+
```sh
|
|
136
|
+
# one file → vector PDF
|
|
137
|
+
node dist/cli.cjs render diagram.mmd -o figures/diagram.pdf
|
|
138
|
+
|
|
139
|
+
# one file → SVG pass-through (convenience)
|
|
140
|
+
node dist/cli.cjs render diagram.mmd -o diagram.svg
|
|
141
|
+
|
|
142
|
+
# whole directory of *.triton / *.mmd → <name>.pdf
|
|
143
|
+
node dist/cli.cjs render-dir diagrams/ -o figures/
|
|
144
|
+
|
|
145
|
+
# options
|
|
146
|
+
node dist/cli.cjs render diagram.mmd -o out.pdf --theme executive --scale 2
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
Installed as the `triton-latex` bin when the package is linked.
|
|
150
|
+
|
|
151
|
+
## Overleaf / no-shell-escape fallback
|
|
152
|
+
|
|
153
|
+
Overleaf (and any compile with shell-escape disabled) can't run the CLI, so render
|
|
154
|
+
the diagrams to PDF **ahead of time** and include the committed assets:
|
|
155
|
+
|
|
156
|
+
```latex
|
|
157
|
+
\usepackage{triton}
|
|
158
|
+
\tritondir{figures}
|
|
159
|
+
\triton{flowchart} % \includegraphics figures/flowchart.pdf
|
|
160
|
+
\tritonfig[width=0.6\linewidth]{avl}{An AVL tree.}
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
```sh
|
|
164
|
+
cd examples
|
|
165
|
+
make figures # render diagrams/ → figures/ (vector PDF)
|
|
166
|
+
make pdf # compile demo.tex (tectonic or pdflatex)
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
Workflow:
|
|
170
|
+
|
|
171
|
+
1. Render locally: `make figures` (or `render-dir`).
|
|
172
|
+
2. Commit `figures/*.pdf`, `triton.sty`, and your `.tex`.
|
|
173
|
+
3. Upload to Overleaf — it compiles with no Node, no Triton, no shell-escape.
|
|
174
|
+
|
|
175
|
+
Regenerate the PDFs whenever a diagram source changes. Because the bare
|
|
176
|
+
`\triton{<name>}` command and the `triton` environment share a name, the package
|
|
177
|
+
dispatches on context (`\@currenvir`): `\begin{triton}` authors inline, while
|
|
178
|
+
`\triton{<name>}` includes a precompiled PDF.
|
|
179
|
+
|
|
180
|
+
## Why a separate package
|
|
181
|
+
|
|
182
|
+
This is an **isolated satellite package** (its own `node_modules`,
|
|
183
|
+
`pnpm-workspace.yaml`, and lockfile), exactly like `extension/`. The PDF toolchain
|
|
184
|
+
(`pdfkit`, `svg-to-pdfkit`) lives here and **only** here — the core `triton`
|
|
185
|
+
package gains **zero** new dependencies. The CLI imports the compiler by relative
|
|
186
|
+
path from `../src` and esbuild bundles it in.
|