# 16 Equations and Formulas

- 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 is 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

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 LaTeX (Size & Colour)

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`

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