Matplotlib - LaTeX for Mathematical Expressions



What is Rendering mathematical expressions?

Rendering mathematical expressions in LaTeX involves using LaTeX syntax to write mathematical equations, symbols and formulas. LaTeX provides a comprehensive set of commands and notation to create complex mathematical expressions with precision and clarity.

Importance of LaTeX for Mathematics

Precision and Clarity − LaTeX allows precise typesetting of mathematical notation and symbols.

Consistency − Maintains consistency in formatting across mathematical documents.

Publication-Quality − Produces high-quality mathematical expressions suitable for academic and scientific publications.

LaTeX's support for mathematical typesetting makes it a preferred choice for researchers, mathematicians, scientists and academics when writing technical or mathematical documents that require accurate and well-formatted mathematical notation.

LaTeX for Mathematical Expressions

The below are the components of LaTex in Mathematical Expressions.

Inline Math Mode

Inline math mode in LaTeX is used to include mathematical expressions within the text of a document.

We can use inline math mode by enclosing the mathematical expression between a pair of single dollar signs $...$.

Using the inline math mode

In this example the mathematical expression `\frac{-b \pm \sqrt{b^2 - 4ac}}{2a}` is included within the text using inline math mode. The result is that the mathematical expression is rendered within the line of text.

Example

The quadratic formula is given by $x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}$.
Output

On executing the above code you will get the following output −

Inline Math Mode

Display Math Mode

Display math mode in LaTeX is used to showcase mathematical expressions in a separate block, centered and distinct from the surrounding text. It's commonly used for larger or standalone equations that deserve prominence in a document.

To use display math mode in LaTeX we have several options let’s see them one by one.

Double Dollar Sign `$$...$$`

Enclose the mathematical expression between $$ symbols for displayed equations.

Example

In this example we are displaying the given input equation by using the $$..$$.

$$
   f(x) = \int_{a}^{b} g(x) \, dx
$$
Output

On executing the above code you will get the following output −

Double Dollar Sign

‘equation’ Environment

Use the `equation` environment to create a numbered equation.

Example

\begin{equation}
   f(x) = \int_{a}^{b} g(x) \, dx
   \end{equation}
Output

On executing the above code you will get the following output −

Double Dollar Sign

Symbols and Operators

In LaTeX we can use a wide range of symbols and operators to represent mathematical notation, expressions and operations. Here are some commonly used symbols and operators along with their LaTeX commands.

  • Greek Letters − Alpha: `\alpha`, Beta: `\beta`, Gamma: `\gamma`, Delta: `\delta` and so on.

  • Arithmetic Operators − Plus: `+`, Minus: `-`, Multiplication: `\times` or `*`, Division: `\div` or `/`

  • Relations and Comparisons − Equals: `=`, Not equals: `\neq`, Less than: `<`, Greater than: `>` and so on.

  • Set Theory − Union: `\cup`, Intersection: `\cap`, Subset: `\subset`, Superset: `\supset` and so on

  • Calculus and Limits − Integral: `\int`, Summation: `\sum`, Limit: `\lim`, Derivative: `\frac{dy}{dx}`

  • Functions − Sine: `\sin`, Cosine: `\cos`, Tangent: `\tan`, Logarithm: `\log`, Exponential: `\exp`

  • Roots and Exponents − Square root: `\sqrt{x}`, Exponent: `x^2`, Subscript: `x_1`, Superscript: `x^i`

  • Other Notations

    Fractions − `\frac{numerator}{denominator}`

    Matrices − `bmatrix`, `pmatrix`, `vmatrix`, etc., using the `amsmath` package

    Special Symbols − For example, `\infty` for infinity, `\emptyset` for an empty set, etc.

Example

In this example we are using the $$..$$, to display the symbols and operators in the LaTex of matplotlib library.

$$(\alpha + \beta = \gamma \times \delta)$$

Output

On executing the above code you will get the following output −

Symbols Operators

By utilizing these LaTeX commands for symbols and operators we can create complex mathematical expressions with precision and clarity in our LaTeX documents.

Fraction and Subscript/Superscript

In LaTeX we can easily create fractions, subscripts and superscripts to represent mathematical expressions using specific commands and notation.

Fractions

To create fractions we can use the `\frac{numerator}{denominator}` command.

Example

In this example we are creating the fraction ¾.

The fraction is $\frac{3}{4}$.
Output

On executing the above code you will get the following output −

Fraction

Subscripts and Superscripts

Subscripts and superscripts can be added using the ‘_’ for subscripts and ‘^’ for superscripts.

