I know that you can use for example KaTeX to convert LaTeX markup client-side, however I am wondering if there exists a way to pre-render LaTeX while building the website with Hugo?
According to the KaTeX website, it’s possible to use it server-side in a Node environment, but as far as I can tell in Hugo there’s currently no way to just plug it in as a compile step.
Server-side or CLI rendering with KaTeX produces HTML, not an image, so you would still need to include CSS on the client side. If this is acceptable, roll your own local server, then use Hugo’s resources.GetRemote to send the tex and retrieve the HTML. The results are cached (getresource).
@jmooring As of Chrome 109, all major browsers (Opera requiring special config) supper MathML syntax without requiring CSS. Much like SVG it’s got it’s own syntax and rendering style. The Hugo In Action way is to have a Netlify function that outputs MathJax’s SVG in a JSON file.
KaTeX can produce the markup that would be great as a server-side render.
@kamov I’ve been exploring this too. It doesn’t appear that Hugo has a render injection point, even with a shortcode that can render these appropriately (I’m still working this though…) another option would be to ask the contributors to bring the KaTeX extension into goldmark (but it’s not an official extension). If I find a way of doing it sans the goldmark extension, I’ll try to post here.
The code below works for me in all my browsers, without CSS:
This code block render hook renders an SVG image from LaTeX mathematical markup using the math.vercel.app service. This is performed when you build your site; no CSS or JS required.
The foreground and background are inverted when the browser is in dark mode.
The result of the remote call is cached; you are not hitting the service on every build.
This example site includes the render hook and a shortcode. You can use the shortcode to produce inline equations.
git clone --single-branch -b hugo-forum-topic-42145 https://github.com/jmooring/hugo-testing hugo-forum-topic-42145
cd hugo-forum-topic-42145
hugo server