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

Katex rehype plugin does not render markdown math #777

Open
4 tasks done
jelc53 opened this issue Oct 7, 2023 · 4 comments
Open
4 tasks done

Katex rehype plugin does not render markdown math #777

jelc53 opened this issue Oct 7, 2023 · 4 comments
Labels
🤞 phase/open Post is being triaged manually

Comments

@jelc53
Copy link

jelc53 commented Oct 7, 2023

Initial checklist

Affected packages and versions

react 18.2.21, node 20.6.2, react-markdown 8.0.7, remark-gfm 3.0.1, remark-math 3.0.1, rehype-katex 5.0.0

Link to runnable example

No response

Steps to reproduce

Install packages npm install react-markdown remark-math rehype-katex katex.

And import 'katex/dist/katex.min.css'; to page.tsx.

Write component for markdown rendering:

import ReactMarkdown from 'react-markdown';
import remarkMath from 'remark-math';
import rehypeKatex from 'rehype-katex';

function MyComponent({ content }) {
  return (
    <ReactMarkdown
      remarkPlugins={[remarkMath]}
      rehypePlugins={[rehypeKatex]}
      children={content}
    />
  );
}

Finally, write page.tsx:

  const markdownContent = `
  This is inline math: $e = mc^2$.

  This is block math:

  $$
  \frac{-b \pm \sqrt{b^2-4ac}}{2a}
  $$
  `;

  return <MyComponent content={markdownContent} />;
}

Expected behavior

Expect math to be rendered without $$ and $ still visible everywhere.

Actual behavior

Math not rendered properly. Instead just read the latex as raw text with $ etc.

Runtime

Node v17

Package manager

npm 8

OS

Linux

Build and bundle tools

Next.js

@github-actions github-actions bot added 👋 phase/new Post is being triaged automatically 🤞 phase/open Post is being triaged manually and removed 👋 phase/new Post is being triaged automatically labels Oct 7, 2023
@wooorm
Copy link
Member

wooorm commented Oct 7, 2023

You are typing in a template literal. \f in a string in JS means whitespace. Escape you \s.
Otherwise, the CSS maybe doesn’t load? Inspect the DOM. Share what you find.

@jelc53
Copy link
Author

jelc53 commented Oct 13, 2023 via email

@wooorm
Copy link
Member

wooorm commented Oct 13, 2023

What is “fine”. It shouldn’t work fine either, with your code it should instead crash. Or you have different code, and likely don’t have CSS?

@CalculusCoder
Copy link

CalculusCoder commented Mar 17, 2024

You would have to use double slashes \\ when working with template literals and latex, as the first \ is not recognized as latex syntax.
$$ \\frac{-b \\pm \\sqrt{b^2-4ac}}{2a} $$

$$ \frac{-b \pm \sqrt{b^2-4ac}}{2a} $$

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤞 phase/open Post is being triaged manually
Development

No branches or pull requests

3 participants