Example

In this example we are displaying a script content.

$x_i^2$ denotes $x$ raised to the power of $i$ squared.
Output

On executing the above code you will get the following output −

Subscripts

Nested Subscripts and Superscripts

We can also nest subscripts and superscripts by enclosing the content in curly braces {}.

Example

In this example we are displaying the nested subscripts.

$x_{i_j}^{2k}$ represents a nested subscript and superscript.
Output

On executing the above code you will get the following output −

Nested Subscripts

Using Commands

For more complex expressions or to ensure consistent formatting we can use commands such as \subscript{} and \superscript{} provided by packages like fixltx2e.

Example

In this example we are displaying the complex expressions.

$x \subscript{i} \superscript{2}$
Output

On executing the above code you will get the following output −

Complex Expressions

LaTeX offers straightforward ways to create fractions, subscripts and superscripts, allowing us to represent mathematical expressions accurately and efficiently.

Matrices and Arrays

In LaTeX matrices and arrays are used to represent data in matrix form or to display sets of equations. The array environment is the basic structure for creating matrices and arrays in LaTeX while the matrix environments provided by the amsmath package offer additional functionality and easier syntax for matrices.

Creating Matrices and Arrays

Here are we are creating the arrays and matrices using respective environments.

Using ‘array’ Environment

The ‘array’ environment allows us to create matrices or arrays in LaTeX.

Example
\[
\begin{array}{ccc}
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9 \\
\end{array}
\]
Output

On executing the above code you will get the following output −

amsmath

Using ‘amsmath’ Package's ‘matrix’ Environments

The amsmath package provides convenient matrix environments such as matrix, pmatrix, bmatrix, Bmatrix, vmatrix, Vmatrix which simplify the creation of matrices.

Example

\[
\begin{matrix}
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9 \\
\end{matrix}
\]
Output

On executing the above code you will get the following output −

Matrix

Matrix Formatting

Here are going to align the columns of the matrix using the LaTex. In matrices or arrays we can specify column alignment using c for centered, l for left-aligned and r for right-aligned columns within the array environment.

The below is the example of applying the column alignment on a matrix.

Example

\[
\begin{array}{ccc}
1 & 222 & 3 \\
4 & 55555 & 6 \\
7 & 888 & 999999 \\
\end{array}
\]
Output

On executing the above code you will get the following output −

Matrix Formatting

Additional Notes

  • Matrices and arrays in LaTeX are enclosed within the \[ ... \] or equation environment to display them as standalone equations.
  • The & symbol separates elements within a row and \\ starts a new row.

LaTeX provides versatile tools for displaying matrices and arrays allowing us to represent mathematical data or equations in matrix form with various alignments and configurations. LaTeX enables the creation of matrices and arrays for mathematical notation.

Example

\begin{bmatrix}
1 & 2 \\
3 & 4
\end{bmatrix}
Output

On executing the above code you will get the following output −

Matrix Alignment

Special Functions

LaTeX supports notation for special functions like trigonometric functions, logarithms, etc.

Example

$$
\sin(\theta), \log(x), \lim_{x \to \infty} f(x)
$$   Example of LaTeX Mathematical Expressions:
Output

On executing the above code you will get the following output −

Special Functions

Remove random unwanted space in LaTeX-style maths

LaTeX ignores the spaces you type and uses spacing the way it's done in mathematics texts. You can use the following four commands in case you want a different spacing style

  • \; − thick space
  • \: − medium space
  • \, − a thin space
  • \! − a negative thin space

To remove random unwanted space in LaTeX-style maths in matplotlib plot we can use "\!" which will reduce the extra spacing.

The below is the example of applying the column alignment on a matrix.

Example

from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
plt.subplot(211)
plt.text(0.4, 0.4, r'$\sum_{n=1}^{\infty}\; \frac{-e^{i\pi}}{2^n}!\left[a^2+\delta ^2- \frac{\pi}{2} \right ]$', fontsize=16, color='r')
plt.title("With thick space")
plt.subplot(212)
plt.text(0.4, 0.4, r'$\sum_{n=1}^{\infty}\! \frac{-e^{i\pi}}{2^n}!\left[a^2+\delta ^2- \frac{\pi}{2} \right ]$', fontsize=16, color='r')
plt.title("With thin space")
plt.show()
Output

On executing the above code you will get the following output −

space_latex

Notice the difference in spacing after the "Σ (sigma)" symbol. In the first case, we have used thick space (\;) and in the second case, we have used the thin space (\!) to reduce extra spacing.

Advertisements