goessner / markdown-it-texmath Goto Github PK
View Code? Open in Web Editor NEWSupport TeX math equations with your Markdown documents.
License: MIT License
Support TeX math equations with your Markdown documents.
License: MIT License
Support Doxygen formula delimiter syntax.
Add the following syntax option:
name: "doxygen"
inline formula: \f$...\f$
block formula: \f[ ... \f]
The "environment style" \f{env}{...\f} does not need to be supported at the moment.
Currently the style of Doxygen is not supported. This is too bad, because Markdown documents can be parsed by Doxygen with Doxygen's advanced syntax.
The formula \f$v_{x} = 0\f$ shall be shown rendered by Katex in the VS code preview.
\f[
1 + 1 = 2
\f]
Operating system: ___
VSCode version: ___
Markdown+Math version: ___
When using kramdown
, $$K$$ $$E_1$$ $$E_2$$ ... $$E_K$$
is parsed as K
This is my code:
const tm = require('markdown-it-texmath');
export const markdownEngine = require('markdown-it')({ html: true })
.use(tm, {
engine: require('katex'),
delimiters: 'kramdown',
katexOptions: { macros: { "\\RR": "\\mathbb{R}" } }
});
Markdown to be parsed:
输入第一行给出正整数 $$N$$($$\le 1000$$),为居民区住房总套数;正实数 $$e$$,即低电量阈值;正整数 $$D$$,即观察期阈值。随后 $$N$$ 行,每行按以下格式给出一套住房的用电量数据:
$$K$$ $$E_1$$ $$E_2$$ ... $$E_K$$
其中 $$K$$ 为观察的天数,$$E_i$$ 为第 $$i$$ 天的用电量。
Result:
<p>输入第一行给出正整数 <eq><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>N</mi></mrow><annotation encoding="application/x-tex">N</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord mathnormal" style="margin-right:0.10903em;">N</span></span></span></span></eq>(<eq><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo>≤</mo><mn>1000</mn></mrow><annotation encoding="application/x-tex">\le 1000</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.7719em;vertical-align:-0.136em;"></span><span class="mrel">≤</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.6444em;"></span><span class="mord">1000</span></span></span></span></eq>),为居民区住房总套数;正实数 <eq><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>e</mi></mrow><annotation encoding="application/x-tex">e</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal">e</span></span></span></span></eq>,即低电量阈值;正整数 <eq><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>D</mi></mrow><annotation encoding="application/x-tex">D</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord mathnormal" style="margin-right:0.02778em;">D</span></span></span></span></eq>,即观察期阈值。随后 <eq><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>N</mi></mrow><annotation encoding="application/x-tex">N</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord mathnormal" style="margin-right:0.10903em;">N</span></span></span></span></eq> 行,每行按以下格式给出一套住房的用电量数据:</p>
<section><eqn><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mi>K</mi></mrow><annotation encoding="application/x-tex">K</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord mathnormal" style="margin-right:0.07153em;">K</span></span></span></span></span></eqn></section><p>其中 <eq><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>K</mi></mrow><annotation encoding="application/x-tex">K</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord mathnormal" style="margin-right:0.07153em;">K</span></span></span></span></eq> 为观察的天数,<eq><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>E</mi><mi>i</mi></msub></mrow><annotation encoding="application/x-tex">E_i</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8333em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.05764em;">E</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3117em;"><span style="top:-2.55em;margin-left:-0.0576em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">i</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></eq> 为第 <eq><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>i</mi></mrow><annotation encoding="application/x-tex">i</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6595em;"></span><span class="mord mathnormal">i</span></span></span></span></eq> 天的用电量。</p>
I'm guessing this is from the new regexes: in dollars
mode, $$x^2$$
omits the 2
when calling KaTeX, so doesn't render.
It looks like /\${2}([^]*?)[^\\]\${2}/gy
should be /\${2}([^]*?[^\\])\${2}/gy
.
Maybe worth double-checking the other regexes.
The use of the y
flag in regular expressions means the plugin is not compatible with React Native (which as of now doesn't support it and throws an error Invalid flags supplied to RegExp constructor
). Maybe something to mention in the doc?
I believe you can't use more than 1 delimiter atm? I usually use $ for inline math and [...] for display math. It would be great if you could use both! Thanks.
Julia's Documenter.jl package uses $...$
and ``.. .``
for inline markdown math, and ```math ... ```
for blocks.
Is there any possibility of adding this mix as an option? I know I can modify this locally, but I'm using a VSCode extension that bundles markdown-it-texmath to highlight Julia markdown files, and the extension can overwrite my changed texmath.js file when it updates...
Is it possible to define your own math delimiters? I'm currently writing for a site that requires all LaTeX (both inline and multiline) to be delimited by double dollar signs, but I can't figure out how (or if it's even possible) to do so.
I assume #28 was closed by accident? Does the VS Code plug-in now support \f$ and \f[ \f] as delimiters?
Hi, I am wondering how to insert a change line in math environment?
I am using delimeter $$ and want to write a two line equation.
However, the symbol // just broke the mathmode..
thx for help 👍
Firstly, thank you so much for this awesome plugin. It is what powers my large parts of my new non-profit.
This feature request for making this plugin compatible with pandoc (the best/most-widely known open-source document converter).
A large portion of our project is to convert latex documents into markdown so that they can be used on our website (eg see bit.ly/sphz-art ). To do this, we use pandoc to convert latex to markdown and use the markdown-it-texmath for rendering.
However, there are a couple of major issues when rendering pandoc output:
For example both these issues show up in the following text generated by pandoc:
The upper bound is $${\ell({x^n})} \leq |n| \, {\ell({x})}$$ for all $n \in
{\mathbb{Z}}$.
I would be really glad if you could provide support for this.
I have a number of files in gitlab which uses:
```math
a^2+b^2=c^2```
How do I tell vscode goessner.mdmath to preview these sections as math please?
Can you please show me an example to set:
"mdmath.macros": {
??
}
I've used the default settings. After rendering, aaa $x$ aaa
should become aaa x aaa
. However, it becomes aaaxaaa
. For a temporary solution, I added a css eq{margin: 0 5px;}
but it's not good! Any idea about this problem?
I am not sure if this would be a vuepress
or markdown-it-texmath
issue. Vuepress is a static site generator that uses markdown-it
to render HTML content.
The vuepress
dev server starts-up really slowly when using markdown-it-texmath
, though regular builds (for production) are not as slow. It seems that they are using webpack-dev-server
under the hood. After the initial build, subsequent refreshes are quicker, i.e. they seem to be using some sort of cache.
I'm not sure how to go about debugging this, but any ideas on what the hold-up might be or suggestions for where to look?
Here is my code:
// init the markdown object (with code highlight)
var md = require('markdown-it')({
highlight: (str, lang) => {
if (lang && highlight.getLanguage(lang)) {
try {
return highlight.highlight(lang, str).value
} catch (__) {
// ...
}
}
return ''
}
}).use(markdownTexmath, {
engine: require('katex'),
delimiters:'dollars',
katexOptions: {macros: {"\RR": "\mathbb{R}"}}
})
But it raise a error like:
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
Which loader should I install?
Thanks for the great work with this package.
Currently, Kramdown with Mathjax only supports $$ for both inline and block latex rendering, since the argument is that a $ is too pervasive and might inadvertently convert to latex what wasn't supposed to be converted e.g. $25 worth of fruits
.
It would be nice to have the option of putting inline math as $$ so that one can then publish the Markdown file to a Jekyll page directly.
If spaces are after the left dollar, the string isn't parse to TeX math.
md().use(tm).render("$a$") // <p><eq>...</eq></p>
md().use(tm).render("$ a$") // <p>$ a$</p>
Hi,
I am using markdown-it-texmath
in a project(Thank you!) with 'bracket' as the delimiter. Would like to ask if it is currently possible for users to specify more than one of the supported delimiters to use interchangeably.
E.g. possible to do something like delimiters: ['dollars', 'brackets']
?
const tm = require('markdown-it-texmath');
const md = require('markdown-it')({html:true})
.use(tm, { engine: require('katex'),
delimiters: 'dollars',
katexOptions: { macros: {"\\RR": "\\mathbb{R}"} } });
Thanks!
the following markdown can't be rendered correctly:
md.markdown = '# Triangle \n\
\n\
Let the right triangle hypothenuse be aligned with the coordinate system *x-axis*. \n\
The vector loop closure equation then reads\n\
\n\
\\[a{\\bold e}_\\alpha + b\\tilde{\\bold e}_\\alpha + c{\\bold e}_x = \\bold 0\\] (1)\n\
\n\
with\n\
\n\
\\[{\\bold e}_\\alpha = \\begin{pmatrix}\\cos\\alpha\\\\ \\sin\\alpha\\end{pmatrix} \\quad and \\quad {\\tilde\\bold e}_\\alpha = \\begin{pmatrix}-\\sin\\alpha\\\\ \\cos\\alpha\\end{pmatrix}\\]\n\
\n\
Resolving for the hypothenuse part \\(c{\\bold e}_x\\) in the loop closure equation (1) \n\
\n\
\\[-c{\\bold e}_x = a{\\bold e}_\\alpha + b\\tilde{\\bold e}_\\alpha\\] (2)\n\
\n\
and squaring \n\
\n\
finally results in the Pythagorean theorem (2)\n\
\n\
\\[ c^2 = a^2 + b^2 \\] (3)'
but using the $ $$ syntax would work.
I'm using prettier to re-format a markdown document as I save.
Suppose you have a:
Very long line with a math mode in somewhere at the curcial point where it cuts $(4,2) \overset{b}{\rightarrow} (5,5)$ end of the line.
And the formatters don't care about
And they re-format it like this:
Very long line with a math mode in somewhere at the curcial point where it cuts $(4,2)
\overset{b}{\rightarrow} (5,5)$ end of the line.
The math mode breaks itself because there is a newline middle of it.
There is a couple of fixes I can think of:
Make markdown-it-texmath to remove newlines in inline
Fix prettier to treat
P.S. I probably try to create issue in Prettier repo too, since math is probably one of most used extensions of markdown. Maybe they will do something to it so that it won't break it.
Especially
Latex blocks only work when they are preceded by an empty line. This behavior is not consistent with other blocks (e.g. code, table, etc.).
For example, the first math block in the following Markdown document won't render:
Code block works:
``` js
var foo = function (bar) {
return bar++;
};
console.log(foo(5));
```
Table works:
| Option | Description |
| ------ | ----------- |
| data | path to data files to supply the data that will be passed into templates. |
| engine | engine to be used for processing templates. Handlebars is the default. |
| ext | extension to be used for dest files. |
Math block doesn't work:
$$\frac{1}{2}$$
Unless an empty line precedes it:
$$\frac{1}{2}$$
I'm not aware of a discussion board, but just wanted to say thanks for this plugin. I just integrated it extensively into https://cocalc.com in this commit.
Regarding "Why doesn't markdown-it-texmath work with other engines ?" from the README, I needed to make this also work with mathjax (though we mainly use katex), and found it very easy to do so by using this option when creating the plugin:
engine: {
renderToString: (tex, options) => {
// We need to continue to support rendering to MathJax as an option,
// but texmath only supports katex. Thus we output by default to
// html using script tags, which are then parsed later using our
// katex/mathjax plugin.
return `<script type="math/tex${
options.displayMode ? "; mode=display" : ""
}">${tex}</script>`;
},
},
I also needed to parse math environments that is determined by \begin/\end environments, but no dollar signs at all, e.g.,
Consider
\begin{equation}
x^3
\end{equation}
None of the delimiters you have support that, so I just extended the built in dollars one via a new delimiter called "cocalc" that does right after loading as follows. This didn't require changing the code of this extension at all. The regular expression below took me a while to come up with:
// The markdown-it-texmath plugin is very impressive, but it doesn't parse
// things like \begin{equation}x^3$\end{equation} without dollar signs.
// However, that is a basic requirement for cocalc in order to preserve
// Jupyter classic compatibility. So we monkey patch the plugin
// and extend the regexps to also recognize these. We do this with a new
// delim object, to avoid any potential conflicts.
texmath.rules["cocalc"] = { ...texmath.rules["dollars"] };
texmath.rules["cocalc"].block.push({
name: "math_block",
rex: /(\\(?:begin)\{[a-z]*\*?\}[\s\S]*?\\(?:end)\{[a-z]*\*?\})/gmy, // regexp to match \begin{...}...\end{...} environment.
tmpl: "<section><eqn>$1</eqn></section>",
tag: "\\",
});
In any case, many thanks for writing (and maintaining!) this plugin.
Hi!
I'm trying to use markdown-it-texmath with markdown-it-attrs. But I can't apply the attribute to displayed math. Am I missing something ?
I tried these ways:
\[
f(x) = \sqrt{3x-2} + \frac{2}{x}
\]{.make-it-nice}
(adding a little space)
\[
f(x) = \sqrt{3x-2} + \frac{2}{x}
\] {.make-it-nice}
on the next line
\[
f(x) = \sqrt{3x-2} + \frac{2}{x}
\]
{.make-it-nice}
on two lines below (and even tried 3 lines)
\[
f(x) = \sqrt{3x-2} + \frac{2}{x}
\]
{.make-it-nice}
Notice that it is working correctly for inline math.
Thanks for your help !
Hello, I was wondering if it would be possible to use \\[
\\]
and \\(
and \\)
as delimiters instead of their single backslash counterparts.
I am using Hugo to render my website and it uses \
as an escape character. So my markdown texts need \\[
as a delimiter for MathJAX (within Hugo) to recieve \[
I find this usage at KaTeX website
When I use \colorbox{aqua}{$F=ma$}
or \fcolorbox{red}{aqua}{$F=ma$}
, it also works in vscode, but it looks bad in browser. like this
Is there a way to use double dollar (display style) delimiters in tables?
e.g., this doesn't work:
| key | formula | notes |
|-----|---------|-------|
| node_density | $$\sum_{j\neq{i}} w_{j}$$ | The default $w=1$ reduces ... |
But the single-dollar (inline) delimiter still seems to work fine.
I had to change "mdmath.delimiters": "'dollars'"
to "mdmath.delimiters": "dollars"
(i.e., remove single quotes within double quotes around dollars) in settings.json in order to render as expected math in VS Code on Ubuntu 18.04. Before removing the single quotes, TeX syntax just showed up as typed in the source.
I used the library 3 weeks ago, and it worked very well.
I started refactoring, updated libraries, and there was a problem.
Katex parameters are duplicated as text.
<span class="katex">
<span class="katex-mathml"></span>
<span class="katex-html" aria-hidden="true"></span>
</span>
it turns out that something like this ABCABC
Since it requires katex
and markdown-it
, I think it had better add katex and markdown-it in dependencies
not devDependencies
The regex for dollar delimiters breaks if the content includes an escaped dollar (\$
), making it impossible to type dollar literals inside math blocks:
$$
\$10.00
$$
$\$10.00$
will all fail to be parsed as a latex block.
Workarounds include using a different delimiter style or use \text{\textdollar}
, but getting it to work without these would be nice.
I've been to update the math_block
regex from:
/\${2}([^$]+?)\${2}/gmy
to
/\${2}((?:\\\$|[^$])+?)\${2}/gmy
but it doesn't cover all cases, and I haven't been able to figure out where to start with the math_inline
regex.
I run this:
const tm = require('markdown-it-texmath');
const md = require('markdown-it')({html:true})
.use(tm, { engine: require('katex'),
delimiters: 'dollars',
katexOptions: { macros: {"\\RR": "\\mathbb{R}"} } });
const str =
`
something
$$
1+1=2
$$
`;
console.log(md.render(str));
Then I got this:
<p>something</p>
<section>
<eqn><span class="katex-display"><span class="katex"><span class="katex-mathml"><math
xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mn>1</mn><mo>+</mo><mn>1</mn><mo>=</mo><mn>2</mn></mrow><annotation
encoding="application/x-tex">
1+1=2
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span
class="strut" style="height:0.7278em;vertical-align:-0.0833em;"></span><span class="mord">1</span><span
class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">+</span><span class="mspace"
style="margin-right:0.2222em;"></span></span><span
class="base"><span class="strut" style="height:0.6444em;"></span><span class="mord">1</span><span
class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace"
style="margin-right:0.2778em;"></span></span><span
class="base"><span class="strut" style="height:0.6444em;"></span><span
class="mord">2</span></span></span></span></span></eqn>
</section>
<!-- redundant -->
</math>
But if I delete the blank line before
const str =
`
something
$$
1+1=2
$$
`;
console.log(md.render(str));
I got a correct result:
<p>
<!-- "something" isn't wrapped by p tags in this case -->
something
<section>
<eqn><span class="katex-display"><span class="katex"><span class="katex-mathml"><math
xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mn>1</mn><mo>+</mo><mn>1</mn><mo>=</mo><mn>2</mn></mrow><annotation
encoding="application/x-tex">
1+1=2
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span
class="strut" style="height:0.7278em;vertical-align:-0.0833em;"></span><span class="mord">1</span><span
class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">+</span><span class="mspace"
style="margin-right:0.2222em;"></span></span><span
class="base"><span class="strut" style="height:0.6444em;"></span><span class="mord">1</span><span
class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace"
style="margin-right:0.2778em;"></span></span><span
class="base"><span class="strut" style="height:0.6444em;"></span><span
class="mord">2</span></span></span></span></span></eqn>
</section>
</p>
$$
\_
$$
not handled properly according to KaTeX docs.
I like having a space as padding within the math environment. Ex: $ 2 $ instead of
The backslash works with markdown-it-katex however.
I have following content:
123
\[
H = \Delta \begin{bmatrix}
0 & 1 \\
1 & 0
\end{bmatrix},
\]
And the current result will be:
123 [ H = \Delta \begin{bmatrix} 0 & 1 \ 1 & 0 \end{bmatrix}, ]
But the expected result should be:
When I am trying to fix this, just modify the content like this:
123
\[
H = \Delta \begin{bmatrix}
0 & 1 \\
1 & 0
\end{bmatrix},
\]
And it will produce the correct result.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.