Skip to content

Instantly share code, notes, and snippets.

@NickyMeuleman
Created June 13, 2021 00:32
Show Gist options
  • Save NickyMeuleman/3e1ae1119d40f553b69341cd1b67faa1 to your computer and use it in GitHub Desktop.
Save NickyMeuleman/3e1ae1119d40f553b69341cd1b67faa1 to your computer and use it in GitHub Desktop.
Ideal KaTeX MDX usecase
import React from "react";
import { CodeBlock } from "../components/CodeBlock";
import { MathBlock } from "../components/MathBlock";
import TeX from "@matejmazur/react-katex";
export default {
pre: ({ children }) => <>{children}</>,
inlineCode: ({ children }) => <code>{children}</code>,
inlineMath: ({ children }) => <TeX>{children}</TeX>,
code: CodeBlock,
math: MathBlock
};

Some inline math $a+b=c$

$$ title=a-block-of-math S_n = \frac{n(2a + (n-1)d)}{2} $$

/** @jsx jsx */
import React from "react";
import TeX from "@matejmazur/react-katex";
import { jsx } from "theme-ui";
const CodeBlock: React.FC<IProps> = ({
children,
title,
...props
}) => {
return (
<React.Fragment>
{title && <div sx={{ variant: `styles.MathBlock.title` }}>{title}</div>}
<div sx={{ variant: `styles.MathBlock` }}>
<TeX block>{children}</TeX>
</div>
</React.Fragment>
)
}
};
export { MathBlock };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment