Adding Media, Interactivity, and Mathematical Equations

# 14 Using LaTeX in Pressbooks

is a markup language and typesetting system commonly used in mathematical and scientific writing.^{[1]} It can be used to create documents from scratch or display equations in a web browser with the help of plugins, as is the case in Pressbooks.

Below is an example of what LaTeX can look like. This example is simple, but LaTeX can be used to write much more complex expressions.

Use the formula [latex]V=\pi r^2h[/latex] to calculate the volume of a cylinder.

In this chapter, you will learn how to get started using LaTeX in Pressbooks. Whether you are new to LaTeX, familiar with it, or already proficient, the following resources might be helpful to you:

- The LaTeX Project is the website of the team that maintains and develops LaTeX.
- LaTeX Questions Answered in the BCcampus Open Education
*Pressbooks Guide*, the source for the chapter you are reading now, includes more detailed information and answers to frequently asked questions.

# How to Use LaTeX to Write Expressions in Pressbooks

Use the delimiters `[latex]…[/latex]`

to mark the boundaries between LaTeX and regular text. This is the simplest and best way in Pressbooks to write LaTeX expressions that will be rendered by MathJax. These shortcodes can be used to write in-line LaTeX expressions (i.e., in the middle of regular text) or to left-align a LaTeX expression written on its own line.

For example, we all know that [latex]a^2+b^2=c^2[/latex] is the Pythagorean theorem. You can also show this equation on its own line and centered, as in the following example:

[latex]a^2+b^2=c^2[/latex]

To show an equation in this way, simply write it on its own line and center it using `Shift + Alt + C`

or the “Align center” button on the toolbar.

Note that it may not be necessary to use LaTeX for an expression or equation. If the needed numbers, symbols, and letters are available on a standard keyboard and can be entered in a clear manner, then LaTeX is not required. Instead, you can reserve LaTeX for complex formulas and equations, including fractions.

# Mathematical Symbols in LaTeX

The following resources are guides to LaTeX symbols, some more comprehensive than others. First, however, be aware that only those LaTeX commands that are supported by MathJax will work in the CUNY of Pressbooks, as well as any other Pressbooks instances that rely on MathJax to render LaTeX. See the Supported TeX/LaTeX commands page in MathJax’s documentation to view a full list of LaTeX commands that will work in Pressbooks.

The page of LaTeX symbols on the Art of Problem Solving wiki is an excellent introduction to some of the most common LaTeX symbols. This page also links to a number of other helpful resources for LaTeX symbols that are also included here.

Detexify is a web-based application with two features: “classify” and “symbols.” The “classify” feature allows you to draw your desired symbol, which generates a list of suggested commands. The “symbols” feature is an alphabetical, searchable list of symbol commands.

Math Vault’s Comprehensive List of Mathematical Symbols [PDF] is a compilation of many symbols in higher mathematics. This guide provides the LaTeX for each symbol and a rendered LaTeX expression containing the symbol. The primary purpose of the guide is to help math students expand their mathematical vocabulary; LaTeX is included to help those who would like to communicate with others on forums and write documents in LaTeX during their studies. So this guide may be useful as a reference, but bear in mind that its focus is not on teaching LaTeX.

The Comprehensive TeX Archive Network (CTAN) has an even more detailed resource called The Comprehensive LaTeX Symbol List [PDF]. This document is over 400 pages long and lists over 18,000 symbols, plus the LaTeX commands and packages required to create them. The table of contents divides the symbols into various broad groups, including body-text symbols, mathematical symbols, scientific symbols, and ancient language characters.

# Viewing Expressions as LaTeX Markup

A good way to learn how to write LaTeX is to examine original markup. MathJax makes it very easy to view equations as they were originally written.

To see how an equation appears as LaTeX markup, right-click it and select **Show Math As > TeX Commands** from the menu. This will trigger a small pop-up window that contains the equation written in LaTeX.

Another option is to right-click the equation and select **Math Settings > Math Renderer > Plain Source**. This will make the original markup appear in the book in place of a human-readable mathematical expression. To go back to the default setting that renders LaTeX in a human-readable way, select **Math Settings > Math Renderer > CommonHTML**.

