Last active
November 25, 2024 05:39
-
-
Save mcleonard/0433c1eca1d56489259118524824f159 to your computer and use it in GitHub Desktop.
Render math expressions in Markdown with Mathjax as a React component
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ReactMarkdown from 'react-markdown'; | |
import MathJax from 'react-mathjax'; | |
import RemarkMathPlugin from 'remark-math'; | |
function MarkdownRender(props) { | |
const newProps = { | |
...props, | |
plugins: [ | |
RemarkMathPlugin, | |
], | |
renderers: { | |
...props.renderers, | |
math: (props) => | |
<MathJax.Node formula={props.value} />, | |
inlineMath: (props) => | |
<MathJax.Node inline formula={props.value} /> | |
} | |
}; | |
return ( | |
<MathJax.Provider input="tex"> | |
<ReactMarkdown {...newProps} /> | |
</MathJax.Provider> | |
); | |
} | |
export default MarkdownRender |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you are using
and facing Failed to resolve import "katex/dist/katex.min.css" from "src/components/YourComponent.tsx". Does the file exist?
You can download the
katex.min.css
file, move the minified css file to your styles directory and manually import it to your react component.