gatsby: gatsby-plugin-mdx doesn't render "complex" Katex formulas
Description
I don’t know the exact terms to describe it, but “complex” Katex formulas prevent MDX from rendering. (Feel free to change the title into something more specific)
I’ve figured out a workaround by passing the formula as a string to a custom component that runs the string through katex.renderToString and then passes the output to dangerouslySetInnerHTML. That might help in pinpointing where the problem occurs.
(⚠️ Note for anyone looking for a QUICKFIX: use this component to render Katex formulas that break rendering)
I have already talked to the MDX team about this, and they managed to get it working by removing gatsby-plugin-mdx from the equation (see the link for codesandboxes with examples).
Steps to reproduce
There is a minimal reproducible example here with instructions on how to make it fail as well as the workaround in action.
This is the formula in question:
$$
\begin{aligned}
\bar{x} &= \dfrac{0+1+2+ \ldots +8}{9} = 3.56 \\
\bar{y} &= \dfrac{23.5+25+ \ldots +37.5}{9}=30
\end{aligned}
$$
Expected result
The formula should render.
Actual result
I get this error:
error "gatsby-plugin-mdx" threw an error while running the onCreateNode lifecycle:
unknown: Expecting Unicode escape sequence \uXXXX (18:9)
16 | {x}
17 | {` &= \\dfrac`}
> 18 | {0+1+2+ \ldots +8}
| ^
19 | {9}
20 | {` = 3.56 `}
21 | {`\\`}
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 2
- Comments: 28 (21 by maintainers)
Commits related to this issue
- ths fix doesn't seem to work: https://github.com/gatsbyjs/gatsby/issues/28031#issuecomment-817170805 — committed to nikoladev/mdx-katex-bug by nikoladev 3 years ago
- Fix math rendering Switches from gatsby-remark-katex to remark-math + rehype-katex See https://github.com/gatsbyjs/gatsby/issues/28031#issuecomment-817170805 — committed to NUbots/NUbook by JosephusPaye 3 years ago
please, mention @nikoladev instead 😃
Marked “not stale”. Yeah, we need to do soon a major release for the whole suite of plugins with updated dependencies.
Hi, this comment is just to provide another example of a formula that I couldn’t get to work and to provide a workaround (for this specific case), in case someone is struggling with a similar problem.
My specific (but imo quite general case) is to get some LaTeX formula into a subscript block. While simple things such as indices (e.g.,
$a_3$) work fine, I couldn’t produce formulas such as$\mathbb{E}_{a_t \sim \pi(s_t)}$, which should renderbut which suffers from the same problem as the one described in the original post, i.e.,
One quite annoying way to make this work is to use a sequence of empty
\phantom{}blocks and subscript everyone of them with a single character or symbol. For the example mentioned above this would be to use$\mathbb{E}_{a_t} \phantom{}_\sim \phantom{}_\pi \phantom{}_{(s_t)}$,which I used to produce the image shown above. Maybe thhis helps someone until the update with MDX v2 is out 😃
You’re welcome 😃
Thanks for letting me know it was hard to find, I’ve updated the intro post for anyone who is looking for a quickfix.
Not really. Here’s a response from a Gatsby employee on them waiting for a stable version 2 of MDX. Only thing I can think of is subscribing to MDX releases (you can click on
Watchat the top and then selectCustomand then chooseReleasesto get Github notification of releases).Woooooow, I only now saw your
KatexBlockandKatexInlinecomponents @nikoladev ! This is 1000x better and easier to use than my stupid hack. This saved my day – thank you!!@janmaltel - i tried
$\mathbb{E}_{a_t \sim \pi(s_t)}$on my gatsby app and I am able to render the math equation just fine:BTW, I am using
@mdx-js/mdxand@mdx-js/reactv1.6.22. Also, I am usingremark-mathv3.0.1 andrehype-katexv5.0.0.Here is the relevant snippet from my
gatsby-config.js:Out of curiosity, are you using mdx v2? Because I’ve only encountered the error
SyntaxError: unknown: Expecting Unicode escape sequence \uXXXXwhen trying to upgrade from mdx v1 to mdx v2 on a next.js project.On a side note, there is a major overhaul in how mdx is parsed in v2 (see https://github.com/mdx-js/mdx/pull/1039) and that could affect the way math equations are rendered. Also, there will be breaking changes mdx v2 so some things that worked in markdown or mdx v1 no longer will work in mdx v2 (i.e., html comments will no longer be supported in mdx v2. see https://github.com/mdx-js/mdx/issues/1042). As already mentioned by the author of mdx, MD !== MDX. I want to add that MDXv1 !== MDXv2
@nikoladev - I tried your complex math formula on a gatsby site that I am working on and it seems to render properly.
the complex math formula:
screenshot of rendered complex formula (in dark mode):
However, I am using the latest npm packages (except for remark-math which needs to be on v3 until
gatsby-plugin-mdxuses remark 13. Also, I am usingrehype-katexinstead ofremark-html-katex(i believe rehype-katex is recommended over remark-html-katex. I didn’t clone your repo, but just copy/pasted the complex math formula from your repo into my gatsby site to see there was a problem. Apparently, it seems to render correctly.So in your
gatsby-config.js, change:to:
And make sure to install
rehype-katex(and removeremark-html-katex)👋 hey we’re working on updating the gatsby remark ecosystem to the latest versions of things which should help out with this issue. If you’ll want to try testing things out to see if katex works better, that’d be really helpful! https://github.com/gatsbyjs/gatsby/discussions/30385