Equation Formatting Primer for OriginLab’s LaTeX App

posted in: Apps, Graphing | 0

Introduction

In previous blog posts, I’ve introduced OriginLab’s LaTeX App. In this post, I will go over the basics for creating equation markup within the App that will generate attractive output. The information and examples are based on the fact that the App automatically includes the amsmath package which improves upon the default equation support built in to LaTeX. Note: this is not an authoritative treatment. If you want to pursue the subject in more depth, links to additional learning resources are available at the end of this post.

 

Using the LaTeX App to Generate an Equation

With a graph (or layout) window active, click on the LaTeX App in the App Gallery. The LaTeX Equation Editor will popup (See Figure 1). You then enter your equation markup and either click the Preview button to see a preview of the generated equation or the OK button to add the generated equation to the graph (or layout). In my introduction to the App, I explain how to resize, rotate and change the text color of the generated equation, so I won’t discuss that again.

Figure 1 (click to enlarge)
Figure 1 (click to enlarge)

 

Using an Online Equation Editor

If you don’t feel comfortable manually entering LaTeX equation markup, there are a number of online LaTeX equation editors that allow a user to generate markup using a graphical user interface. One of these is available here: https://www.codecogs.com/latex/eqneditor.php. You simply build your markup and then copy and paste the markup into the LaTeX App. Such generators produce generally decent and thorough markup.

 

Testing Equation Markup

If you want to test or debug errors in your equation markup, you can run it in Texworks, an application that ships with MiKTeX (the typesetting engine used for Origin’s LaTeX App). To do this, see the section Testing with TeXworks at the end of this post.

 

Equation Formatting Commands

In this section I will review basic commands for formatting equations using Origin’s LaTeX App. For the examples, I have added white space to the markup in order to make more clear the concept I am illustrating (white space is generally unnecessary much of the time). More information about white space in equation markup is provided below.

Important: For any but the most trivial equations, the three most important sections are probably Math Functions, Parentheses and Other Delimiters, and Fractions. So you should definitely read them.

Note: If any of the images of LaTeX output below aren’t clear, you can click on the images to enlarge them!

 

Automatic Single Equation Environment

The App automatically wraps the equation markup a user enters in additional LaTeX necessary to typeset a complete document. The markup includes a pair of \begin{equation} and \end{equation} commands that put the user-entered markup into a single equation environment as illustrated below. Note: equation numbering has also been disabled though I will provide a method for restoring numbering below.

 

Equation Fonts

Origin’s LaTeX App uses the default font for LaTeX, Computer Modern. There are three variants: Computer Modern Serif, Computer Modern Sans Serif, and Computer Modern Typewriter with the serif version being used for equations. I will discuss options for switching between them within an equation below.

Note: the App does not allow the user to select a different font than Computer Modern. Computer Modern is a tried and true, robust font that has been used for many years. Changing fonts in LaTeX is not always a easy thing and it isn’t as simple as creating a list in the App GUI that the user can select from. Hence, it was decided to stick with the LaTeX default.

 

White Space in Equation Markup

Within equation markup, white space is normalized. That is, spaces or lines breaks between terms are ignored (except in the case of the family of \text{} commands which I will discuss a little later). As you can see all of the following examples create the same output:

y=a+bx ltxp1
y = a + b \times x ltxp2
y
= a+
b \times x
ltxp3

 

Horizontal Spacing

LaTeX is by default very good at arriving at appropriate spacing by itself but there may be times when more spacing may be desired. For example, in the following integral:

y = \int x dx ltxp4

a little space is needed between the x and dx. It can be added using one or more of these spacing commands (ranked smallest to largest): \, \: \; \quad \qquad (negative spacing is possible using \!). Here is the previous integral with some space added:

y = \int x \, dx ltxp5

 

Font Style for Mathematical Terms

The font style for mathematical terms in a equation (as opposed to plain text which will be discussed later) can be modified using the following commands where the term to be modified is enclosed in braces:

2a+3b ltxp6 Default math style
\mathrm{2a+3b} p33 Upright
\mathit{2a+3b} ltxp8 Italics
\mathsf{2a+3b} ltxp9 Sans serif
\mathtt{2a+3b} ltxp10 Typewriter

A good example of usage is to correct the integral example from above to make the d in dx upright:

y = \int x \, \mathrm{d}x ltxp32

 

Adding Non-Mathematical Text to Equations

Non-mathematical text can be added to an equation using the following commands where the text is enclosed in braces. It should be noted that spaces in the text of these commands are respected and not normalized.

x \; \text{is the independent variable} ltxp11 Upright text for math font
x \; \textrm{is the independent variable} ltxp12 Serif text
x \; \textit{is the independent variable} ltxp13 Italic text
x \; \textbf{is the independent variable} ltxp14 Bold text
x \; \textsf{is the independent variable} ltxp15 Sans serif text
x \; \texttt{is the independent variable} ltxp16 Typewriter text

 

