Skip to content

Support for inline math using LaTeX #289

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

Closed
Yeeler opened this issue Apr 28, 2018 · 14 comments
Closed

Support for inline math using LaTeX #289

Yeeler opened this issue Apr 28, 2018 · 14 comments

Comments

@Yeeler
Copy link

Yeeler commented Apr 28, 2018

I have quite lot of markdown files which contain many latex equations.
How can I get them show up properly in vuepress?

e.g.
$J \left( \theta_0, \theta_1 \right) = \frac{1}{2m}\sum\limits_{i=1}^m \left( h_{\theta}(x^{(i)})-y^{(i)} \right)^{2}$

@imfing
Copy link

imfing commented Apr 29, 2018

You can customize by yourself.
There are instructions here 👉 #113

@Yeeler
Copy link
Author

Yeeler commented Apr 29, 2018

Latex has different syntax from Katex. Markdown-it-katex didn't work on latex. I tried many other markdown plugins, including markdown-it-latex, markdown-it-mathjax, amd markdown-it-texmath etc. No luck.

When I was previewing markdown files in VS Code, I can see the above equation with a plug-in named 'markdown-math' installed. Why l can't do the same in Vuepress?

My current solution is using https://www.codecogs.com/eqnedit.php to insert an equation image. Is there any better choice? How can I show latex equation in node JS? What package should I use?

@imfing
Copy link

imfing commented Apr 29, 2018

Katex is a library for TeX math rendering, not a new kind of TeX syntax.

Following the guide in #113 , you'll get your inline math equation parsed and rendered correctly.

image

For all the functions supported in Katex, you may check its official doc KaTeX/function-support

It's enough for you to handle 99% of the cases like the cost function shown above.

@Yeeler
Copy link
Author

Yeeler commented Apr 29, 2018

I don't know how to add Katex CSS into my theme, but I did all the other steps. And I got
image

this is my markdown file:

Latex demo

$J \left( \theta_0, \theta_1 \right) = \frac{1}{2m}\sum\limits_{i=1}^m \left( h_{\theta}(x^{(i)})-y^{(i)} \right)^{2}$

Can you please provide your vuepress project? Thanks for your help.

@imfing
Copy link

imfing commented Apr 29, 2018

Seems that you didn't include the <link> in your markdown file.

  • Add the css in your page README.md
# This is a markdown file

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.5.1/katex.min.css">

<link rel="stylesheet" href="https://cdn.jsdelivr.net/github-markdown-css/2.2.1/github-markdown.css"/>

$J \left( \theta_0, \theta_1 \right) = \frac{1}{2m}\sum\limits_{i=1}^m \left( h_{\theta}(x^{(i)})-y^{(i)} \right)^{2}$

I have created a sample skeleton in vuepress-homepage which might be helpful for your configuration.

The raw README.md file and the rendered page: Demo
Check the config.js of mine so you could have ideas of where to modify. 😉

@Yeeler
Copy link
Author

Yeeler commented Apr 29, 2018

you are right. I missed the css. I didn't know that we can apply css in markdown file in this way.

I copied default theme by run 'vuepress eject docs'
then I modified 'D:_GIT\vue01\docs.vuepress\theme\Layout.vue'
append '<style src="markdown-it-katex/node_modules/katex/dist/katex.min.css"></style>'

finally, all worked.

@meteorlxy
Copy link
Member

Add css in head field of config.js might be a better choice.

@ulivz
Copy link
Member

ulivz commented Apr 30, 2018

See: #113 (comment)

Closing as this can be done in userland.

@ulivz ulivz closed this as completed Apr 30, 2018
@OnceMore2020
Copy link

OnceMore2020 commented May 2, 2018

configure head in config.js, like this:

module.exports = {
    head: [
        ['link', { rel: 'stylesheet', href: 'https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.5.1/katex.min.css' }]
    ],
    ...

would be more elegant.

@Yeeler
Copy link
Author

Yeeler commented May 2, 2018

yeah I like it.

@faroit
Copy link

faroit commented May 15, 2018

I actually couldn't find the info that its possible to add custom headers without ejecting the theme. This is great! Maybe this could go into the documentation!

@faroit
Copy link

faroit commented May 15, 2018

actually couldn't find the info that its possible to add custom headers without ejecting the theme

🙈 I missed that, sorry

@RyanGreenup
Copy link

If anybody else is facing a similar problem and really wants to use MathJax rather than KaTeX (in order to support \label{} in an align environment rather than merely \tag{] in an \[ aligned \] environment for instance) a similar thing can be done by injecting the cdnjs MathJax script:

<script type="text/javascript" async
  src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML">
</script>

Which can be acheived in VuePress, as described in the manual by adding the following to the .vuepress/config.js:

head: [
  ['script',
    { type: '"text/javascript" async',
    src: 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML'
    }
  ]
],

This wouldn't be worth adding to this issue, but the new community vuepress-plugin-mathjax plugin does not render LaTeX in an aligned environment and the deprecated latex plugin doesn't quite render the math in a way that looks correct:

$$
\begin{aligned}
         0 &= ax^2 +bx +c
\implies x &= \frac{- b \pm \sqrt{b^2- 4ac} }{2a} 
\end{aligned}
$$

This method was suggestested but not detailed in #113 by songololo.

@BruceYuj
Copy link

BruceYuj commented Oct 9, 2020

@RyanGreenup I use your method to fix some syntax like $$\begin{cases} \end{cases}$$, but it didn't work. I use the community plugin - vuepress-plugin-mathjax and manually inject Mathjax script. Can you help me?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants