vuejs / vitepress

Vite & Vue powered static site generator.
https://vitepress.dev
MIT License
11.48k stars 1.86k forks source link

KaTeX/MathJax implementation #529

Closed ngctnnnn closed 8 months ago

ngctnnnn commented 2 years ago

Is your feature request related to a problem? Please describe.

I am frustrated and find it difficult in implement LaTeX onto the vitepress

Describe the solution you'd like

There are many packages on applying LaTeX to the website, e.g. Mathjax

Describe alternatives you've considered

No response

Additional context

No response

Validations

brc-dd commented 2 years ago

Use any KaTeX/MathJax markdown-it plugin. There shouldn't be much issue IG.


Here is an example:

yarn add markdown-it-katex

.vitepress/config.js:

module.exports = {
  markdown: {
    config: (md) => {
      md.use(require("markdown-it-katex"));
    },
  },
};

index.md:

---
head:
  - - link
    - rel: stylesheet
      href: https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.5.1/katex.min.css
---

# KaTeX Demo

$\sqrt{3x-1}+(1+x)^2$

Output:

image

magnetenstad commented 2 years ago

Using @brc-dd 's suggestion, I get this error when trying to build.

vitepress v0.22.3
✓ building client + server bundles...
✖ rendering pages...
build error:
 TypeError: Invalid value used as weak map key
    at WeakMap.set (<anonymous>)

Do you have a solution?

leonbohmann commented 1 year ago

Same error here

brc-dd commented 1 year ago

@leonbohmann @magnetenstad Hi! I'll look into that. It was working at that time. Something might have changed in the core.

brc-dd commented 1 year ago

This is the root issue BTW: https://github.com/vuejs/core/issues/3746, was also reported on VuePress: https://github.com/vuepress/vuepress-next/issues/328

leonbohmann commented 1 year ago

Did not find that issue, is the fix using the compilerOptions a valid thing? Did not try that yet..

brc-dd commented 1 year ago

@lukeromanowicz Okay, so this config should work:

import { defineConfig } from 'vitepress'
import markdownItKatex from 'markdown-it-katex'

const customElements = [
  'math',
  'maction',
  'maligngroup',
  'malignmark',
  'menclose',
  'merror',
  'mfenced',
  'mfrac',
  'mi',
  'mlongdiv',
  'mmultiscripts',
  'mn',
  'mo',
  'mover',
  'mpadded',
  'mphantom',
  'mroot',
  'mrow',
  'ms',
  'mscarries',
  'mscarry',
  'mscarries',
  'msgroup',
  'mstack',
  'mlongdiv',
  'msline',
  'mstack',
  'mspace',
  'msqrt',
  'msrow',
  'mstack',
  'mstack',
  'mstyle',
  'msub',
  'msup',
  'msubsup',
  'mtable',
  'mtd',
  'mtext',
  'mtr',
  'munder',
  'munderover',
  'semantics',
  'math',
  'mi',
  'mn',
  'mo',
  'ms',
  'mspace',
  'mtext',
  'menclose',
  'merror',
  'mfenced',
  'mfrac',
  'mpadded',
  'mphantom',
  'mroot',
  'mrow',
  'msqrt',
  'mstyle',
  'mmultiscripts',
  'mover',
  'mprescripts',
  'msub',
  'msubsup',
  'msup',
  'munder',
  'munderover',
  'none',
  'maligngroup',
  'malignmark',
  'mtable',
  'mtd',
  'mtr',
  'mlongdiv',
  'mscarries',
  'mscarry',
  'msgroup',
  'msline',
  'msrow',
  'mstack',
  'maction',
  'semantics',
  'annotation',
  'annotation-xml'
]

export default defineConfig({
  markdown: {
    config: (md) => {
      md.use(markdownItKatex)
    }
  },
  vue: {
    template: {
      compilerOptions: {
        isCustomElement: (tag) => customElements.includes(tag)
      }
    }
  }
})
brc-dd commented 1 year ago

Using MathJax is much easier actually. You just need to do this:

import mathjax3 from 'markdown-it-mathjax3';

const customElements = ['mjx-container'];

export default {
  markdown: {
    config: (md) => {
      md.use(mathjax3);
    },
  },
  vue: {
    template: {
      compilerOptions: {
        isCustomElement: (tag) => customElements.includes(tag),
      },
    },
  },
};

Complete demo: https://github.com/brc-dd/vitepress-mathjax3

Output:

image

noClaps commented 1 year ago

To add on to this, you can also use

mjx-container>svg {
    display: block;
    margin: auto;
}

in your CSS to center the equations like KaTeX did.

wlbksy commented 1 year ago

Could you provide a page on this LaTeX topic in the doc @noClaps , thanks !

noClaps commented 1 year ago

@wlbksy I'm sorry, I don't understand what you mean. The VitePress documentation doesn't have a page on LaTeX at the moment, and seeing as it's an external plugin, they probably won't add one.

AetaBeta commented 1 year ago

Using MathJax is much easier actually. You just need to do this:

import mathjax3 from 'markdown-it-mathjax3';

const customElements = ['mjx-container'];

export default {
  markdown: {
    config: (md) => {
      md.use(mathjax3);
    },
  },
  vue: {
    template: {
      compilerOptions: {
        isCustomElement: (tag) => customElements.includes(tag),
      },
    },
  },
};

Complete demo: https://github.com/brc-dd/vitepress-mathjax3

Output:

image why my latex fomula can not stay in one line with other text when it complied

AetaBeta commented 1 year ago
image
brc-dd commented 1 year ago

why my latex fomula can not stay in one line with other text when it complied

