Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add out-of-the-box LaTeX in Markdown support to Hugo #10888

Closed

Conversation

janhuenermann
Copy link

@janhuenermann janhuenermann commented Apr 10, 2023

Hi,

from this thread in the forum a solution emerged to bring proper support for LaTeX in Markdown to Hugo.

Disclaimer: credits for this feature go to @jbirnick, who added support for this originally in his fork. I just opened this PR because the thread was about to go stale and this seems to be a feature that many people in the community would appreciate (including me).

The advantage of this approach over existing solutions is:

  • no need for special syntaxes that are unique to Hugo. Support standard LaTeX in Markdown out of the box. For example, any Markdown file that already renders on Github, will render now in Hugo.
  • allow rendering of inline LaTeX without shortcodes, which requires a lot of boilerplate

For more details, including previous solutions, I'd recommend to take a look at the original forum thread.

How to enable

Put the following in your config.toml:

[markup.goldmark.extensions]
Katex = true

Then include the Katex client side stylesheets. Please refer to the official installation guides from Katex.

Testing

Tested manually locally and verified that Katex = true renders Katex in the HTML code. Disabling it leads to previous behaviour.

@CLAassistant
Copy link

CLAassistant commented Apr 10, 2023

CLA assistant check
All committers have signed the CLA.

@janhuenermann janhuenermann changed the title Jan/katex support Add out-of-the-box LaTeX in Markdown support to Hugo Apr 10, 2023
@janhuenermann
Copy link
Author

If anyone has concerns regarding quickjs using cgo, I also came up with a solution that doesn't depend on any external packages/C code. It's here, would love to hear some feedback: janhuenermann@ad38246

@bep
Copy link
Member

bep commented Apr 12, 2023

Thanks for this. I would appreciate opening up an issue discussing this before creating a PR.

I'm closing this because of that, but additionally:

  • This comes with some dependencies I'm not sure about, but the blocker is QuickJS, which requires CGO. I know there are 2 other examples of that in Hugo (libwebp, libsass), but the current policy is that we consider this for policies that we must have. LateX doesn't fall into that category.
  • Also it seem to be fairly slow (see benchmark below). I see a cache is in place, but I suspect that this feature does not cache very well (I'm guessing most expressions in a site to be different).
BenchmarkRender-10    	      61	  18840946 ns/op	    6504 B/op	      30 allocs/op

@bep bep closed this Apr 12, 2023
@janhuenermann
Copy link
Author

Hi @bep,

Thanks for taking the time to look at the PR and provide feedback! I'm sorry for not opening an issue for discussion first. I didn't know that this is part of the contribution best practices in this repo. I'll open an issue after this response.

I completely agree with you. These are reasonable concerns. However, considering the high demand for this feature among the scientific blogging community, I believe it's worth exploring alternative implementations that don't require CGO. In fact, I've found a solution that enables LaTeX support in pure Go without any external dependencies (see janhuenermann@ad38246). I think it's worth considering this, as it would address the dependency concern.

As for the performance, you're right that it could be an issue for large sites. However, I want to emphasize that this feature is entirely optional and disabled by default. Therefore, users who don't enable it won't experience any performance hit. Also, I think the performance hit in this implementation mainly comes from executing the JavaScript code for SSR of the expressions, which is not necessary for the alternative implementation I mentioned.

Thanks again! I'll open an issue now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants