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/MathJax implementation #529

Closed
4 tasks done
ngctnnnn opened this issue Feb 13, 2022 · 31 comments · Fixed by #2977
Closed
4 tasks done

KaTeX/MathJax implementation #529

ngctnnnn opened this issue Feb 13, 2022 · 31 comments · Fixed by #2977
Labels
build Related to the build system enhancement New feature or request has-workaround Has workaround, low priority

Comments

@ngctnnnn
Copy link

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
Copy link
Member

brc-dd commented Feb 13, 2022

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
Copy link

magnetenstad commented Mar 4, 2022

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
Copy link

Same error here

@brc-dd
Copy link
Member

brc-dd commented Jun 9, 2022

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

@brc-dd brc-dd reopened this Jun 9, 2022
@brc-dd
Copy link
Member

brc-dd commented Jun 9, 2022

This is the root issue BTW: vuejs/core#3746, was also reported on VuePress: vuepress/core#328

@leonbohmann
Copy link

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

@brc-dd
Copy link
Member

brc-dd commented Jun 9, 2022

@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 brc-dd closed this as completed Jun 9, 2022
@brc-dd
Copy link
Member

brc-dd commented Jun 22, 2022

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
Copy link

noClaps commented Jun 22, 2022

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
Copy link

wlbksy commented Jun 29, 2022

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

@noClaps
Copy link

noClaps commented Jun 29, 2022

@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
Copy link

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
Copy link

image

@brc-dd
Copy link
Member

brc-dd commented Sep 8, 2022

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
Copy link

qyingkou commented Jan 2, 2023

@brc-dd 👍
#529 (comment)
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
Copy link
Member

brc-dd commented Jan 2, 2023

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

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

@qyingkou
Copy link

qyingkou commented Jan 2, 2023

@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
Copy link
Member

brc-dd commented Jan 2, 2023

@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
Copy link

qyingkou commented Jan 2, 2023

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
Copy link
Member

brc-dd commented Jan 2, 2023

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

@brc-dd brc-dd reopened this Jan 2, 2023
@brc-dd brc-dd added enhancement New feature or request build Related to the build system labels Jan 2, 2023
@brc-dd brc-dd added the has-workaround Has workaround, low priority label Jan 2, 2023
@qyingkou
Copy link

qyingkou commented Jan 2, 2023

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
Copy link
Member

brc-dd commented Jan 2, 2023

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
Copy link

qyingkou commented Jan 2, 2023

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

@wlbksy
Copy link

wlbksy commented Jan 4, 2023

docusaurus makes LaTeX default

@brc-dd brc-dd changed the title LaTeX implementation KaTeX/MathJax implementation Feb 2, 2023
@nkbai
Copy link

nkbai commented Mar 30, 2023

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

Locietta added a commit to Locietta/blog-lost-pieces that referenced this issue May 2, 2023
@wlbksy
Copy link

wlbksy commented Jul 5, 2023

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
Copy link

Fanisting commented Jul 11, 2023

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
Copy link

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

@brc-dd
Copy link
Member

brc-dd commented Sep 20, 2023

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

@theadanielskocher
Copy link

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
Copy link
Member

brc-dd commented Sep 20, 2023

Add it inside your defineConfig call.

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

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 28, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
build Related to the build system enhancement New feature or request has-workaround Has workaround, low priority
Projects
None yet
Development

Successfully merging a pull request may close this issue.