MCPcopy Index your code
hub / github.com/Mathpix/mathpix-markdown-it

github.com/Mathpix/mathpix-markdown-it @v3.0.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v3.0.0 ↗ · + Follow
3,161 symbols 9,539 edges 826 files 316 documented · 10%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

mathpix-markdown-it

npm version Build Status GitHub

What is Mathpix Markdown?

mathpix-markdown is a superset of Markdown that adds helpful syntax for the STEM community, such as advanced equation, table, and chemistry support. Wherever possible, we borrow syntax from LaTeX. In other cases (such as chemistry) we invent new syntax that is backward compatible with Markdown.

Here are the key benefits over plain Markdown: - better equation support via LaTeX syntax (powered by MathJax), including equation numbering and referencing conventions from LaTeX - better support for tables, via the LaTeX tabular syntax, which allows for complex, nested tables often seen in scientific publications - advanced figure referencing via LaTeX syntax - support for abstracts, author lists, and linkable sections; these are a fact of life for academic publications - support for chemistry diagrams represented with SMILES markup, compatible with popular chemistry tools like ChemDraw

Editing an MMD file in VS Code

Mathpix Markdown Syntax reference

Click here for the full syntax reference.

How to edit mmd files?

Mathpix Markdown is an open format with multiple implementations:

  • you can use this Github repo and the mathpix-markdown-it npm library to render STEM content on your website
  • you can use the VS Code plugin (see picture above) to edit mmd files
  • use can use our web editor Snip Notes to edit, export, and publish mmd files (with exports to pdf and docx formats)
  • you can use our experimental static site generator Spectra to edit local mmd files and see changes in real time

How is Mathpix Markdown different from regular Markdown?

Mathpix Markdown addresses these limitations by adding support for the following standard Latex syntax elements which are already familiar to the scientific community:

  • inline math via \( <latex math> \)
  • block math via \[ <latex math> \] or $$ <math> $$
  • tables via \begin{tabular} ... \end{tabular}
  • figures and figure captions via \begin{figure} \caption{...} ... \end{figure}
  • lists: unordered lists via \begin{itemize} ... \end{itemize} and ordered lists via \begin{enumerate} ... \end{enumerate}
  • numbered and unnumbered equation enviornments \begin{elem} ... \end{elem} and \begin{elem*} ... \end{elem*} where elem=equation|align|split|gather
  • equation, table, and figure references via \label, \ref, \eqref, \tag
  • text formatting options \title{...}, \author{...}, \begin{abstract}...\end{abstract}, \section{Section Title}, \subsection{Section Title}, \subsubsection{Section Title}, \textit{italicized text}, \textbf{bold text}, \url{link}
  • chemistry equation via <smiles>OC(=O)c1cc(Cl)cs1</smiles> or
```smiles
OC(=O)c1cc(Cl)cs1
```
  • images (Markdown). Parse and render additional parameters such as width, height, alignment:
![foo](foo.png){ width=50% }
![foo](foo.png){ width="36px" }
![image](<src> "title"){width="20px",height="20px"}
![image](<src> "title"){width="20px",height="20px",right}
![image](<src> "title"){width="20px",height="20px", align="left"}

Image properties

\newtheorem{theorem}{Theorem}
\newtheorem{lemma}[theorem]{Lemma}

\begin{theorem}
Let \(f\) be a function whose derivative exists in every point, then \(f\) 
is a continuous function.
\end{theorem}

\begin{lemma}
Given two line segments whose lengths are \(a\) and \(b\) respectively there 
is a real number \(r\) such that \(b=ra\).
\end{lemma}

\begin{proof}
To prove it by contradiction try and assume that the statement is false,
proceed from there and at some point you will arrive to a contradiction.
\end{proof}

Footnote marker without text. Auto increment counter to 1 \footnotemark{} should be 1.

Footnote marker with text. Auto increment counter to 2 \footnotemark{} be 2. \footnotetext{text should be 2}

Auto increment counter to 3 \footnote{text  should be 3}

Auto increment counter to 4 \footnote{text  should be 4}

Footnote marker without text. Auto increment counter to 5 \footnotemark{} should be 5.

Footnote marker with text. Auto increment counter to 6 \footnotemark{} should be 6. \footnotetext{text should be 6}

Auto increment counter to 7 \footnote{text  should be 7}

Auto increment counter to 8 \footnote{text  should be 8}

Underline text: \underline{Underlined text!}
Underline text: \uline{Underlined text!}

Double underline text: \underline{\underline{Double underlined text!}}
Double underline text: \uuline{Double underlined text!}

Wavy underlined text: \uwave{This text is underlined with a wavy line!}

