LaTeX无法使用$$语法
现象
在使用 rspress-plugin-katex 插件时无法使用以下语法
$$
a \times b
$$
报错:MDX compile error: Cannot read properties of undefined (reading 'mathFlowInside')
原因
可能的主要原因是remark-math的版本过高,移除了对$$语法的支持
说可能,是因为我在remark-math仓库中并没有看到相关说明,但我降低remark-math的版本确实可以解决当前问题。
解决方法
方法 1:降低 remark-math 版本,重写插件
-
手动安装低版本
remark-mathnpm i remark-math@5 -
import { PresetConfigMutator } from 'rspress-plugin-devkit'; import remarkMath, { Options as RemarkMathOptions } from 'remark-math'; import rehypeKatex, { Options as RehypeKatexOptions } from 'rehype-katex'; import type { RspressPlugin } from '@rspress/shared'; export interface RspressPluginKatexOptions extends RemarkMathOptions, RehypeKatexOptions {} export function rspressPluginKatex(options: RspressPluginKatexOptions = {}): RspressPlugin{ const katexCss = require.resolve('katex/dist/katex.min.css'); return { name: 'rspress-plugin-katex', config(config, utils, isProd) { return new PresetConfigMutator(config).disableMdxRs().toConfig(); }, globalStyles: katexCss, markdown: { remarkPlugins: [[remarkMath, options]], // @ts-expect-error rehypePlugins: [[rehypeKatex, options]] } }; } -
导入组件
import { rspressPluginKatex } from './plugins.ts'; export default defineConfig({ plugin: [rspressPluginKatex()] });
方法 2:更改语法
$$
...
$$
更改为
```math
...
```