## Section 7 Mathematics

To be able to create both LaTeX and HTML output (plus variations), we rely on MathJax, which in turn supports an extensive subset of the mathematical symbols normally available. The AMSMath symbol set is a good approximation. The PreTeXt Guide has a link to the complete list of macros supported by MathJax. We load the `AMSsymbols`

library and the library for extensible arrows, `extpfeil`

.

### Subsection 7.1 Basic Mathematics

The following is from the MathJax demonstration page^{â€‰1â€‰}, an identity due to Ramanujan:

And again, from the MathJax demonstration page, Maxwell's equations:

A small test that the extensible arrows library is included properly:

Look back at the top of the source file of this document to see how to include your TeX macros just once. For best results keep your macros simple and semantic.

PreTeXt provides modest built-in support for â€śslantedâ€ť, or â€śbeveledâ€ť, or â€śniceâ€ť fractions. To wit, we mean fractions such as: \(\sfrac{3}{8}\text{.}\) Use the pre-defined `\sfrac{}{}`

macro in your mathematics to achieve this presentation. (The presentation in HTML output could be improved. Please make suggestions if your skills with LaTeX macros and knowledge of MathJax's supported commands warrant.)

We consider a system of equations. We number the first and last equation (there are just two) and include an `xml:id`

on each. We reference the whole system later as the range of equations from the first to the last.

### Subsection 7.2 Displayed Mathematics

Multi-line displays of mathematics are achieved with the `md`

tag ( â€śmath displayâ€ť), and the variant that produces numbers on each line, `mdn`

( â€śmath display numberedâ€ť), used within a paragraph (`p`

). As a good example of how XML syntax is superior, you author \(n\) lines of equations by enclosing each line inside of a `mrow`

tag, rather than using \(n-1\) separators (such as `\\`

).

If you use no ampersands to express alignment (read ahead), then each equation is centered independently on the width of the text. This is implemented according to the AMSmath LaTeX package's `gather`

environment. Example:

An ampersand is used, in two ways, to describe positioning several equations per line, organized in columns. We have created the pre-defined LaTeX macro `\amp`

as one way specify these, but the escape sequence `&`

may be used also. The second, fourth, sixth, â€¦ ampersands separate columns, and the spacing between columns will be provided automatically. The first, third, fifth, â€¦ ampersands are alignment points for the equations in each column. Typically this is placed just prior to a binary operator, such as an equal sign (`\amp = `

), or for a column of explanations or commentary, just prior to the `\text{}`

macro. Note that this scenario suggests always having an odd number of ampersands in each `mrow`

. In the example below, alignment is on the equals sign in the first two columns, and provides left-justification to the explanations in the third column. N.B.: the use below of the `\text{}`

macro does not include mathematics within its argument. Doing so may yield unpredictable results depending on your choice of delimiters for the mathematics (and using an `m`

tag will be ineffective).

PreTeXt will automatically detect the presence or absence of ampersands, but by defining macros for entire aligned equations, you can effectively hide the ampersands. So the `@alignment`

attribute can override automatic detection. We use a simple LaTeX macro to demonstrate setting `alignment='align'`

to override the use of a `gather`

environment and use a `align`

environment instead. Example:

The AMSmath LaTeX package's `alignat`

environment is a third variant of alignment. It never happens automatically, you need to ask for it with `alignment="alignat"`

. It is very similar to `align`

but adds no space between the equation columns. So you can leave it that way, or you can add your own â€śextraâ€ť space to suit. Here is a previous example with no inter-column space:

This modified example has a middle row with three columns, while the other rows have just one column, as a test of our routines for determining the `mrow`

with the greatest number of ampersands (and how many there are),

Final example demonstrates that ampersands in other objects (matrices here) can wreak havoc with computing the number of columns. So we provide yet another attribute to override automatic detection, `alignat-columns`

. This is the number of *columns* not the number of *ampersands*. Generally, for \(c\) columns, there will be \(2c-1\) ampersands.

One caveat: if your number of ampersands is even (see advice above about using an odd number) behavior should still be correct, as in next example.

If you want super-precise control over alignment of the terms of a system of equations (linear or not) you can use the `alignat`

option to advantage by not including any extra space. This example is modified slightly from a post by Alex Jordan:

Beautiful.

A long equation, to check layout on various screen sizes. This is Weil's â€śexplicit formulaâ€ť for the Riemann \(\zeta\)-function:

#### Example 7.1. Excessive Display Mathematics.

In print versions, a long run of displayed equations often needs to be broken across pages. If you are reading some other version of this, then there is nothing to see here. But for LaTeX output it could be interesting. First, with no extra effort, this page-long display should break naturally, no matter how the preceding material changes.

In this version we have turned off page breaking for the entire display, but then allowed a break at every fourth equation, so you should see a reasonably attractive page break right after one of the \(m^2+n^2=p^2\) equations.

So. Do not take any extra steps and let LaTeX figure out the breaks. If you do not like a break, modify the `md`

or `mdn`

to go back to the AMSmath default behavior and not break at all. Ever. Or rather, go further and modify an individual `mrow`

to suggest that it is a good place for a break.

This is a poorly-authored paragaph to test the conversion to HTML. There are two displayed equations, separated by a period ending the first one's sentence, which should migrate into the display, and not leave behind an empty paragraph:

This final sentence should remain, inside another HTML paragraph, without the second equation's period.

### Subsection 7.3 LaTeX Packages and MathJax Extensions

If you would like to use macros from a LaTeX package *and* there is a MathJax extension *of the same name* which implements the same macros, then you may use these with your mathematics as we demonstrate here.

This example is from Jason Underdown. The package is named `cancel`

and is included in the TeXLive distribution, so is fairly standard. The particular macro being demonstrated is `\cancelto{}{}`

.

Look at the source of this article to see the package name being supplied in a `latex-preamble/package`

element within the `docinfo`

section. That is the only setup required to make the macro usable in LaTeX and HTML output.

The packages appear before the author-supplied macros, so you can use macros from the packages as building blocks for document-specific macros. We cannot guarantee there will be no conflicts between additional packages and those in use normally, or added in the future. So use at your own risk.

### Subsection 7.4 Advanced Mathematics

MathJax is extremely capable in rendering a subset of LaTeX in web browsers, and improving all the time. You can get fairly fancy with some of its supported commands. In particular, if you need to mix in a few words with your mathematics, the `\text{}`

macro is supported. For example, you might use an â€śifâ€ť or an â€śotherwiseâ€ť in the definition of a piecewise function.

Consider that the first line below is text sandwiched in-between two Greek letters, wrapped in a `\text{}`

macro. In HTML output we have taken care that the font for text material within display mathematics should match the font of the surrounding paragraph, as also happens with LaTeX output. The second line is nearly identical in the source, but is just naked text being rendered like a slew of variables.

We are not suggesting here that using words in place of symbols, as in the first line, is a good practice. (It is not.)

The following example is a good stress-test of using the `\text{}`

macro to achieve certain effects. Note the Unicode left and right smart quotes. This a contribution from Alex Jordan as part of his work on APEX Calculus.

And another one from Alex. Note the use of the `\mathord{}`

and `\mathrel{}`

macros to control spacing around the mathematical symbols. Examine the source to see how the quotation marks have been authored with XML syntax for Unicode characters, since we do not allow most markup inside mathematics.

Generally, you cannot use any XML elements inside of the mathematics elements. An exception is the `xref`

element which you might want to use to provide justifications for the steps of a derivation. Here is a visual example that is mathematically meaningless,

Scott Beaver likes to write short chains of equalities all in one line, with the cross-references sitting on each equals sign. Here we test the LaTeX `\overset`

and `\underset`

macros wrapping a PreTeXt `<xref>`

, with and without content, inside an `<me>`

element. Note that `\stackrel`

is obsolete, and `\overunderset`

is not yet supported by MathJax (but see GitHub #2704^{â€‰2â€‰}). The mathematics is Scott's, the reasons are totally unrelated to the math.

We suggest using cross-references that only display numbers (`<xref>`

with `@text`

set to `global`

) since if you stick to elements like `<theorem>`

, `<lemma>`

, `<definition>`

, or `<axiom>`

, then the numbers will be unambiguous and the target of the cross-reference will contain full information. But note that if you mix in divisions, or perhaps figures, as reasons then there is a possibility that numbers will need to be qualified by their type. We have provided an abbreviation for one cross-reference to TheoremÂ 2.1 (which will not benefit from automatic translation to other languages).

### Subsection 7.5 Local Tags on Equations

If you are not writing a research monograph, maybe (a) you will not use many numbered equations, or do not like the looks of them, or feel they scare your readers, and (b) maybe your cross-references are always local-ish, like strictly within an `example`

or a `proof`

. For this situation you can create, and employ, a â€ślocalâ€ť tag on a displayed equation. Nothing enforces the idea of what constitutes local, and there is nothing to stop you from using the same symbols more than once. With freedom comes responsibility.

Use the `@tag`

attribute on an `mrow`

, only. (Remember, you can have just one `mrow`

.) The behavior is identical within an `md`

or `mdn`

. The value of the `@tag`

attribute is a symbol name. The prefix `d`

means â€śdoubleâ€ť, and the prefix `t`

means â€śtripleâ€ť. So allowed values are

star, dstar, tstar dagger, ddagger, tdagger daggerdbl, ddaggerdbl, tdaggerdbl hash, dhash, thash maltese, dmaltese, tmaltese

Cross-references to these tagged equations happens in the usual way and should behave as expected. We test the double versions to make sure the symbols render properly in various output formats.

Here are the local cross-references: (âś¶âś¶), (â€ â€ ), (â€ˇâ€ˇ), (##), (âś âś ). We test another farther away in SectionÂ 20, contrary to our advice above.

### Subsection 7.6 Commutative Diagrams

This diagram is authored by Tom Judson using the syntax of the AMS LaTeX â€śCDâ€ť package. Inside of a `<me>`

element start with `\begin{CD}`

. Remember to escape the less-than character.

While this package is not as flexible as some generic drawing packages, it has the advantage of full support by MathJax, and thus the HTML version will be more accessible.

### Subsection 7.7 Line-Breaking after Mathematics

As of 2021-05-14, in HTML output the next sentence should just fill a full line across the page. We take active measures to bind the concluding period to the final bit of mathematics, the variable \(x\text{.}\) The prevents a bad line break which could see the period *begin* a new line, all by itself. In the event that the line-breaking siutation improves, we could relax these measures. This testing is only relevant to HTML output, not LaTeX output.

xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx \(x\text{.}\)

### Subsection 7.8 Fonts and Mathematics

This section is about testing types and sizes of fonts, not so much about using different typefaces. First, one long displayed equation, which is designed to be full-width for LaTeX output when using defaults as of 2020-01-29 (commit `defd4bffd462e7ea`

).

Start paragraph.

End paragraph.

The next paragraph has five ways to write the sine of \(x\text{,}\) bracketed by plain text versions. This tests font size and the fonts employed. The raw source of this paragraph is (spread over two lines)

sin x | <m>\sin x</m> | <m>\text{sin}\ x</m> | <m>\mathrm{sin}\ x</m> | <m>\text{sin}x</m> | <m>sin x</m> | sin x

The five ways, from good to bad,

The best way, using

`\sin`

. Note the distance to the \(x\text{.}\)With a

`\text{}`

macro.With a

`\mathrm{}`

macro. Not recommended for PreTeXt.Without a space. Note that the previous two require explicit spacing.

No extra effort, so LaTeX renders as a product of four variables.

sin x | \(\sin x\) | \(\text{sin}\ x\) | \(\mathrm{sin}\ x\) | \(\text{sin}x\) | \(sin x\) | sin x

Finally a simple paragraph that places a text â€śxâ€ť next to a variable â€śxâ€ť.

wordxx\(x+x\)xxword

### Subsection 7.9 Miscellaneous

In HTML output, a MathJax workaround for a Safari rendering bug was inserting extra spaces after textual subscripts and superscripts ( MathJax thread^{â€‰3â€‰}). PreTeXt CSS now applies a correction. The following tests if the CSS fix is sufficient, and could be used to test the necessity of our fix in the future. Following is the original report, though `NOT`

has been moved to a superscript:

There should not be anything to see in LaTeX/PDF output. (2021-10-21)

`www.mathjax.org/demos/tex-samples/`

`github.com/mathjax/MathJax/issues/2704`

`groups.google.com/g/mathjax-users/c/ANjLK9KtcWA/m/vlHaPja-AwAJ`