Dashed underline text: \dashuline{Dashed Underline}
Dotted underline text: \dotuline{Dotted Underline}

Strikethrough text: \sout{Text with a horizontal line through its center!}
Struck with Hatching text: \xout{Text with hatching pattern!}

\begin{lstlisting}[language=C, mathescape]
/* the following code computes $\displaystyle\sum_{i=1}^{n}i$ */

for (i = 1; i <= limit; i++) {
  sum += i;
}
\end{lstlisting}

What is mathpix-markdown-it?

mathpix-markdown-it is an open source implementation of the mathpix-markdown spec written in Typescript.

It relies on the following open source libraries:

  • MathJax v3 (to render math with SVGs)
  • markdown-it (for standard Markdown parsing)

Quickstart

Installation

npm usage:

$ npm install mathpix-markdown-it

yarn usage:

$ yarn add mathpix-markdown-it

How to use

React usage

mathpix-markdown-it components usage

We provide React components which make rendering of mathpix-markdown-it easy for React applications: Full example

import {MathpixMarkdown, MathpixLoader} from 'mathpix-markdown-it';


class App extends Component {
  render() {
    return (
      <MathpixLoader>
          <MathpixMarkdown text="\\(ax^2 + bx + c = 0\\)"/>
          <MathpixMarkdown text="$x = \frac { - b \pm \sqrt { b ^ { 2 } - 4 a c } } { 2 a }$"/>
          ...
      </MathpixLoader>
    );
  }
}

MathpixMarkdownModel methods usage

Example of render() method usage

import * as React from 'react';
import { MathpixMarkdownModel as MM } from 'mathpix-markdown-it';

class App extends React.Component {
  componentDidMount() {
    const elStyle = document.getElementById('Mathpix-styles');
    if (!elStyle) {
      const style = document.createElement("style");
      style.setAttribute("id", "Mathpix-styles");
      style.innerHTML = MM.getMathpixFontsStyle() + MM.getMathpixStyle(true);
      document.head.appendChild(style);
    }
  }
  render() {
    const html = MM.render('$x = \\frac { - b \\pm \\sqrt { b ^ { 2 } - 4 a c } } { 2 a }$');
    return (











    )
  }
}

export default App;

Example of markdownToHTML() method usage

class ConvertForm extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      value: '\\[\n' +
        'y = \\frac { \\sum _ { i } w _ { i } y _ { i } } { \\sum _ { i } w _ { i } } , i = 1,2 \\ldots k\n' +
        '\\]',
      result: ''
    };

    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
  }

  handleChange(event) {
    this.setState({value: event.target.value});
  }

  handleSubmit(event) {
    event.preventDefault();
    this.setState({result: MM.markdownToHTML(this.state.value)})
  }

  render() {
    return (



        <form onSubmit={this.handleSubmit}>
          <h1>Input Text with Latex:</h1>
          <textarea value={this.state.value} onChange={this.handleChange} />
          <input type="submit" value="Convert" />
        </form>






    );
  }
}

Latex to mathml/asciimath/tsv conversion

Example of Latex to mathml/asciimath/tsv conversion

Rendering methods have the ability to convert Latex representation to such formats as: mathml, asciimath, tsv

const options = {
      outMath: { //You can set which formats should be included into html result
        include_mathml: true,
        include_asciimath: true,
        include_latex: true,
        include_svg: true, // sets in default
        include_tsv: true,
        include_table_html: true, // sets in default
      }
    };
const html = MathpixMarkdownModel.markdownToHTML(`$x^x$`, options);

markdownToHTML() returns an HTML string that will contain the formats specified in the options.

For Latex formulas, the result will be:

<span class="math-inline">
    <mathml style="display: none">...</mathml>
    <asciimath style="display: none">...</asciimath>
    <latex style="display: none">...</latex>
    <mjx-comtainer class="MathJax" jax="SVG">..</mjx-comtainer>
</span>

For tabular, the result will be:




    <table id="tabular">...</table>
    <tsv style="display: none">...</tsv>



Then calling the parseMarkdownByHTML(html) method will return all formats as a list from the incoming html string.

For Latex formulas:

[
    {
      "type": "mathml",
      "value": "<math>...</math>"
    },
    {
       "type": "asciimath",
       "value": "x^(x)"
     },
    {
       "type": "latex",
       "value": "x^x"
     },
    {
       "type": "svg",
       "value": "<sgv>...</svg>"
     }
]

For tabular:

[
    {
      "type": "html",
      "value": "<table>...</table>"
    },
    {
       "type": "tsv",
       "value": "<tsv>...</tsv>"
     }
]

Example of outMath option usage

For Latex formulas:

const options = {
      outMath: {
        include_mathml: false,
        include_asciimath: true,
        include_latex: false,
      }
    };
