87 Math in Pressbooks

Pressbooks offers two different methods to create mathematical equations in your books: MathJax and QuickLaTeX. Each method has an array of options and benefits. In this chapter, we’ll cover:

MathJax

MathJax is an open source JavaScript display engine that translates mathematical markup into accessible, high resolution equations that can be rendered in any supported book format on Pressbooks. Pressbooks supports input provided using three different mathematical syntaxes for MathJax: LaTeX, AsciiMath, and MathML.

MathJax is activated and enabled as the default mathematics rendering solution for all hosted PressbooksEDU networks (QuickLaTeX is the default math rendering option for authors on Pressbooks.com). You can find and change your MathJax settings by going to Settings > MathJax from the left sidebar menu.

NOTE: MathJax has replaced PB LaTeX on all hosted PressbooksEDU networks. Equations written in PB LaTeX are still compatible with MathJax and should continue to display. Open source Pressbooks network administrators may need to install a microservice to enable LaTeX rendering in exported files.
When MathJax is properly configured, you’ll see an integral display at the top of the MathJax page. This integral will also reflect any setting changes you’ve made. If you do not see this integral, contact your network administrator and ask them to investigate the configuration settings.

MathJax admin page

Settings

You can change the text color of all equations in your book by insert the hex code for the color you’ve chosen into the form next to Text Color and saving your changes. The integral at the top of the page will refresh to reflect your changed settings.

Accessibility and Other Features

Within the webbook, readers can right click any equation rendered in MathJax to access additional display options and accessibility features.

MathJax settings menu from webbook

Options include the ability to magnify any equation on click or hover, the ability to display the math in HTML, SVG, and other formats, and more. For more information on the accessibility features and other settings  available from MathJax, check out MathJax’s documentation.

LaTeX

There are two ways to use LaTeX syntax in MathJax:

Shortcode syntax: 
[la​tex]e^{i \pi} + 1 = 0[/lat​ex]

Dollar sign syntax: 
$latex e^{i \pi} + 1 = 0$

Draft your LaTeX equations directly into the visual editor. Note that equations will not display fully formatted in the editor. However, you can view the webbook or export your files at any time to see how the equations appear.

NOTE: LaTeX expressions can be rendered with either MathJax or the QuickLaTeX plugin. Choose your method of rendering LaTeX based on which features you need for your book. Note that while MathJax and QuickLaTeX support similar syntaxes for creating mathematical expressions, there are some slight differences (detailed below).

For more details on how to use LaTeX, see LaTeX Project documentation.

AsciiMath

There are two ways to use AsciiMath syntax in MathJax:

Shortcode syntax: 

[asciimath]e^{i \pi} + 1 = 0[/asciimath]

Dollar sign syntax: 

$asciimath e^{i \pi} + 1 = 0$

Like LaTeX, AsciiMath can be drafted directly in the visual editor. View the webbook or export your files to see how the equations display.

For more information on how to use AsciiMath, see AsciiMath’s website.

MathML

MathML is formatted in markup syntax. Wrap equations in <math> tags to render in MathJax.

Example: 

<math><mrow><mrow><msup> <mi>x</mi> <mn>2</mn> </msup> <mo>+</mo><mrow><mn>4</mn><mo>⁢</mo><mi>x</mi></mrow><mo>+</mo><mn>4</mn></mrow><mo>=</mo><mn>0</mn></mrow></math>
MathML, like any HTML, will only display correctly if it’s been entered in the text editor. To switch to the text editor from the default visual editor, click the Text tab on any chapter editor above the toolbar.
For more information on how to draft equations in MathML, see W3C’s Mathematical Markup Language documentation.

Word Imports

If you are drafting in Microsoft Word before importing to Pressbooks, you may be interested in knowing how to format equations so that they can import correctly into Pressbooks. The following shortcodes can be used to convert your equations into MathJax-rendered expressions from imported Word docx files:

LaTeX

[la​tex]latex code here[/lat​ex]

OR

[equation]latex code here[/equation]

AsciiMath

[asciimath]a^2[/asciimath]

Wrap your equations in these shortcodes while drafting in Word. Please note that MathML cannot be imported from a Word docx file. For more information on how to use shortcodes, see the Shortcodes chapter.

QuickLaTeX

QuickLaTeX is a plugin that allows users to write native LaTeX syntax directly into their books without wrapping shortcodes around every part of every equation. QuickLaTeX is the default mathematics rendering option for Pressbooks.com users and is can be used as an optional replacement for MathJax within individual books on other networks.

Activating QuickLaTeX

To enable the QuickLaTeX plugin:

  1. Go to your book’s dashboard
  2. Click the Plugins tab on the left-hand menu of your screen
  3. Scroll to WP QuickLaTeX
  4. Click Activate
NOTE: QuickLaTeX should be enabled on a book by book basis; if network activated, it will replace the native MathJax rendering service for all books.

Once you’ve enabled the QuickLaTeX plugin, you can format equations with any of the following syntaxes:

  • $ … $
  • [ … ]
  • ( … )
  • $ $ … $ $
  • Environments: equation, align, displaymath, eqnarray, multline, flalign, gather, and alignat

The QuickLaTeX plugin also supports TikZ graphics and other commands that may not be supported using regular LaTeX. For more information about commands and notation in LaTeX, see Brown University’s LaTeX guide.

There are three ways to render equations with the QuickLaTeX plugin:

1. Add the [latexpage] shortcode to the top of your chapter. Once you’ve written [latexpage] at the top of a chapter, LaTeX is active for that entire chapter, and everything written in LaTeX syntax will be translate by QuickLaTex. For example if you were to input the following:

