As mentioned in the overview, Section 3.6, we use LaTeX syntax for mathematics. In order to allow for quality display in HTML, and other electronic formats, this limits us to the subset of LaTeX supported by the very capable MathJax Javascript library. Generally this looks like the amsmath package maintained by the American Mathematical Society at their AMS-LaTeX page. For a complete and precise list of what MathJax supports, see MathJax’s Supported TeX/LaTeX commands. Once you have digested this more general section, be sure to also consult Section 4.10 for some very specific suggestions.
Use the <m> to place variables or very short expressions within a sentence of a paragraph, the content of a <title>, a <cell> of a table, a footnote, or other similar locations of sentence-like text. You can’t cross-reference this text, nor make a knowl with it. Though you can typically cross-reference a containing element.
Longer mathematical expressions in an <m> element can lead to awkward line breaks, both in HTML output, and especially in PDF generated from LaTeX. And complicated fractions or integrals can introduce abnormal line-spacing that is distracting to a reader. As a rough rule-of-thumb, keep an inline expression shorter than a moderately-long regular word and avoid tall constructions. This should allow LaTeX’s line-breaking algorithms the best chance of success.
So a simple, short equality such as \(x=2\) should not cause a problem, but if you want to claim that the probability distribution of the normal distribution has the right scaling factors, \(\int_{-\infty}^{\infty}\frac{1}{\sigma\sqrt{2\pi}}e^{-\frac{1}{2}\frac{\left(x-\mu\right)^2}{\sigma}} dx = 1\text{,}\) there is a good chance it will do less harm to your paragraph of you display it
The <me> element can be used for longer expressions or a single equation. Typically you will get vertical separation above and below, and the contents will be centered. See below about concluding periods (and other punctuation), and alignment. The <men> variant will produce a numbered equation, and therefore with a provided @xml:id attribute, can be the target of a cross-reference (<xref>).
We begin with a pure container, either <md> or <mdn>. The former numbers no lines, the latter numbers every line. Within the container, content, on a per-line basis, goes into a <mrow> element. You can think of <mrow> as being very similar to <me>. In a LaTeXalign the \\ mark the end of a displayed line. In PreTeXt each <mrow> delimits a displayed line, and there are no \\s. Use \amp to mark the alignment point.
On any given <mrow> you can place the @number attribute, with allowable values of yes and no. These will typically be used to override the behavior inherited by the container, but there is no harm if they are redundant. A given line of the display may be the target of a cross-reference, though the numbering flexibility means you can try (and fail) to target an unnumbered equation.
An <mrow> may have a @tag attribute in place of a @number attribute. This will create a “number” on the equation which is just a symbol. This is meant for situations where you do not want to use numbers, and the resulting cross-reference is “local.” In other words, the <xref> and its target are not far apart, such as maybe within the same <example> or the same <proof>. Allowable values for the attribute are:
These are the names of symbols, with prefixes where the prefix d means “double”, and the prefix t means “triple”. Cross-references to these tagged equations happens in the usual way and should behave as expected. See Section 3.4 and Section 4.5 for more on cross-references.
The LaTeX macros, \amp, \lt, and \gt are always available within these mathematics elements, so that you can avoid the exceptional XML characters &, < and >. See Section 3.14 for this same information, but in the broader context of your entire document.
Once in a while, you need a little bit of “regular” text within an expression and you do not want it to look like a product of a bunch of one-letter variables. Use the \text{} macro for these. Only. Other ways of switching out of math-mode and into some sort of “regular” text will appear inferior, and can raise errors in certain conversions.
Do place surrounding spaces inside the \text{} macro.
This example amply illustrates the use of macros for XML exceptional characters (twice), appropriate use of the \text{} macro (twice), spaces in the \text{} macro (once), sentence-ending punctuation (see the source, the period is not inside the <me> element) and yes, we did think twice about the \\ (an exception to the rule).
There is a temptation to use color to indicate or highlight portions of mathematics, especially for electronic outputs where color is easy and cheap. But before you leap, how will this work in black-and-white printed output? How will it work for a blind reader using a screen-reader or a braille version?
Subsection4.9.7Cross-References in Display Mathematics
A cross-reference is achieved with the <xref> element, see Section 3.4. You can place an <xref> inside a <mrow>, and remarkably, it will do the right thing. This is one of only two XML elements you can mix-in with LaTeX syntax. A typical use is to provide a justification or explanation for a step in a proof, derivation, or simplification. And it works best with alignment, see below.
Displayed mathematics is implemented with the AMS-LaTeXalign environment. Ampersands are used to control this, so use the \amp macro for these. The first ampersand in a line or row is an alignment point, typically a symbol, like an equality. The next ampersand is a column separator, then the next is an alignment point, then a column separator, then… The moral of the story is you should have \(n\) alignment points, with \(n-1\) column separators, for a total of \(2n-1\) ampersands—always an odd number.
<md>
<mrow>A \amp = B \amp D \amp = E \amp \amp \text{Because}</mrow>
<mrow> \amp = C \amp \amp = F \amp \amp <xref ref="txo" /></mrow>
</md>
produces
\begin{align*}
A \amp = B \amp D \amp = E \amp \amp\text{Because}\\
\amp = C \amp \amp = F \amp \amp\knowl{./knowl/xref/txo.html}{\text{Table 4.5.3}}\text{.}
\end{align*}
Sometimes you want several short equations on one line. Do not use <me>. Instead use a single <mrow> inside an <md>, and use alignment to spread them out evenly.
You can fool the alignment behavior by hiding all your ampersands in macro definitions, so there is the optional @alignment attribute for the <md> or <mdn> element, in order to force the right kind of alignment. Allowable values are gather, align, and alignat. The latter is similar to align, but no space is automatically provided between columns. You can leave it that way, or explicitly add your own. For example, this allows you to precisely arrange individual terms of a system of linear equations, especially when terms with zero coefficients are omitted. When using the alignat option PreTeXt tries to count ampersands to see how many columns you intend, since LaTeX needs this number (we are not sure why). This detection can be fooled too, especially if you have something like a matrix with lots of ampersands for other purposes. So set the @alignat-columns attribute to the number of intended columns, if necessary.
Commutative diagrams may be authored using the AMS LaTeXamscd package. While restricted in some ways, such as the lack of sloped or curved arrows, it has one important advantage over more general drawing tools. Support for HTML output comes from MathJax, and hence has accessible versions included in the output.
Typical use would be within an <me> element, so starting with \begin{CD}. Despite this being multi-line output, we have not chosen to integrate it within the more general md/mrow structure, but that decision can be revisited.
The other mix-in XML element is <fillin>. It may use a @fill attribute with some math string to determine width, height, and depth of the blank. Or it may use a @characters attribute that takes an integer value to hint at the width.
Subsection4.9.11Page Breaks for Tall Display Mathematics
For print output, do nothing additional and LaTeX will do its best to break your display between lines. You can turn this behavior off by setting the @break attribute on the <md> or <mdn> to the value no. Once you do this, you can then selectively allow a page break after a given <mrow> by setting the @break attribute on the <mrow> to the value yes.
These go in the <docinfo> section, wrapped in a <macros> element. Keep them simple—one or two arguments, and one-line definitions. This is not the place to be fancy, and not the place to try to end-run the structural aspects of PreTeXt. The idea is to define something like \adjoint{A} for the matrix A to be a superscript asterisk, and later you can change your mind and use a superscript dagger instead. Keep in the spirit of PreTeXt and use readable, semantic macros. For example, do not use \a{A} for the adjoint of A. Repeating: keep your LaTeX macros simmple, and to a single line.
The name of your macros also cannot contain any numbers, otherwise MathJax will “silently” fail and may not read any subsequent macros you might have. This is important because PreTeXt will place custom macros for you at the end of your own, defined at Subsection 4.9.4, to be used. Those would fail to be processed by MathJax if your own macros caused it to stop reading.
We have resisted using overly-verbose MathML for mathematics, or worse, inventing our own XML vocabulary for mathematics. LaTeX syntax generally works great, but to work even better within PreTeXt an author needs to take a few extra steps. Your work will translate better to a variety of formats, and will be easier to maintain, with well-designed macros. A well-designed macro will convey the mathematical meaning of the object to a reader of your source, without them looking at your definition of the macro. In situations where a mathematical object might be written with different notation, it should be trivial to change the macro’s definition and preserve the mathematical meaning. For example, consider two versions of a binomial coefficient:
Vertical bars are used for a variety of mathematical objects. Paired to create functions of expressions: absolute value, \(\left\lvert x-1\right\rvert\text{;}\) norm of a vector, \(\left\lVert \mathbf{v}\right\rVert\text{;}\) cardinality of a set, \(\left\lvert X\right\rvert\text{;}\) and the determinant of a matrix, \(\left\lvert A^k\right\rvert\text{.}\) As relations: division, \(a\mid b\text{;}\) parallel lines \(L_1\parallel L_2\text{.}\) Sets are another use: \(E=\left\{x\in{\mathbb Z}\,\middle\vert\, x\equiv 0\pmod 2\right\}\text{.}\)
LaTeX\vert, \Vert, \lvert, \rvert, \lVert, \rVert are the delimiters, where l and r refer to left and right, and the capitalized versions are a pair of vertical lines. The qualifiers \left and \right can be used to have the length of the bar grow to match what it encloses. Note that there is a \middle that we have used above with \vert for the set \(E\text{,}\) and we have added space on either side. \mid and \parallel are relations, used above to indicate divisibility and parallel lines, and so automatically get an extra bit of spacing on either side.
The “times” symbol sees many uses that are different: dimensions, multiplication, and more complicated products (such as a Cartesian product of two sets). Macros \product, \times, and by could carry different meanings, even if each one is defined by the \times symbol, \(\times\text{.}\) For example:
If a chunk of displayed math concludes a sentence, then the sentence-ending punctuation should appear at the conclusion of the display. (And certainly not at the start of the first line after the display!) But do not author the punctuation within the mathematics element, put it afterwards, where it logically belongs.
More specifically, place a sentence-ending period (say) immediately after the closing of an <me>, <men>, <md>, or <mdn> element. PreTeXt will place the period in your output in the right place and in the right way. (By using LaTeX’s \text{} macro, if you are curious to know the details.) Here is an example. The XML source
This all applies more generally to clause-ending punctuation, such as a comma. Take notice of the requirement that the punctuation must be immediately after the closing tag of the math element, otherwise it will not migrate properly. For example, do not interrupt the flow with whitespace, or an XML comment, or anything else.
For inline mathematics (the <m> element) the same authoring principle holds, though you would likely do this naturally. Author the punctuation outside the element, where it will remain.
Here is a technical subtlety that will demonstrate some of the inner machinery of PreTeXt and our conversions. In your work, locate a theorem that has some numbered display mathematics (mdn) which is at the end of a sentence, and which you have authored as described above. In HTML output, test a cross-reference (xref) to the theorem and you will see the period for the end of the sentence at the end of the display, where it should be. Now test a cross-reference (xref) to one of the numbered equations. First, the knowl will contain the entire display, to provide context, but it also will not contain the period, since the rest of the sentence is not in the knowl and so the period is not necessary.
Best Practice4.9.2.Authoring Punctuation after Mathematics.
Always follow the instructions in Subsection 4.9.14 about placing all punctuation following mathematics after the math element, not inside it. PreTeXt will do the right thing for display math for you. But furthermore, there are some special situations where the output format is not visual, such as braille or audio, where the placement of the punctuation is both different and important to not confuse “reader.” You can help ensure your various outputs are of the highest quality by observing these sorts of details.
It is common to make lists of expressions, equations, or identities. Think of the definitions of trigonometric functions, a collection of antiderivatives, or a compendium of generating functions.
In these situations, author a list item, <li>, within an <ol> or <ul>, by using only the necessary <m> element. Do not use an intervening <p>, and do not include any adjacent text. Whitespace is OK. Then PreTeXt will add LaTeX’s \displaystyle command to improve the visual appearance of the mathematics, and so you do not need to.
Note that any text, other than whitespace, outside the <m> tag will disable this feature, including punctuation. However, according to the Chicago Manual of Style[1, 6.127], “Items carry no closing punctuation unless they consist of complete sentences.” So that comma at the end of your equation probably doesn’t belong there anyway.
LaTeX, which we use as the syntax for mathematics, allows extensive customization through packages, which are enabled through a \usepackage{} macro. Packages also provide extensive customization or control over the document structure. Thus, PreTeXt provides support for additional packages to enhance mathematical content, but does not have any facility beyond this (e.g. to influence or support document structure).
We use LaTeX to create PDF and print output. For most other electronic formats, we use MathJax to embed mathematics onto a web page or to make images of mathematics used in EPUB, and so on. MathJax has extensions, which mimic certain LaTeX packages. For a list of supported extensions see MathJax’s TeX/LaTeX Extension List. Note that some of these extensions are technical (not supporting mathematical syntax) and some we load already as part of setting up MathJax, so carefully evaluate how a given package is selected to support certain macros.
Suppose there is a LaTeX math-mode macro you would like to use. We will illustrate with the \cancelto macro that is part of the fairly standard cancel package. It will draw an arrow through a mathematical formula, with a replacement value. Conveniently, MathJax has a cancel extension, and this extension also defines the \cancelto macro. You need to check this, as a LaTeX package and a MathJax extension are not guaranteed to define the same collection of macros. Nor are the two guaranteed to have the same name (though this is best practice).
Note that both attributes @latex-name and @mathjax-name must be present. They may be identical (as above, and typical) or they may be different (rare). There are situations where one might be empty. Of course, you can use these macros in other definitions of mathematics macros you might make (Subsection 4.9.12). However, we do not guarantee the absence of conflicts with other packages in use, even if employed by PreTeXt. Nor do we support debugging such conflicts.
If there are LaTeX macros you desire, but without a MathJax implementation, then there is not much we can do, short of suggesting you write your own extension (not recommended, see A Custom Extension). There are some workarounds which we may document shortly.
Titles are a key feature of PreTeXt, and migrate to various places in different conversions, where they might be mixed with bold text, or where they do not render properly. Please report any substandard situations. A good example of this phenomenon is mathematics in division titles, where we provide a <plaintitle> alternate. See Section 4.8 for a complete discussion.
We are moving away from built-in automatic support for the \sfrac{}{} macro. As of 2023-10-18 you may use this macro, but results will just look like regular inline fractions. However, you can get superior typesetting in PDF output by loading the xfracLaTeX package with the following in <docinfo>:
Historically, we provided good internal support for the LaTeXextpfeil package of extensible arrows, even though the LaTeX version has some severe shortcomings. As of 2023-10-19, it is now an author’s responsibility to elect this package, along with the reliable MathJax extension of the same name. Add to <docinfo>:
As mentioned at the start of this section, your use of LaTeX needs to also be supported by MathJax so that it may be rendered as part of an HTML page displayed in a web broswer. In addition to the information at the start of Section 4.9, this subsection has some notes that may help you navigate this situation.
Generally, MathJax supports commands available in the amsmath package.
The \matrix{} command, and its friends (such as \pmatrix{}) are not supported by LaTeX, despite being recognized by MathJax. So use environments like \begin{matrix} and \begin{pmatrix} (with their corresponding \end{}, of course) and you will get accurate results for both formats.