const latex = `$x^x$`;
const html = MathpixMarkdownModel.markdownToHTML(latex, options);
const parsed = MathpixMarkdownModel.parseMarkdownByHTML(html, false);

html:




    <span class="math-inline">
        <asciimath style="display: none;">x^(x)</asciimath>
        <mjx-comtainer class="MathJax" jax="SVG"><svg>...</svg></mjx-comtainer>
    </span>




parsed:

[
    {
       "type": "asciimath",
       "value": "x^(x)"
     },
    {
       "type": "svg",
       "value": "<sgv>...</svg>"
     }
]

For tabular:

const options = {
      outMath: {
        include_table_html: false,
        include_tsv: true,
      }
    };
const latex = `\\begin{tabular}{ l c r }
  1 & 2 & 3 \\\\
  4 & 5 & 6 \\\\
  7 & 8 & 9 \\\\
\\end{tabular}`;
const html = MathpixMarkdownModel.markdownToHTML(latex, options);
const parsed = MathpixMarkdownModel.parseMarkdownByHTML(html, false);



  <tsv style="display: none">1    2       3
4       5       6
7       8       9</tsv>




parsed:

[
  { 
    type: 'tsv', 
    value: '1\t2\t3\n4\t5\t6\n7\t8\t9' 
  }
]

Example of the include_sub_math option usage for tables containing nested tables and formulas

parseMarkdownByHTML(html: string, include_sub_math: boolean = true)

By default, the include_sub_math option is enabled, and as a result will contain formats for the nested table and math.
const options = {
    outMath: {
        include_asciimath: true,
        include_mathml: true,
        include_latex: true,
        include_svg: true,
        include_tsv: true,
        include_table_html: true
    }
  };
const latex = `\\begin{tabular}{ l c r }
                 1 & {$x^1$} & 3 \\\\
                 4 & {$y^1$} & 6 \\\\
                 7 & {$z^1$} & 9 \\\\
               \\end{tabular}`;
const html = MathpixMarkdownModel.markdownToHTML(latex, options);
const parsed = MathpixMarkdownModel.parseMarkdownByHTML(html);

parsed: ```js [ { type: 'html', value: '

..
' }, { type: 'tsv', value: '1\tx^(1)\t3\n4\ty^(1)\t6\n7\tz^(1)\t9' },

{ type: 'mathml', value: '\n \n' }, { type: 'asciimath', value: 'x^(1)' }, { type:

Extension points exported contracts — how you extend this code

Window (Interface)
(no doc)
lib/copy-to-clipboard-code.d.ts
Window (Interface)
(no doc)
lib/bundle.d.ts
Window (Interface)
(no doc)
lib/context-menu.d.ts
MathpixSpeechConfig (Interface)
(no doc)
lib/browser/add-speech.d.ts
IYamlParserResult (Interface)
(no doc)
lib/yaml-parser/index.d.ts
ITocItem (Interface)
(no doc)
lib/markdown/mdPluginTOC.d.ts
PMathpixLoader (Interface)
(no doc)
lib/components/mathpix-loader/index.d.ts
LoadSreOptions (Interface)
(no doc)
lib/sre/sre-browser.d.ts

Core symbols most depended-on inside this repo

push
called by 1583
es5/browser/add-speech.js
n
called by 583
es5/browser/add-speech.js
match
called by 539
es5/browser/add-speech.js
indexOf
called by 419
es5/browser/add-speech.js
getInstance
called by 355
es5/browser/add-speech.js
equal
called by 270
es5/browser/add-speech.js
setAttribute
called by 179
es5/browser/add-speech.js
toString
called by 173
es5/browser/add-speech.js

Shape

Function 1,830
Method 1,067
Class 148
Interface 97
Enum 19

Languages

TypeScript100%

Modules by API surface

es5/browser/add-speech.js772 symbols
src/markdown/md-chemistry/smiles-drawer/src/Drawer.ts71 symbols
src/markdown/md-chemistry/smiles-drawer/src/Parser.ts52 symbols
lib/markdown/md-chemistry/smiles-drawer/src/Parser.js50 symbols
src/markdown/md-chemistry/smiles-drawer/src/Vector2.ts38 symbols
src/markdown/mdPluginText.ts34 symbols
src/markdown/md-chemistry/smiles-drawer/src/SvgDrawer.ts34 symbols
lib/mathjax/serialized-ascii/handlers.js34 symbols
lib/markdown/mdPluginText.js34 symbols
src/mathjax/serialized-ascii/handlers.ts33 symbols
es5/copy-to-clipboard-code.js33 symbols
es5/context-menu.js33 symbols

For agents

$ claude mcp add mathpix-markdown-it \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page