Newline problem with transform.ToMath

I need to have a string as a paramters to a shortcode that has double backslashes in it. How do I escape a DOUBLE backslash?

for example… {{<math `line 1\\line2`>}}

(I want the two back slashes to get passed in the string)

This generates a build error:
failed to render shortcode “math”: failed to process shortcode: “/Users/gleicher/Projects/559/Workbooks25/common/themes/Workbook/layouts/shortcodes/math.html:5:13”: execute of template failed at <transform.ToMath>: error calling ToMath: not a function

The shortcode seems to work for everything else - other than double backslashes.

I’ve tried using 4 backslashes, but that creates the same problem.

If it helps, this is my shortcode (math.html):

{{- $opts := dict "displayMode" (in .Params "display")}}
{{- transform.ToMath (.Get 0) $opts -}}

It works great, except for the double backslashes.

no solution , just a hint for someone for analysis:

no shortcode used, just called in “main” block of home template

that errors out:

{{- transform.ToMath `line1\\line2` (dict "displayMode" true) -}}

Error: error building site: render: failed to render pages:
  render of "/content/_index.md" failed: "\layouts\home.html:3:16":
    execute of template failed: template: home.html:3:16:
      executing "main" at <transform.ToMath>:
        error calling ToMath: not a function

this works:

{{- transform.ToMath `line1\\line2` (dict "displayMode" false) -}}
hugo env v0.147.5
hugo v0.147.5-7766fc62416ff95ed7768c19947ac0fcd96444cc+extended windows/amd64 BuildDate=2025-05-22T11:37:19Z VendorInfo=gohugoio
GOOS="windows"
GOARCH="amd64"
GOVERSION="go1.24.0"
github.com/sass/libsass="3.6.6"
github.com/webmproject/libwebp="v1.3.2"
github.com/sass/dart-sass/protocol="3.1.0"
github.com/sass/dart-sass/compiler="1.83.4"
github.com/sass/dart-sass/implementation="1.83.4"```

This is not my domain of expertise, but I suspect that you can get some help from the Katex docs here:

Thanks for looking into this…

It’s not the problem I thought it was…

I have no idea how one traces this down, since it seems to be somewhere in the way that Hugo handles KaTeX.

mine not either …

it’s not the \\ per se, that one works

{{- transform.ToMath `\sum_{\substack{0<i<m\\0<j<n }}` (dict "DisplayMode" true) -}}

and I’m out here :wink:

OK. This is weird.

There seem to be some uses of the double backslash in KaTeX that cause Hugo to forget that ToMath is a function - but only in display mode.

My examples that break (I have several) are valid LaTeX (and could be rendered correctly by MathJax or LaTeX). This all happened when switching from MathJax to KaTeX.

When I look at the Hugo source code, I see nothing… I don’t even see the “display” parameter in the code (I am looking at transform.go around 221)

If it’s a KaTeX problem… why does Hugo forget that KaTeX exists (that ToMath isn’t a function)?

Actually, the plot thickens…

LaTeX & KaTeX Math Rendering: The Double Backslash \\ in Markdown - upyesp - suggests that “\cr” is a synonym for \ - and indeed, Supported Functions · KaTeX suggests that it is a valid line separator. But, if I try to use it (in a place where the double backslash breaks), I get a valid error message: execute of template failed: template: _shortcodes/math.html:5:13: executing “_shortcodes/math.html” at <transform.ToMath>: error calling ToMath: KaTeX parse error: Undefined control sequence: \cr at position 7: line 1\̲c̲r̲ ̲line2

So, there is both a KaTeX problem (why doesn’t it like line separators in display mode some of the time) and a Hugo problem (why do the error messages sometimes get replaced by ToMath is not a function).

Ugh.

I guess I should re-open the problem with a more descriptive title.

This does not address the toMath problem, but client side rendering using katex.js works fine.

Well…
Can’t say I truly understand… but…

It seems like KaTeX doesn’t like the line break in an outer environment in display mode. If you look at the example from @irkode , the line break is within the substack environment.

My workaround: put the multiline equation inside of an align environment - and it seems to work just fine.

So…
don’t use a KaTeX line break in an outer level environment in display mode because it causes Hugo to break in a way that forgets that ToMath is a function.

Hugo knows “ToMath” is a function. The error message is unclear; there either isn’t a an object of the “not a function” portion of the message, or its not printable, or there isn’t one.

Regardless, since katex handles this as expected with client side rendering, and in the sandbox on their website, Hugo’s transform.ToMath should handle it as well.

1 Like

@gleicher

With the katex command-line interface I get this message:

LaTeX-incompatible input ...
In LaTeX, \\ or \newline does nothing in display mode [newLineInDisplayMode]

So… (a) don’t do that, and (b) we need to find a way to make that message visible.

See also: https://discourse.gohugo.io/t/katex-transform-tomath-throws-error-with-valid-syntax/53320/3

Related issues and pull requests:

3 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.