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

Feature: Raw pass-through of math for MathJax / KaTeX etc. #799

Closed
wants to merge 1 commit into from
Closed

Feature: Raw pass-through of math for MathJax / KaTeX etc. #799

wants to merge 1 commit into from

Conversation

ViktorQvarfordt
Copy link

@ViktorQvarfordt ViktorQvarfordt commented Aug 28, 2016

Add an option math which, when enabled, disables all processing of math wrapped in $..$, \(..\), \[..\], $$..$$ or \begin{}..\end{}. For use with MathJax / KaTeX etc.

Similar to #722 and #723, but better (in my opinion).

@ViktorQvarfordt
Copy link
Author

ViktorQvarfordt commented Aug 28, 2016

Perhaps it would be nice to allow more fine-grained control by changing the option math to an Array (instead of Bool) and renaming it to mathDelimiters. It is the list of delimiters; mathDelimiters: [["$", "$"], ["\\[", "\\]"]]. However, \begin{}..\end{} is more complicated and must be treated separately, the option for this could simply be "beginend", i.e. mathDelimiters: [["$", "$"], ["\\[", "\\]"], "beginend"].

@ViktorQvarfordt
Copy link
Author

ViktorQvarfordt commented Aug 28, 2016

The commit has been updated to use the above discussed more flexible variant.

Since the option mathDelimiters is not a Bool, I could not write tests using the existing test suite. However, the following serves a s a good test.

foo $a_1b_{2}\$a_1b_{2}$ bar

foo \(a_1b_{2}\) bar

foo \begin{align}
  \begin{cases}
    a_1b_{2}
  \end{cases}
  a_1b_{2}
\end{align} bar

foo \[
  \begin{cases}
    a_1b_{2}
  \end{cases}
  a_1b_{2}
\] bar

foo $$
  \begin{cases}
    a_1b_{2}
  \end{cases}
  a_1b_{2}
$$ bar

Expected HTML:

<p>foo $a_1b_{2}\$a_1b_{2}$ bar</p>
<p>foo \(a_1b_{2}\) bar</p>
<p>foo \begin{align}
  \begin{cases}
    a_1b_{2}
  \end{cases}
  a_1b_{2}
\end{align} bar</p>
<p>foo \[
  \begin{cases}
    a_1b_{2}
  \end{cases}
  a_1b_{2}
\] bar</p>
<p>foo $
  \begin{cases}
    a_1b_{2}
  \end{cases}
  a_1b_{2}
$ bar</p>

@ViktorQvarfordt
Copy link
Author

If we get this merged, it would be nice to add marked to this list http://docs.mathjax.org/en/latest/misc/mathjax-in-use.html.

@anaderi
Copy link

anaderi commented Nov 6, 2016

looks like a cool addition! any objections for merging it?

UPD: ah, I see: f0fd264

@wookayin
Copy link

wookayin commented Dec 19, 2016

It looks good anyway. Any updates, BTW?

@joeky888
Copy link

@wookayin I found that markdown-here forked this project and it support LaTeX.

Then I copied his marked.js, then

marked.setOptions({
        renderer: new marked.Renderer(),
        gfm: true,
        tables: true,
        breaks: true,
        pedantic: true,
        sanitize: true,
        smartLists: true,
        smartypants: true,
        math: function mathify(mathcode) {
            return '<img src="https://chart.googleapis.com/chart?cht=tx&chl={urlmathcode}" alt="{mathcode}">'
                    .replace(/\{mathcode\}/ig, mathcode)
                    .replace(/\{urlmathcode\}/ig, encodeURIComponent(mathcode));
        }
});

Usage: ${YOUR_LaTeX_Code}$
Ex:
$E = \frac{mc^2}{\sqrt{1-\frac{v^2}{c^2}}}$

;)

@joeky888
Copy link

If you want to keep it offline, you should use this PR.

@hgustafsson
Copy link

hgustafsson commented Jan 27, 2017

@ViktorQvarfordt Great work!

Since you are already parsing the input, do you think you could return the delimeters and the contained TeX separately? This would allow for easy renderers like:

renderer.math = function (begin, tex, end) {
    if ( begin === "$" || begin === "\\(" )
        return katex.renderToString(tex, {displayMode: false});
    else if ( begin === "$$" || begin === "\\[" )
        return katex.renderToString(tex, {displayMode: true});
    else
        return katex.renderToString(begin + tex + end, {displayMode: true});
    }                
}

without further parsing.

@ViktorQvarfordt
Copy link
Author

That's a cool idea! I've updated the PR to reflect this. But I'm lazy and ignored extracting the begin and end tags properly, see the code, there is a TODO on line 617 . Feel free to do this!

Note: I also rebased this PR on top of the latest chjj/marked.

@BOT-Man-JL
Copy link

BOT-Man-JL commented Apr 8, 2017

Hello @chjj,
Please solve this problem.
I'm suffering _ rendered to <em> ... 😅 #336

@joshbruce
Copy link
Member

Closing due to merge conflicts - flagging with #936 for future reference.

@joshbruce joshbruce closed this Jan 21, 2018
@julien-c
Copy link
Contributor

Only a partial solution to the use case described in this PR, but if you're doing server-side rendering of Math (for instance using KaTeX), why don't you:

  • first parse the math,
  • replace it with HTML,
  • and finally render using marked?

@julien-c julien-c mentioned this pull request Jun 19, 2020
@UziTech
Copy link
Member

UziTech commented Sep 7, 2022

Katex has been something people have been asking for for a while so I just created an extension and published it to npm.

marked-katex-extension

@Demi1024
Copy link

Hello @chjj, Please solve this problem. I'm suffering _ rendered to \<em\> ... 😅 #336

same here,any update?

@calculuschild
Copy link
Contributor

@Demi1024 Underscores around text are supposed to turn into <em> tags. This is part of the Commonmark spec. https://spec.commonmark.org/0.30/#emphasis-and-strong-emphasis

If that is not what you are referring to, could you be more specific, or create a new issue describing your problem?

@Demi1024
Copy link

@Demi1024 Underscores around text are supposed to turn into \<em\> tags. This is part of the Commonmark spec. https://spec.commonmark.org/0.30/#emphasis-and-strong-emphasis

If that is not what you are referring to, could you be more specific, or create a new issue describing your problem?

I don't want to break the syntax of Markdown, but is it possible to skip the parsing of underscores when inserting mathematical formulas in Markdown?

Parsing underscores as emphasis will cause the conversion of mathematical formulas to fail, as shown in the image below.
image

Is it possible to output mathematical formulas as they are without any conversion, for example, when encountering segments that start and end with $$?

@UziTech
Copy link
Member

UziTech commented Aug 17, 2023

Is it possible to output mathematical formulas as they are without any conversion, for example, when encountering segments that start and end with $$?

Yes, if you use https://www.npmjs.com/package/marked-katex-extension.

@UziTech
Copy link
Member

UziTech commented Aug 17, 2023

Or if you don't want to use katex you can also use code blocks or html blocks, both of which won't convert the contents with markdown.

<span>
_test_
</span>

produces:

_test_

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

Successfully merging this pull request may close these issues.