[latexpage]
At first, we sample $f(x)$ in the $N$ ($N$ is odd) equidistant points around $x^*$:
\[
f_k = f(x_k),\: x_k = x^*+kh,\: k=-\frac{N-1}{2},\dots,\frac{N-1}{2}
\]
where $h$ is some step.
Then we interpolate points $\{(x_k,f_k)\}$ by polynomial
\begin{equation} \label{eq:poly}
P_{N-1}(x)=\sum_{j=0}^{N-1}{a_jx^j}
\end{equation}
Its coefficients $\{a_j\}$ are found as a solution of system of linear equations:
\begin{equation} \label{eq:sys}
\left\{ P_{N-1}(x_k) = f_k\right\},\quad k=-\frac{N-1}{2},\dots,\frac{N-1}{2}
\end{equation}
Here are references to existing equations: (\ref{eq:poly}), (\ref{eq:sys}).
Here is reference to non-existing equation (\ref{eq:unknown}).

WP QuickLaTeX would render something which looks like this:

Example of QuickLaTeX

2. Enable the “Use LaTeX Syntax Sitewide” setting. Go to Settings > QuickLaTeX from the left sidebar, then select the Advanced tab. Turn on the “Use LaTeX Syntax Sitewide” setting so that all LaTeX expressions throughout your entire book are rendered by QuickLaTeX.

3. Use the [la​tex] shortcode for your equations. Anything wrapped in the [la​tex] shortcode will render in QuickLaTeX once the plugin has been activated. Because the allowed syntaxes for denoting mathematical expressions can differ between MathJax and QuickLaTeX,  this is our recommended method if you think you may want to switch from using MathJax to QuickLaTex or vice versa, since any equation wrapped in latex shortcodes will display in MathJax and QuickLaTeX.

Customizing QuickLaTeX

To customize your QuickLaTex, navigate to Settings > QuickLaTeX on the left-hand menu of your book’s dashboard.

You can customize QuickLaTeX settings available from the Basic Settings tab or the Advanced tab.

Basic Settings: 

  • Font size
  • Font color
  • Background color
  • Equation alignment
  • Equation number position

QuickLaTeX basic settings

Advanced Settings:

  • Use LaTeX syntax sitewide
  • Exclude $ from syntax interpretation
  • Set LaTeX preambles

Advanced QuickLaTeX settings

For more information on QuickLaTeX, visit the website for the plugin.

Other Uses for LaTeX

LaTeX can also be used for individual special characters. While Pressbooks supports as wide a range of special characters as possible, it may be that you find a character you need displaying as a question mark in your outputs. To fix this, find the character code you need and wrap it in the latex shortcode.

Math Within Other Book Elements

Math in most syntaxes should be compatible with most elements in your book, including image captions, textboxes, body text, and more. Here are a few exceptions:

  • Any shortcode within another shortcode. Shortcode elements will break when placed inside of another shortcode. This means that the [asciimath][/asciimath] and [latex][/latex] shortcodes cannot be nested inside of footnotes, image captions, or any other shortcode element. See Shortcodes for a full list of elements in Pressbooks that are or can be created with shortcodes. However, both AsciiMath and LaTeX offer a non-shortcode syntax, which we recommend using if you're placing math inside of a shortcode element.
  • Titles. Equations won't look good inside of a title because the style of the math will override the style of the heading. The code used to create the equation may also display in the table of contents.
  • Running content. Similar to titles, the code used to create the equation may display in the running content.

Using Math in H5P Activities

You can draft equations inside of H5P elements in Pressbooks with MathJax LaTeX. You'll first need to add the mathematics library to your book's H5P setup in Pressbooks. To do this:

  1. Make sure you have H5P activated and have the mathematics library from H5P downloaded to your device
  2. Go to H5P Content > Libraries from the left sidebar menu
  3. Click Choose File and add the H5P mathematics library
  4. Click Upload

To find the H5P mathematics library and read more about how to use LaTeX in H5P, see Mathematical expressions in H5Ps.

For more information about drafting equations in LaTeX, see the MathJax LaTeX section of this chapter.

Using Math in TablePress

You can use MathML or QuickLaTeX to create equations and formulas in the tables you make in TablePress.

NOTE: The TablePress plugin is only available to users on PressbooksEDU networks.

Using QuickLaTeX in TablePress

In order to create QuickLaTeX equations in TablePress tables, you must first check that:

  • You have QuickLaTex activated
  • You have enabled the "Use LaTeX Syntax Sitewide" setting under Settings > QuickLaTeX > Advanced* 

*This is recommended but not compulsory. The alternative is to add the [latexpage] shortcode to each individual cell of the table, or to follow the instructions for Using PB LaTeX.

Once you've activated or enabled these settings, you can use any supported QuickLaTeX syntax to create your equation within the table's cells. All content can be edited from within the TablePress interface.

For instance:

An equation wrapped in $ QuickLaTeX syntax

Becomes:

A rendered equation within the TablePress table in the webbook

Using MathML in TablePress

Write your MathML markup directly into the table cells in TablePress as you would in the text editor. Save the table, add it to your chapter, and you're ready to go.

Other Solutions

If LaTeX is a bit much for you or you'd rather look at other options, you can always use special characters, symbols, and the sub/superscript buttons on the toolbar to compose your equations.

If you are fluent in HTML+CSS, you can also look at constructing and formatting your formulas that way.

License

Icon for the Creative Commons Attribution 4.0 International License

Pressbooks User Guide Copyright © 2012 by Book Oven Inc. (Pressbooks.com) is licensed under a Creative Commons Attribution 4.0 International License, except where otherwise noted.

Share This Book