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

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

Katex rehype plugin does not render markdown math #777

jelc53 opened this issue Oct 7, 2023 · 11 comments
Labels
🤷 no/invalid This cannot be acted upon 👎 phase/no Post cannot or will not be acted on

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} $$

@liukkm
Copy link

liukkm commented Jun 25, 2024

At present, GPT4 will not return the mathematical formula beginning and ending with '$$', and katex cannot work normally at this time. What should I do?

@wooorm
Copy link
Member

wooorm commented Jun 25, 2024

make a repro

@ChristianMurphy ChristianMurphy added the 🙊 open/needs-repro This needs a reproduction label Jun 25, 2024

This comment has been minimized.

@liukkm
Copy link

liukkm commented Jun 26, 2024

Hi! Thanks for taking the time to contribute! This has been marked by a maintainer as needing a reproduction: It’s not yet clear whether this is a problem. Here are a couple tips:

  • Thoroughly document how to reproduce the problem, in steps or with code
  • Don’t link to your complete project: make the repro as tiny as possible, preferrably with only the problematic project in question
  • Make sure you’re on the latest versions of projects (and node/npm/yarn!)
  • The best issue report is a failing test proving it

Thanks, — bb

Like the markdown text described below (returned by gpt4o), the mathematical formula is not surrounded by $$, which causes the mathematical formula to not render correctly

markdown:

Overview of Lorentz Transformation

  1. Time transformation:
    [ t' = \gamma \left( t - \frac{vx}{c^2} \right) ]

  2. Spatial transformation:
    [ x' = \gamma (x - vt) ]
    [ y' = y ]
    [ z' = z ]

Where (\gamma) is the Lorentz factor, defined as:
[ \gamma = \frac{1}{\sqrt{1 - \frac{v^2}{c^2}}} ]

My react-markdown reference code looks like this

function _MarkDownContent(props: { content: string }) {
  const [setDefaultContent] = useChatStore((state) => [
    state.setDefaultContent,
  ]);
  // const [showImage, setShowImage] = useState(false)
  // const [imageSrc, setImageSrc] = useState('')
  return (
    <>
      {/*  RehypeRaw 解析HTML*/}
      <ReactMarkdown
        remarkPlugins={[RemarkMath, RemarkGfm, RemarkBreaks]}
        rehypePlugins={[
          RehypeKatex as any, // 解析HTML中的数学类,例如开平方根或者求最值
          [
            RehypeHighlight,
            {
              detect: false,
              ignoreMissing: true,
            },
          ],
        ]}
      >
        {props.content}
      </ReactMarkdown>
    </>
  );
}

export const MarkdownContent = React.memo(_MarkDownContent);

@wooorm
Copy link
Member

wooorm commented Jun 26, 2024

That seems very different from what the person reported here?

the mathematical formula is not surrounded by $$

You have markdown that uses one style of math.
This project supports a different style of math.
There is no standard for math in markdown.

What should I do?

No clue. This project doesn’t do, or attempt to do, what you want.

@wooorm
Copy link
Member

wooorm commented Jun 26, 2024

Closing this. OP was unclear what problem they had (like about how escapes work in JavaScript?), so I left it open for them to clarify.
Now everyone is sharing all their different potential problems here.
Please use different issues for your different problems.

@wooorm wooorm closed this as not planned Won't fix, can't repro, duplicate, stale Jun 26, 2024
@wooorm wooorm added the 🤷 no/invalid This cannot be acted upon label Jun 26, 2024

This comment has been minimized.

@github-actions github-actions bot added 👎 phase/no Post cannot or will not be acted on and removed 🤞 phase/open Post is being triaged manually 🙊 open/needs-repro This needs a reproduction labels Jun 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤷 no/invalid This cannot be acted upon 👎 phase/no Post cannot or will not be acted on
Development

No branches or pull requests

5 participants