@AetaBeta You probably haven't added styles. See the linked repo. You need to set display: inline-block on mjx-container.

qyingkou commented 1 year ago

@brc-dd 👍 https://github.com/vuejs/vitepress/issues/529#issuecomment-1162767122 According to this issue, the configuration can indeed be successful, so that the mathematical formula can be displayed.

But the browser console shows a lot of warning messages,as follows:

image

How to deal with these warning messages, and how to make these unresolvable components be parsed correctly?

brc-dd commented 1 year ago

@qyingkou Refer my comment just above the one you linked 😅 https://github.com/vuejs/vitepress/issues/529#issuecomment-1151186631

It shouldn't be required for mathjax though. You need it only for katex.

qyingkou commented 1 year ago

@qyingkou Refer my comment on that issue just above the one you linked 😅 #529 (comment)

It shouldn't be required for mathjax though. You need it only for katex.

@brc-dd :Thank you for your reply, maybe I didn't make it clear. I use "markdown-it-mathjax3" package, I completely copied the demo folder structure and configuration content you wrote. brc-dd/vitepress-mathjax3

However, a lot of warning messages appeared, many components could not be resolved. Can you tell me how should I parse it correctly?

brc-dd commented 1 year ago

@qyingkou Ah, markdown-it-mathjax3 has added AssistiveMML extension in its latest version. You'll need to have a config like this:

import mathjax3 from 'markdown-it-mathjax3';

const customElements = [
  'mjx-container',
  'mjx-assistive-mml',
  'math',
  'maction',
  'maligngroup',
  'malignmark',
  'menclose',
  'merror',
  'mfenced',
  'mfrac',
  'mi',
  'mlongdiv',
  'mmultiscripts',
  'mn',
  'mo',
  'mover',
  'mpadded',
  'mphantom',
  'mroot',
  'mrow',
  'ms',
  'mscarries',
  'mscarry',
  'mscarries',
  'msgroup',
  'mstack',
  'mlongdiv',
  'msline',
  'mstack',
  'mspace',
  'msqrt',
  'msrow',
  'mstack',
  'mstack',
  'mstyle',
  'msub',
  'msup',
  'msubsup',
  'mtable',
  'mtd',
  'mtext',
  'mtr',
  'munder',
  'munderover',
  'semantics',
  'math',
  'mi',
  'mn',
  'mo',
  'ms',
  'mspace',
  'mtext',
  'menclose',
  'merror',
  'mfenced',
  'mfrac',
  'mpadded',
  'mphantom',
  'mroot',
  'mrow',
  'msqrt',
  'mstyle',
  'mmultiscripts',
  'mover',
  'mprescripts',
  'msub',
  'msubsup',
  'msup',
  'munder',
  'munderover',
  'none',
  'maligngroup',
  'malignmark',
  'mtable',
  'mtd',
  'mtr',
  'mlongdiv',
  'mscarries',
  'mscarry',
  'msgroup',
  'msline',
  'msrow',
  'mstack',
  'maction',
  'semantics',
  'annotation',
  'annotation-xml',
];

export default {
  markdown: {
    config: (md) => {
      md.use(mathjax3);
    },
  },
  vue: {
    template: {
      compilerOptions: {
        isCustomElement: (tag) => customElements.includes(tag),
      },
    },
  },
};
qyingkou commented 1 year ago

Like the configuration as Katex,thanks @brc-dd

It seems that there will be some components that cannot be parsed after updating the package in the future, just add the name of the component (that reported the error) to customElements.

brc-dd commented 1 year ago

Yeah. Should we add this to core? I guess using latex is a fairly popular demand and most of the other frameworks support it out of the box. /cc @kiaking

qyingkou commented 1 year ago

Latex is important for the expressiveness of document typesetting. More and more development documents need to explain algorithms, and algorithms need mathematical formulas. Let us see which lucky package gets integrated into the core...

brc-dd commented 1 year ago

Yeah, I agree. But the main problem is the package size. Adding it will nearly double VitePress' installation size! We probably can make it an optional peer dep.

qyingkou commented 1 year ago

I agree! I wonder which library you will choose. From the comparison of KateX and MathJax, the former seems to render faster. KateX

wlbksy commented 1 year ago

docusaurus makes LaTeX default

nkbai commented 1 year ago

it doesn't render multiline latex correctly, it works only for single line latex.

for example:

$$
H=H1(\alpha) \\
k=ECVRF\_nonce\_generation(SK,H) \\
\Gamma =H^x \\
c=H2(H,\Gamma,B^k,H^k) \\
s=k+cx
$$

render as : image

formula: $\Gamma=H^x$

render as :

image

wlbksy commented 10 months ago

Yeah, I agree. But the main problem is the package size. Adding it will nearly double the VitePress' installation size! We probably can make it an optional peer dep.

Could the team make this happen, please~

Fanisting commented 10 months ago

For the repo above, I still got the results that inline math is unavailable. I modified the existing CSS solution and it works. Here is the new code in mathjax3.css:

mjx-container {
  display: inline-block;
  margin: auto 2px -2px;
}

mjx-container > svg {
  margin: auto;
  display: inline-block;
}
theadanielskocher commented 7 months ago

I tried to install all of them in my project, but nothing works. How can I address this problem?

brc-dd commented 7 months ago

@theadanielskocher Refer https://vitepress.dev/guide/markdown#math-equations

theadanielskocher commented 7 months ago

Oh, when I add the code to turn on the math, it shows some errors: image

What do you think I should do?

brc-dd commented 7 months ago

Add it inside your defineConfig call.

export default defineConfig({
  title: '...',
  .... other stuff ....,
  markdown: {
    math: true
  }
})