Try out these methods on the following equation:

[latex]a^2+b^2=c^2[/latex]

# External LaTeX Editors

It can be tedious to make an attempt at an equation in LaTeX while working in Pressbooks, especially if you’re writing a particularly complex equation or you’re still getting the hang of LaTeX and need to work through a lot of trial and error. Instead of repeatedly writing and hitting Preview, you can use an external LaTeX editor, which may be in the form of a web-based or desktop application.

The Interactive LaTeX Editor by Paul Lutus is a web-based editor that quickly renders LaTeX using up to three different renderers, including MathJax. This is particularly useful to Pressbooks users, since you can see exactly which commands do and do not work with MathJax. The editor also allows you to see examples of symbols, Greek letters, and common science and math expressions written in LaTeX.

MiKTeX is a free distribution of TeX/LaTeX. When downloaded, MiKTeX also comes with the editor TeXworks. This option is less ideal than Paul Lutus’s editor, as you must use some basic LaTeX document structuring for anything to compile correctly, which is not necessary in Pressbooks. However, all the bare-bones formatting you need is the following:

`\documentclass{article}`

`\begin{document}`

`Write your equations here.`

`\end{document}`

The advantage of MiKTeX is that, if you try to use a command from a package that is not currently being used by your installation of LaTeX, MiKTeX will offer to download it for you. This helps to circumvent some of the mystifying errors that can appear when you try out a command found online without realizing that it’s from a package that you need to explicitly call on.

If you are a Mac user, a recommended program is LaTeXiT. The graphical interface of this equation editor allows you just to focus on equations, which is great for beginners who don’t want to be bogged down in the details of document formatting, but the program also allows for further customization from those who do want to control things like what packages are used.

# LaTeX and PDF Exports

PDFs in general can be finicky, and LaTeX can sometimes fail to display properly in a PDF. If your LaTeX renders properly in the webbook but not the PDF, there are a few possible sources of the problem.

## Excess Spaces

PDFs will often not display LaTeX formulas that are written with more than one space in a row. (Note that this is only the case with LaTeX rendered with MathJax, not QuickLaTeX.)

For example, the equation `[latex]A = \dfrac{1}{2} b h[/latex]`

looks fine in the webbook.

[latex]A = \dfrac{1}{2} b h[/latex]

But here’s a screenshot of what it looks like in the PDF.

Spaces are not needed between LaTeX commands in order for them to be rendered in a readable way because equations are automatically spaced appropriately. However, it is common to insert spaces to make equations more readable for humans working in the back end. If you choose to add single spaces between commands for your own readability as a LaTeX writer, be sure to search for excess spaces using the Find feature in your browser and delete them.

## Problems with MathJax

If there are no excess spaces in your commands and you’re using `[latex]…[/latex]`

delimiters, then there may be a problem with MathJax, the engine that renders LaTeX in Pressbooks.

You can check to see if MathJax is working properly by going to **Settings**** > MathJax** on the Pressbooks dashboard. You should see a big integral at the top of the page, as in the following image:

Below the integral is the sentence, “If you can see a big integral, then PB-MathJax is configured correctly, and all is well.”

However, if there’s a problem with MathJax, you may see unrendered LaTeX instead of an integral at the top of the page. If so, there may be something wrong with the plugin for the CUNY instance of Pressbooks. Please contact us as soon as possible to let us know about the problem.

For more details about using mathematical notation in Pressbooks, including by using markup languages other than LaTeX, see Add Mathematical Notation in the Pressbooks User Guide. |

- This chapter is a remixed version of chapters in the section LaTeX Questions Answered in the BCcampus Open Education
*Pressbooks Guide*by Lauri M. Aesoph, published under a Creative Commons Attribution 4.0 International License. Changes have been made in accord with the style, structure, and audience of this guide. ↵

A programming language built to input, format, and display mathematical and scientific formulas.

The visual—WYSIWYG—editor view in Pressbooks which allows you to see how content will be formatted in different exported files and the webbook.

A self-contained version of Pressbooks administered by a particular group. Different instances of Pressbooks may have different features, or may be reserved for particular users. A Pressbooks instance is also called a "domain" or "network."