Skip to content

Commit

Permalink
Merge pull request #2232 from alixander/bad-latex
Browse files Browse the repository at this point in the history
d2parser: escape backslashes for latex
  • Loading branch information
alixander authored Nov 24, 2024
2 parents db687ab + a7869de commit 50d048f
Show file tree
Hide file tree
Showing 15 changed files with 2,572 additions and 641 deletions.
3 changes: 3 additions & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

- Composition: links pointing to own board are purged [#2203](https://github.com/terrastruct/d2/pull/2203)
- Syntax: reserved keywords must be unquoted [#2231](https://github.com/terrastruct/d2/pull/2231)
- Latex: Backslashes in Latex blocks do not escape [#2232](https://github.com/terrastruct/d2/pull/2232)
- This is a breaking change. Previously Latex blocks required escaping the backslash. So
for older D2 versions, you should remove the excess backslashes.

#### Bugfixes ⛑️

Expand Down
6 changes: 6 additions & 0 deletions d2parser/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -1480,6 +1480,12 @@ func (p *parser) parseBlockString() *d2ast.BlockString {
}

if r != endHint {
if (bs.Tag == "latex" || bs.Tag == "tex") && r == '\\' {
// For LaTeX, where single backslash is common, we escape it so that users don't have to write double the backslashes
sb.WriteRune('\\')
sb.WriteRune('\\')
continue
}
sb.WriteRune(r)
continue
}
Expand Down
12 changes: 3 additions & 9 deletions e2etests/stable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1381,30 +1381,24 @@ c: |md
{
name: "latex",
script: `a: |latex
\\Huge{\\frac{\\alpha g^2}{\\omega^5} e^{[ -0.74\\bigl\\{\\frac{\\omega U_\\omega 19.5}{g}\\bigr\\}^{\\!-4}\\,]}}
\Huge{\frac{\alpha g^2}{\omega^5} e^{[ -0.74\bigl\{\frac{\omega U_\omega 19.5}{g}\bigr\}^{\!-4}\,]}}
|
b: |latex
e = mc^2
|
z: |latex
gibberish\\; math:\\sum_{i=0}^\\infty i^2
gibberish\; math:\sum_{i=0}^\infty i^2
|
z -> a
z -> b
a -> c
b -> c
sugar -> c
c: mixed together
c -> solution: we get
Linear program: {
formula: |latex
\\min_{ \\mathclap{\\substack{ x \\in \\mathbb{R}^n \\ x \\geq 0 \\ Ax \\leq b }}} c^T x
\min_{ \mathclap{\substack{ x \in \mathbb{R}^n \ x \geq 0 \ Ax \leq b }}} c^T x
|
}
`,
Expand Down
26 changes: 10 additions & 16 deletions e2etests/testdata/files/grid_outside_labels.d2
Original file line number Diff line number Diff line change
Expand Up @@ -4,61 +4,55 @@ container: ___________________________________container_________________________

amscd plugin: {
ex: |tex
\\begin{CD} B @>{\\text{very long label}}>> C S^{{\\mathcal{W}}_\\Lambda}\\otimes T @>j>> T\\\\ @VVV V \\end{CD}
\begin{CD} B @>{\text{very long label}}>> C S^{{\mathcal{W}}_\Lambda}\otimes T @>j>> T\\ @VVV V \end{CD}
|
}

braket plugin: {
style.3d: true
ex: |tex
\\bra{a}\\ket{b}
\bra{a}\ket{b}
|
}

cancel plugin: {
ex: |tex
\\cancel{Culture + 5}
\cancel{Culture + 5}
|
}

color plugin: {
icon: https://icons.terrastruct.com/essentials/profits.svg
icon.near: outside-right-center
ex: |tex
\\textcolor{red}{y} = \\textcolor{green}{\\sin} x
\textcolor{red}{y} = \textcolor{green}{\sin} x
|
}

gensymb plugin: {
ex: |tex
\\lambda = 10.6\\,\\micro\\mathrm{m}
\lambda = 10.6\,\micro\mathrm{m}
|
}

mhchem plugin: {
style.multiple: true
ex: |tex
\ce{SO4^2- + Ba^2+ -> BaSO4 v}
|
}

physics plugin: {
ex: |tex
\\var{F[g(x)]}
\\dd(\\cos\\theta)
\var{F[g(x)]}
\dd(\cos\theta)
|
}

multilines: {
ex: |tex
\\displaylines{x = a + b \\\\ y = b + c}
\\sum_{k=1}^{n} h_{k} \\int_{0}^{1} \\bigl(\\partial_{k} f(x_{k-1}+t h_{k} e_{k}) -\\partial_{k} f(a)\\bigr) \\,dt
\displaylines{x = a + b \\ y = b + c}
\sum_{k=1}^{n} h_{k} \int_{0}^{1} \bigl(\partial_{k} f(x_{k-1}+t h_{k} e_{k}) -\partial_{k} f(a)\bigr) \,dt
|
}

asm: {
ex: |latex
\\min_{ \\mathclap{\\substack{ x \\in \\mathbb{R}^n \\ x \\geq 0 \\ Ax \\leq b }}} c^T x
\min_{ \mathclap{\substack{ x \in \mathbb{R}^n \ x \geq 0 \ Ax \leq b }}} c^T x
|
}
}
2 changes: 1 addition & 1 deletion e2etests/testdata/files/grid_with_latex.d2
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ y: {
z: {
grid-columns: 2
lim: |latex
\\lim_{h \\rightarrow 0 } \\frac{f(x+h)-f(x)}{h}
\lim_{h \rightarrow 0 } \frac{f(x+h)-f(x)}{h}
|
add: |latex
1 + 1
Expand Down
30 changes: 15 additions & 15 deletions e2etests/testdata/stable/grid_outside_labels/dagre/board.exp.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 50d048f

Please sign in to comment.