Sarmate.net Sarmate.net
Home Features Pricing Documentation Contact
Log in Sign up
Scientific web 9 min

Mathematical formulas (KaTeX)

mathpad renders mathematical formulas via KaTeX, an ultra-fast JavaScript engine that displays LaTeX syntax directly in the browser. Here we cover how to write inline formulas, display formulas, aligned equations and systems — with the same syntax as LaTeX.

Inline and display: $..$ and $$..$$

As in LaTeX:

HTML
<p>The discriminant $\Delta = b^2 - 4ac$ determines the number of solutions.</p>
<p>The solutions are then:</p>
$$x = \dfrac{-b \pm \sqrt{\Delta}}{2a}$$
Output

The discriminant $\Delta = b^2 - 4ac$ determines the number of solutions.

The solutions are then:

$$x = \dfrac{-b \pm \sqrt{\Delta}}{2a}$$
Inline fractions: \dfrac vs \frac In inline ($..$), \frac defaults to small style (compact numerator and denominator). For full display (like in display mode), use \dfrac: $\dfrac{1}{2}$ instead of $\frac{1}{2}$.

Common macros

Here are the most useful LaTeX commands in a typical math course — all supported by KaTeX:

CommandOutput
\dfrac{a}{b}$\dfrac{a}{b}$
\sqrt{x}, \sqrt[3]{x}$\sqrt{x},\;\sqrt[3]{x}$
x^{2}, x_{n+1}$x^{2},\;x_{n+1}$
\int_{a}^{b} f(x)\,dx$\int_{a}^{b} f(x)\,dx$
\sum_{k=0}^{n} a_k$\sum_{k=0}^{n} a_k$
\lim_{n \to +\infty} u_n$\lim_{n \to +\infty} u_n$
\vec{u},\;\overrightarrow{AB}$\vec{u},\;\overrightarrow{AB}$
\mathbb{R},\;\mathbb{N},\;\mathbb{C}$\mathbb{R},\;\mathbb{N},\;\mathbb{C}$
\Leftrightarrow,\;\Rightarrow,\;\implies$\Leftrightarrow,\;\Rightarrow,\;\implies$
\leq,\;\geq,\;\neq$\leq,\;\geq,\;\neq$
\alpha,\;\beta,\;\Omega,\;\Delta$\alpha,\;\beta,\;\Omega,\;\Delta$
\binom{n}{k}$\binom{n}{k}$
Editor: «More symbols» In the online editor, Math → More symbols opens a catalog of 120 symbols organized in 9 sections (arrows, operators, Greek letters, sets, geometry…). One click copies the command to the clipboard.

<mp-align> — aligned equations

Equivalent to the amsmath align* environment. Anchors are aligned with &, each line ends with \\.

HTML
<mp-align>
(x+2)^2 & = & x^2 + 4x + 4 \\
        & = & x^2 + 4(x+1) \\
        & \geq & 0
</mp-align>
Output $$\begin{array}{rcl}(x+2)^2 & = & x^2 + 4x + 4 \\ & = & x^2 + 4(x+1) \\ & \geq & 0\end{array}$$

Variant: equivalences

To align equivalences ($\Leftrightarrow$) in the middle:

HTML
<mp-align>
2x + 6 = 0 &\Leftrightarrow& 2x = -6 \\
           &\Leftrightarrow& x = -3
</mp-align>
Output $$\begin{array}{rcl}2x + 6 = 0 &\Leftrightarrow& 2x = -6 \\ &\Leftrightarrow& x = -3\end{array}$$

Equation systems

For a system with an opening curly brace on the left, use \left\{\begin{array}{l}...\end{array}\right. directly in a display block:

HTML
$$\left\{\begin{array}{rcl}
  2x + 3y & = & 7 \\
  x - y & = & 1
\end{array}\right.$$
Output $$\left\{\begin{array}{rcl}2x + 3y & = & 7 \\ x - y & = & 1\end{array}\right.$$

Piecewise function

Use \begin{cases}...\end{cases} for a case definition:

HTML
$$f(x) = \begin{cases}
  -x & \text{si } x \lt 0 \\
  x^2 & \text{si } x \geq 0
\end{cases}$$
Output $$f(x) = \begin{cases}-x & \text{si } x \lt 0 \\ x^2 & \text{si } x \geq 0\end{cases}$$

Matrices

Several environments are available depending on the desired brackets:

HTML
$$A = \begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix},\quad
\det(A) = \begin{vmatrix} 1 & 2 \\ 3 & 4 \end{vmatrix} = -2$$
Output $$A = \begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix},\quad \det(A) = \begin{vmatrix} 1 & 2 \\ 3 & 4 \end{vmatrix} = -2$$

Text within a formula

In math mode, letters are rendered in italic (variables). To insert normal text, use \text{...}:

HTML
$$\text{for all } x \in \mathbb{R},\quad x^2 \geq 0$$
Output $$\text{for all } x \in \mathbb{R},\quad x^2 \geq 0$$

Reference a numbered block

To reference a numbered theorem, property or chapter, give it a standard HTML id, then use <mp-ref target="...">. The reference will display the target element's number and be clickable.

AttributeValuesDescription
targetid of the target elementRequired — points to the HTML anchor
formatshort | fullshort = number only («3»), full = labeled («Theorem 3»). Default short.
HTML
<mp-theorem id="thm-pythagore">
  <mp-title>Pythagorean theorem</mp-title>
  $$a^2 + b^2 = c^2$$
</mp-theorem>

… <mp-ref target="thm-pythagore" format="full"></mp-ref> then gives us …

Complete example

HTML
<p>We solve the equation $2x^2 - 3x - 2 = 0$.</p>

<p>Computing the discriminant:</p>

<mp-align>
\Delta & = & (-3)^2 - 4 \times 2 \times (-2) \\
       & = & 9 + 16 \\
       & = & 25
</mp-align>

<p>Since $\Delta > 0$, the equation has two real solutions:</p>

$$\left\{\begin{array}{rcl}
  x_1 & = & \dfrac{3 - 5}{4} = -\dfrac{1}{2} \\[6pt]
  x_2 & = & \dfrac{3 + 5}{4} = 2
\end{array}\right.$$
Output

We solve the equation $2x^2 - 3x - 2 = 0$.

Computing the discriminant:

$$\begin{array}{rcl}\Delta & = & (-3)^2 - 4 \times 2 \times (-2) \\ & = & 9 + 16 \\ & = & 25\end{array}$$

Since $\Delta > 0$, the equation has two real solutions:

$$\left\{\begin{array}{rcl} x_1 & = & \dfrac{3 - 5}{4} = -\dfrac{1}{2} \\[6pt] x_2 & = & \dfrac{3 + 5}{4} = 2\end{array}\right.$$

Ready to try mathpad?

Create your first interactive scientific HTML document in the online editor, or import your existing LaTeX course in one click.