diff --git a/d2compiler/compile.go b/d2compiler/compile.go index be7150d1a..8009f8705 100644 --- a/d2compiler/compile.go +++ b/d2compiler/compile.go @@ -409,6 +409,7 @@ func (c *compiler) compileLabel(attrs *d2graph.Attributes, f d2ir.Node) { } switch attrs.Language { case "latex": + println("\033[1;31m--- DEBUG:", scalar.ScalarString(), "\033[m") attrs.Shape.Value = d2target.ShapeText case "markdown": rendered, err := textmeasure.RenderMarkdown(scalar.ScalarString()) diff --git a/d2parser/parse.go b/d2parser/parse.go index 3eb500484..5d385f210 100644 --- a/d2parser/parse.go +++ b/d2parser/parse.go @@ -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 } diff --git a/e2etests/testdata/txtar/single-backslash-latex/dagre/board.exp.json b/e2etests/testdata/txtar/single-backslash-latex/dagre/board.exp.json new file mode 100644 index 000000000..35b4e6fbc --- /dev/null +++ b/e2etests/testdata/txtar/single-backslash-latex/dagre/board.exp.json @@ -0,0 +1,129 @@ +{ + "name": "", + "isFolderOnly": false, + "fontFamily": "SourceSansPro", + "shapes": [ + { + "id": "formula", + "type": "rectangle", + "pos": { + "x": 10, + "y": 20 + }, + "width": 142, + "height": 142, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "B4", + "stroke": "B1", + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "formula", + "fontSize": 28, + "fontFamily": "DEFAULT", + "language": "", + "color": "N1", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 92, + "labelHeight": 36, + "labelPosition": "OUTSIDE_TOP_CENTER", + "zIndex": 0, + "level": 1 + }, + { + "id": "formula.equation", + "type": "text", + "pos": { + "x": 40, + "y": 50 + }, + "width": 82, + "height": 82, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "transparent", + "stroke": "N1", + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "\\\\begin{equation} \\\\label{eq1}\n\\\\begin{split}\nA & = \\\\frac{\\\\\\\\pi r^2}{2} \\\\\\\\\n & = \\\\frac{1}{2} \\\\pi r^2\n\\\\end{split}\n\\\\end{equation}", + "fontSize": 16, + "fontFamily": "DEFAULT", + "language": "latex", + "color": "N1", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 82, + "labelHeight": 82, + "zIndex": 0, + "level": 2 + } + ], + "connections": [], + "root": { + "id": "", + "type": "", + "pos": { + "x": 0, + "y": 0 + }, + "width": 0, + "height": 0, + "opacity": 0, + "strokeDash": 0, + "strokeWidth": 0, + "borderRadius": 0, + "fill": "N7", + "stroke": "", + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "", + "fontSize": 0, + "fontFamily": "", + "language": "", + "color": "", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 0, + "labelHeight": 0, + "zIndex": 0, + "level": 0 + } +} diff --git a/e2etests/testdata/txtar/single-backslash-latex/dagre/sketch.exp.svg b/e2etests/testdata/txtar/single-backslash-latex/dagre/sketch.exp.svg new file mode 100644 index 000000000..13b6c3cc8 --- /dev/null +++ b/e2etests/testdata/txtar/single-backslash-latex/dagre/sketch.exp.svg @@ -0,0 +1,832 @@ +formula + + + + \ No newline at end of file diff --git a/e2etests/testdata/txtar/single-backslash-latex/elk/board.exp.json b/e2etests/testdata/txtar/single-backslash-latex/elk/board.exp.json new file mode 100644 index 000000000..ff19a34d5 --- /dev/null +++ b/e2etests/testdata/txtar/single-backslash-latex/elk/board.exp.json @@ -0,0 +1,129 @@ +{ + "name": "", + "isFolderOnly": false, + "fontFamily": "SourceSansPro", + "shapes": [ + { + "id": "formula", + "type": "rectangle", + "pos": { + "x": 12, + "y": 12 + }, + "width": 182, + "height": 182, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "B4", + "stroke": "B1", + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "formula", + "fontSize": 28, + "fontFamily": "DEFAULT", + "language": "", + "color": "N1", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 92, + "labelHeight": 36, + "labelPosition": "INSIDE_TOP_CENTER", + "zIndex": 0, + "level": 1 + }, + { + "id": "formula.equation", + "type": "text", + "pos": { + "x": 62, + "y": 62 + }, + "width": 82, + "height": 82, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "transparent", + "stroke": "N1", + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "\\\\begin{equation} \\\\label{eq1}\n\\\\begin{split}\nA & = \\\\frac{\\\\\\\\pi r^2}{2} \\\\\\\\\n & = \\\\frac{1}{2} \\\\pi r^2\n\\\\end{split}\n\\\\end{equation}", + "fontSize": 16, + "fontFamily": "DEFAULT", + "language": "latex", + "color": "N1", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 82, + "labelHeight": 82, + "zIndex": 0, + "level": 2 + } + ], + "connections": [], + "root": { + "id": "", + "type": "", + "pos": { + "x": 0, + "y": 0 + }, + "width": 0, + "height": 0, + "opacity": 0, + "strokeDash": 0, + "strokeWidth": 0, + "borderRadius": 0, + "fill": "N7", + "stroke": "", + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "", + "fontSize": 0, + "fontFamily": "", + "language": "", + "color": "", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 0, + "labelHeight": 0, + "zIndex": 0, + "level": 0 + } +} diff --git a/e2etests/testdata/txtar/single-backslash-latex/elk/sketch.exp.svg b/e2etests/testdata/txtar/single-backslash-latex/elk/sketch.exp.svg new file mode 100644 index 000000000..7b60f04d6 --- /dev/null +++ b/e2etests/testdata/txtar/single-backslash-latex/elk/sketch.exp.svg @@ -0,0 +1,832 @@ +formula + + + + \ No newline at end of file diff --git a/e2etests/txtar.txt b/e2etests/txtar.txt index 38b0ec213..ad28b43f5 100644 --- a/e2etests/txtar.txt +++ b/e2etests/txtar.txt @@ -657,3 +657,15 @@ my_table: { } x -> my_table."shape" + +-- single-backslash-latex -- +formula: { + equation: |latex + \begin{equation} \label{eq1} + \begin{split} + A & = \frac{\\pi r^2}{2} \\ + & = \frac{1}{2} \pi r^2 + \end{split} + \end{equation} + | +}