88 Equations and Formulas: How to Use LaTeX

In many academic fields, you may find yourself needing to include a mathematical equation or other formula that requires special characters and formatting. Pressbooks currently supports LaTeX codes, which enables you to simply format these formulas and ensure that they will display correctly in all outputs. This chapter will cover:

What is LaTeX?

LaTeX is a programming language used for technical and scientific documents, but in small snippets, it can also be used to easily create formulas and equations in Pressbooks. Each character has its own code, and there are functions for common formatting as well.

If you’re familiar with regular expressions, it’s similar in that it looks far more complicated than it is, and once you get the basics, you’ll never look back. For example, here are a few character codes:

\alpha alpha
\beta beta
\cos cos
\leq leq
\simeq simeq

And some of the formatting options:

\sqrt{abc} sqrt{abc}
\frac{abc}{xyz} frac{abc}{xyz}
\overleftarrow{abc} overleftarrow{abc}

By combining these character and formatting codes, you can create nearly any formula you can think of. Think of the process as having three elements:

  • Your formula or equation
  • LaTeX, a language into which you translate your formula
  • The shortcode, which Pressbooks uses to translate LaTeX into a properly formatted digital version of your formula

How to Use LaTeX

You have two options for inputting LaTeX. You can either use PB LaTeX, or you can enable the QuickLaTeX plugin in order to type native LaTeX syntax directly into your chapters.

PB LaTeX

Inserting LaTeX into Pressbooks uses shortcodes to keep things simple. Shortcodes are like shortcuts that let you do some formatting with very little effort. To add your formula to your book, you simply need to wrap it in the LaTeX shortcode shown below, and insert it directly into your chapter, in the visual editor:
latex1

This will automatically convert your formula to a small image to preserve its formatting across exports.

If you need to create your formula from scratch, you can use a cheatsheet (we like this one from Brown University) and put together your code. As an example, let’s look at Pythagoras’ theorem: a^2 + b^2 = c^2

By using the cheatsheet, we can see that most of these characters can be typed in directly, and to get the superscript for the ‘squared’ function, we just use ^ (on your keyboard). So our formula, when translated into LaTeX, will look like this:

a^2 + b^2 = c^2

Then, when we wrap it in the LaTeX shortcode, it displays in outputs as:

a^2 + b^2 = c^2

For a more advanced example, let’s say we want to use the truth function [overline{p, xi}, N(xi)].

Using the cheatsheet again, we can find the code for the special character, and the overline:

  • ξ (xi) is xi
  • To overline: overline{abc}

Combined, the LaTeX version of the formula looks like this:

[overline{p, xi}, N(xi)]

Finally, wrap it in the LaTeX shortcode and add it to your chapter in the visual editor:latex2

Which will display as:

[\overline{p, \xi}, N(\xi)]

Other examples:

x^n + y^n = z^n

becomes x^n + y^n = z^n
x=frac{1+y}{1+2z^2} becomes  x=\frac{1+y}{1+2z^2}
int_0^infty e^{-x^2} dx=frac{sqrt{pi}}{2} becomes  \int_0^\infty e^{-x^2} dx=\frac{\sqrt{\pi}}{2}

NOTE: This function is currently more effective for standalone formulas than in-text. In-text formulas will still display correctly, but may be slightly larger or smaller than your body text and won’t be affected by other styling you have applied to your page.

Customizing  PB LaTeX (Size & Color)

There are a couple of ways you can customize how your LaTeX formulas display. Some are controlled within your chapter editor, and others are set globally.

For the global settings, go to “Settings” –> “PB LaTeX” and you will see the screen shown below. Here, you can:

  • Check that LaTeX is working properly.
  • See how to use ‘inline’ syntax instead of shortcodes, if that is your preference.
  • Set the default text color.
  • Set the default background color.
PB LaTeX Screen

PB LaTeX Screen

You can also control the LaTeX display using the shortcode itself. The first option is colour, where you can specify a custom text and/or background colour for when you don’t want to apply these globally. To do so, add the following colour and/or background setting to the opening shortcode:

latex4

The second option is size, which can range from tiny:

a^2 + b^2 = c^2

To very large:

a^2 + b^2 = c^2

To control the sizing, add the ‘size’ setting to the opening shortcode as follows:

latex5

The size options range from -4 (the tiny version above) to 4 (the larger version above). These numbers correspond with the following LaTeX commands:

    -4     tiny
    -3     scriptsize
    -2     footnotesize
    -1     small
    0      normalsize (12pt)
    1      large
    2      Large
    3      LARGE
    4      huge

QuickLaTeX

QuickLaTeX is a plugin that allows users to write native LaTeX syntax directly into their books.

This includes:

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

It also allows TikZ graphics support. Users can write TikZ commands directly in the post using begin{tikzpicture} … end{tikzpicture}. QuickLaTeX will render them into image such that text on the picture and on the page will be of the same size.

 NOTE: Enabling the QuickLaTeX plugin does not damage existing PB LaTeX. There is no need to change existing chapters that already use PB LaTeX shortcodes.

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 must be enabled for each book; it can’t be turned on for the entire network.

Activating WP QuickLaTeX plugin

Once you’ve enabled the QuickLaTeX plugin, you can activate QuickLaTeX on a chapter or part by inputting [latexpage] at the top of your visual or text editor. All typed LaTeX syntax after this shortcode will be converted into readable equations.

Using the same cheatsheet from above, you can use native LaTeX syntax to create your desired equations:

Example of QuickLaTex in a text editor

The above formulas above get converted into:

Formulas converted from text to QuickLaTeX

Customizing QuickLaTeX

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

QuickLaTeX Settings Page

From this page, you can click on the Basic Settings tab.

QuickLaTeX basic settings

You can adjust font size, font color, and background color for your equations here. You can also choose whether you want your equations aligned left, right or center and whether you want the equation numbers aligned to the left or right.

If you’d like to enable QuickLaTex for the entire book, you can click on the Advanced tab and toggle Use LaTeX Syntax Sitewide to ON. Then, click Update QuickLaTeX Settings. (NOTE: This ONLY applies to the book that you are on. Turning on this setting does not activate QuickLaTeX on the other books in your network.)

Advanced QuickLaTeX settings

Once you’ve turned on this setting, you no longer have to put [latexpage] at the top of each chapter or part that you want to include QuickLaTeX in. LaTeX syntax will automatically convert to proper images in all chapters and parts.

You can also, from this tab, choose to exclude the option for $ … $ syntax.

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

Using LaTeX in TablePress Tables

You can use PB LaTeX 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 PB LaTeX in TablePress

In order to create PB LaTeX equations in TablePress, just wrap the following shortcode around every equation or formula in your table while in the TablePress interface:

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

For instance:

wrap your formula with the <img src='https://s0.wp.com/latex.php?latex&bg=T&fg=000000&s=0&zoom=1' alt='' title='' class='latex' /> shortcode

Becomes

a formula with LaTeX applied in a TablePress output

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

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.

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. These are well supported across web and ebook outputs, but in PDF can be a little patchy. We are working on better support for this, but if you run into any trouble, email us at support@pressbooks.com and we will look into it.

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

Watch This Space

We are currently working on expanding our support for mathematics and other special formatting for academic purposes. If you have any requests, recommendations or expertise to offer in this area, we welcome your input. Please email us at support@pressbooks.com.


Prefer to watch and learn? Here’s a brief visual intro to using equations in Pressbooks.

License

Icon for the Creative Commons Attribution 4.0 International License

Equations and Formulas: How to Use LaTeX by Book Oven Inc. (PressBooks.com) is licensed under a Creative Commons Attribution 4.0 International License, except where otherwise noted.