I'm currently facing an issue with MathJax and ReactMarkdown in my React application. I've set up a component that uses better-react-mathjax to render Markdown content, including LaTeX math expressions. However, I'm encountering difficulties as only the Markdown content is being rendered, and the LaTeX expressions are not processed by MathJax.
export const ContentDetails = ({ content }) => {
const config = {
loader: { load: ["[tex]/html"] },
tex: {
packages: { "[+]": ["html"] },
inlineMath: [["$", "$"]],
displayMath: [["$$", "$$"]]
}
};
const ConvertToHtml = ({ markdownContent }) => {
return (
<MathJaxContext config={config} version={3}>
<MathJax>
<ReactMarkdown>
{markdownContent}
</ReactMarkdown>
</MathJax>
</MathJaxContext>
)
}
return (
<div className='pt-[80px] mb-[80px]'>
<div className='ml-[60px] mr-[60px] space-y-8'>
{content?.map((contentItem, index) => (
<div
key={index}
className='mr-[140px] ml-[140px] space-y-6 font-amiri text-right flex flex-col items-end'
>
<h1 className='mr-[12px] text-h3'>{contentItem.title}</h1>
<div className='mr-[12px] text-h6 leading-10 text-[#464848]'>
<ConvertToHtml markdownContent={contentItem.content_body} />
</div>
</div>
))}
</div>
</div>
);
};
I have already tested that both Mathjax and Reactmarkdown work fine when separate. The issue only raises when I combine them.
better-react-mathjaxis not adapted to being used together withreact-markdownand vice versa so it is expected that it requires some fiddling to make it work. MathJax itself works by typesetting the DOM andbetter-react-mathjaxleverages this functionality for use with React (which manipulates the DOM in a very particular way).react-markdownalso manipulates content and the DOM to render markdown correctly and if the resulting format is not in a regular DOM representation with the MathJax delimiters preserved, MathJax will not be able to help you out. There may also be a race condition between the processing of math and the processing of markdown whereby one of the two gets invalidated by the other.A thorough and controlled solution would thereby involve hooking into the
react-markdownrendering process and maybe add some wrapper (MathJaxcomponent or similar). Initially, one can also of course just attempt to wrap the entireMarkdowncomponent in aMathJaxcomponent to see if it works.It turns out that my initial attempts work well after just wrapping the
Markdowncomponent in aMathJaxcomponent, so if this doesn't work for you, what happens? Can you create a sandbox where the problem is shown? As I said, I would, if the problem can't be solved, try to hook into thereact-markdownrendering process and try to add theMathJaxfunctionality there, either by means of aMathJaxcomponent or extract theMathJaxBaseContextand then manually typeset with it, although such complicated use should probably not be necessary here.Here is a sandbox where it works out well: https://codesandbox.io/p/sandbox/user-example-so77769966-q352j2
PS! It seems you're including the
MathJaxContextin something that looks like a reusable component, theMathJaxContextshould only exist ONCE in an app and should wrap the entire app (https://github.com/fast-reflexes/better-react-mathjax?tab=readme-ov-file#usage) DS