# 16 Equations and Formulas: How to Use LaTeX

For the latest updates to this resource, check out in our

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?
• How to create a LaTeX code
• Other uses for LaTeX
• Other solutions for equations and formulas

## 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 \beta \cos \leq \simeq

And some of the formatting options:

 \sqrt{abc} \frac{abc}{xyz} \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:

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:

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:

For a more advanced example, let’s say we want to use the truth function .

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:

Which will display as:

Other examples:

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

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.

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:

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

To very large:

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

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

. 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

*** QuickLaTeX cannot compile formula:

*** Error message:
Error: Nothing to show, formula is empty


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.

Once you’ve enabled the QuickLaTeX plugin, you can activate QuickLaTeX on a chapter or part by inputting 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:

The above formulas above get converted into:

### Customizing QuickLaTeX

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

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.)

Once you’ve turned on this setting, you no longer have to put 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.

## 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.