Mathematical Operators and Symbols

The basic math operators are as follows (with a \; spacer added between each):

+ \; - \; / \; \div \; \times \; \cdot \; x^{y} \; x_{y} ltxp17

In addition to those, LaTeX and the ams packages provide pretty much every mathematical operator and symbol ever needed by mere mortals. Tables of operators and symbols are available on this page: https://en.wikibooks.org/wiki/LaTeX/Mathematics

Additionally, certain symbols and characters must be escaped (or symbol-substituted) if they are to be output. The common ones are:

Unescaped: { } # $ % & _ \ ^
Escaped/Substituted: \{ \} \# \$ \% \& \_ \backslash \hat{}

 

Mathematical Functions

LaTeX and the ams packages provide a rich variety of commands for mathematical functions. For functions, simply entering the function verbatim like log, ln, exp, or sin is not ideal. There are defined commands for functions (look in the literature) that should be used when applicable. For example, don’t do this:

y = log A + ln B + ( exp( \pi C ) x D ) ltxp18

This is the correct way to enter a function using a defined command:

y = \log A +\ln{B} + ( \exp( \pi C ) \times D ) ltxp19

 

Parentheses and Other Delimiters

When enclosing terms in parentheses or other delimiters (e.g. ( ), [ ],{ }, | |, etc) attention must be taken to ensure that their height grows to enclose the content within the delimiter. For any but the most simple equation, delimiter pairs should be prefaced with either \left or \right. By doing so, they grow to the correct size (most of the time).

For example, look at the parentheses in this equation. Notice how they aren’t sized correctly and look terrible.

y = V_{max} (\frac{x^n}{k^n+x^n}) ltxp20

Now look at this example. Notice that the use of \left and \right before the parentheses make them the correct size.

y = V_{max} \left(\frac{x^n}{k^n+x^n} \right) ltxp21

If delimiters still aren’t the desired size, the following commands can be used in the place of \left & \right: \bigl & \bigr, \Bigl & \Bigr, \biggl & \biggr, and \Biggl & \Biggr.

When using braces { }, you must escape them like \left\{ and \right\}.

 

Fractions

Fractions can be specified using the \frac{}{} command where the two sets of braces contain the numerator and denominator. When LaTeX encounters fractions within fractions it decreases their size. For complex equations, fractional terms can become very small. for example:

y = \frac{ A_1 - A_2 }{ 1 + \left( \frac{ x }{x_0 } \right)^p } + A_2 ltxp22

To overcome this, there is a variant on the \frac command: \dfrac. \dfrac forces the fraction not to decrease in size. In the example below, notice how using \dfrac increases the size of the second fraction.

y = \frac{ A_1 - A_2 }{ 1 + \left( \dfrac{ x }{x_0 } \right)^p } + A_2 ltxp23

Continued fractions have their own command to avoid each level of fraction decreasing in size. Here is an example of the command:

y = \cfrac{ A }{ 1+ \cfrac{ B }{ 1 + \cfrac{ C }{ D } } } ltxp24

 

Multiple Lines and Multiple Equations

As previously mentioned, Origin’s LaTeX App wraps user-entered equation markup in a single equation environment by automatically adding \begin{equation} and \end{equation} commands before and after user-entered markup. This prevents the use of the \begin{multiline}, \begin{gather}, \begin{align} and \begin{alignat} commands typically used for equations with more than one line.

Fortunately there are subordinate display environment commands that can be used within the single equation environment that have much of the same functionality. Those commands include split, gathered, and aligned. Note: there is a fourth, alignedat, that I will not discuss it because it is a bit complex for this post.

With these three subordinate commands, line breaks are specified using \\. split and aligned also support using the & symbol to vertically align multiple lines at specific positions while gathered doesn’t.

Important: Be sure to read Delimiters Across Multiple Lines.

 


split
Command

The split subordinate environment is used when an equation in a single line is longer than desired and would be better presented broken up into two or more lines. Notice in the example below the placement of the & symbol to vertically align the three parts of the equation.

\begin{split}
y = y0 &+ A1\times \exp(-(x-x0)/t1) \\
&+ A2 \times \exp(-(x-x0)/t2) \\
&+ A3 \times \exp(-(x-x0)/t3)
\end{split}
ltxp25

 

aligned Command

The aligned subordinate environment is especially useful for presenting multiple equations. Like split, it supports the & symbol for vertical alignment. Notice in the equation below the manual addition of pseudo line numbers using \mathrm{} with a spacer and a second & to align them. Also notice the addition of [12pt] to the line break (\\). That notation allows the vertical spacing to be customized.

