How to use LaTeX in Markdown

2 minute read comments

Many Markdown processors are capable to interpret LaTeX’s math mode commands in order to display a broad variaty of mathematical expressions. For some processors, this support is already enabled by default or you have to enable the so-called MathJax support. MathJax is a display engine based on JavaScript, that renders mathematical LaTeX expressions into a PNG, that is then displayed in the preview mode of your Markdown editor or web browser.

While some Markdown processors are equipped with such support, others are not. Also web browser based Markdown solutions like Jekyll websites lack such built-in support. Nevertheless, also for these cases it’s actually very easy to get MathJax running. The only thing you need to do is to add the following HTML command to your Markdown document:

<script
  src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"
  type="text/javascript">
</script>

Jekyll websites: Just add the HTML code to your layout definition files, e.g., single.html or post.html, and MathJax support gets enabled on all your pages that use these layouts.

Jupyter notebooks come with enabled MathJAX support by default .

How to use LaTeX in your Markdown document

The solution above enables the following syntax to use LaTeX within your Markdown document:

$$ your equation $$

Place this expression in the middle of your text for inline equations. Place the expression into its own line and insert one empty line to the previous paragraph for display style equations:

This is an inline equation: $$V_{sphere} = \frac{4}{3}\pi r^3$$,<br>
followed by a display style equation:

$$V_{sphere} = \frac{4}{3}\pi r^3$$

     This is an inline equation: \(V_{sphere} = \frac{4}{3}\pi r^3\),
     followed by a display style equation:

\[V_{sphere} = \frac{4}{3}\pi r^3\]

Info: You can also use \\( your equation \\) and \\[ your equation \\] for inline and display style equations, respectively. The HTML code above enables the syntax with the $$ signs with automatic distinction between inline and display style equations.

Info: You can use all LaTeX math mode expressions and commands, that are covered by the default LaTeX and the amsmath packages. The LaTeX guide on this website contains some commonly used math mode expressions.

To assign numbers to your equations, use the \tag{number} command:

$$
\begin{align}
  \tag{1.1}
  V_{sphere} = \frac{4}{3}\pi r^3
\end{align}
$$
\[\begin{align} \tag{1.1} V_{sphere} = \frac{4}{3}\pi r^3 \end{align}\]

Extended MathJax support

Just recently I have discovered an extended version of the HTML command above by Sebastian Flennerhag :

<script type="text/javascript"
  src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS_CHTML">
</script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    tex2jax: {
      inlineMath: [['$','$'], ['\\(','\\)']],
      processEscapes: true},
      jax: ["input/TeX","input/MathML","input/AsciiMath","output/CommonHTML"],
      extensions: ["tex2jax.js","mml2jax.js","asciimath2jax.js","MathMenu.js","MathZoom.js","AssistiveMML.js", "[Contrib]/a11y/accessibility-menu.js"],
      TeX: {
      extensions: ["AMSmath.js","AMSsymbols.js","noErrors.js","noUndefined.js"],
      equationNumbers: {
      autoNumber: "AMS"
      }
    }
  });
</script>

This code enhances the LaTeX usage within Markdown documents by the following functions:

  • Inline equations can be defined by a single $ sign, which corresponds to the original LaTeX syntax, e.g, $V_{sphere} = \frac{4}{3}\pi r^3$.
  • Equation numbering is performed automatically in the equation, eqnarray and align environment. You can even reference equations with the LaTeX command \eqref{label}:
\begin{align}
  V_{sphere} = \frac{4}{3}\pi r^3 \label{eq:test1}
\end{align}

In Eq. $\eqref{eq:test1}$ you can see...while in Eq. $\eqref{eq:test2}$...

\begin{align}
  V_{cube} = l w h \label{eq:test2}
\end{align}
\[\begin{align} V_{sphere} = \frac{4}{3}\pi r^3 \label{eq:test1} \end{align}\]

     In Eq. $\eqref{eq:test1}$ you can see…while in Eq. $\eqref{eq:test2}$…

\[\begin{align} V_{cube} = l w h \label{eq:test2} \end{align}\]

The MathJax documentation contains more information about the settings and how to further extend the MathJax support within your Markdown documents.


Comments

Comment on this post by publicly replying to this Mastodon post using a Mastodon or other ActivityPub/Fediverse account.

Comments on this website are based on a Mastodon-powered comment system. Learn more about it here.

There are no known comments, yet. Be the first to write a reply.