\begin{aligned}
y &= Y_b + A \left( 1-e^{ \dfrac{ -(x-x_0) }{ \tau} } \right) \qquad &\mathrm{(1)} \\[12pt]
y &= Y_b + A_1 \left( 1-e^{ \dfrac{ -(x-x_0) }{ \tau_1} } \right) +
A_2 \left( 1-e^{ \dfrac{ -(x-x_0) }{ \tau_2} } \right) \qquad &\mathrm{(2)}
\end{aligned}
ltxp26

 

gathered Command

The gathered subordinate environment simply centers multiple lines. It does not support the & symbol for vertical alignment. For example:

\begin{gathered}
y = A + Bx \\
y = A + Bx + Cx^2 \\
y = A + Bx + C x^2 + Dx^3
\end{gathered}
ltxp27

 

Adding a Separate Line of Text to an Equation

The amsmath package provides for adding an arbitrary separate line of text in an equation via the \intertext{} command. Unfortunately, it is not available in a single equation environment which Origin’s LaTeX App uses. However, an approximation can be made by wrapping markup in an aligned command and using with a line break specifier (\\) and \text{} command. For example:

\begin{aligned}
&y = y0+A \, \mathrm{e}^{ - \tfrac{ x }{ t_0} } \, \sin \left( \pi \, \frac{ x-xc }{ w } \right) \\[12pt]
&\text{where} \; A > 0 \text{,} \: w > 0 \text{,} \: t_o > 0
\end{aligned}
ltxp28

 

Case Notation

To output case or piece-wise notation using Origin’s LaTeX App, use the aligned and cases commands like in the following example. Notice the use of the & alignment token to align the if s.

\begin{aligned}
f(x) =
\begin{cases}
\exp{ \left( x \right)} &\text{if} \; x \geq 0 \\
1 &\text{if} \; x < 0
\end{cases}
\end{aligned}
ltxp29

 

Delimiters Across Multiple Lines

There may a case where delimiters (parentheses, brackets, etc) need to span more than one line. Doing the following will generate an error:

\begin{split}
\left( \int BBB \\
CCC \right)
\end{split}
This causes an error!

What has to be done is to add an invisible delimiter (\left. or \right.) to each line such as:

\begin{split}
\left( \int BBB \right. \\
\left. CCC \right)
\end{split}
ltxp30

But then it can be seen that the size of the delimiters are not the same. So a special command, \vphantom{}, needs to be added to the line with the smaller delimiter. \vphantom{} takes one argument, in this case the tallest element in the line with the tallest delimiter. In the example below, the \int command on the first line enlarges the parenthesis to its left. That delimiter size needs to be matched in the second line. So \vphantom{} is used with the \int function symbol as its argument. Now it can be seen that now the delimiters match.

\begin{split}
\left( \int BBB \right. \\
\left.\vphantom{\int} CCC \right)
\end{split}
ltxp31

 

Conclusion

It can be see that there are many options for formatting LaTeX equation markup in Origin’s LaTeX App. But that does not mean it is necessarily difficult. In fact, once you understand some of these concepts, it is actually quite easy to create attractive and meaningful equations using the App.

Thank you for reading!

 

Sources for In-Depth Information

The following are very good resources for learning more about using the amsmath package to typeset equations in LaTeX. You are also encouraged to Google because the web is brimming with useful information the subject.

User’s Guide for the amsmath Package (Version 2.0)
Short Math Guide for LaTeX
LaTeX Wikibook/Mathematics
LaTeX Wikibook/Advanced Mathematics

 

Testing with TeXworks

TeXworks is an open-source GUI application for typesetting LaTeX documents. It can be used to test and debug errors in equation markup. A shortcut to TeXworks should appear in the MiKTeX folder of your Windows Start menu (or whatever it is for your version of Windows). If you can’t find it there, TeXworks.exe should be found somewhere in the \miktex\bin\ folder where you originally chose to put MiKTeX when you installed it.

The TeXworks user interface can be seen in Figure 1. It consists of a text editor in which to enter LaTeX markup.  “pdfLaTeX” should be selected as the typesetting method. Once the markup is entered, the green arrow button can be clicked to typeset the markup. If it runs correctly, another window will open displaying the output in the form of a PDF allowing you to see how your equation will look. I have provided the required additional LaTeX markup in a code listing after Figure 1. It can be copied and pasted it into TeXworks and then the actual equation markup can be put in the indicated line.

Figure 1 (click to enlarge)
Figure 1 (click to enlarge)

Required LaTeX markup to run in TeXworks

If there is a syntax error in the LaTeX markup or some other error occurred, the text editor will display a “Console output” tab (see Figure 2). In that tab, TeXworks will show the error though the message may be a bit cryptic. Before fixing an error, it is best to click the red stop button to stop and reset the typesetting process. The button will then turn back into the green arrow button. Debugging complex equation markup may be an iterative approach, so some patience may be required.

Figure 2 (click to enlarge)
Figure 2 (click to enlarge)

 

Leave a Reply