Mathematics/Tutorials

Unofficial ConTeXt Wiki mirror

Last modified: 2025-02-10

Math modes

TeX was designed for ease of typesetting books that contained mathematics. As ConTeXt is built on top of TeX, it inherits all those features. In addition to these, ConTeXt adds lot of macros to make the typesetting of mathematics easier.

There are two kinds of math modes --- inline math and display math. Mathematical expressions that are written with the running text are called inline math; while mathematical expressions that break the flow of the text (such as formulas or equations) are called display math. TeX takes care of proper spacing around expressions and provides macros to typeset most mathematical constructs. Complicated expressions can be built by working in steps---break down the expression into sub-expressions, build the sub-expressions and then combine them to get the complicated expression.

The basics of typesetting math in ConTeXt is explained here.

Display math mode

Type \startformula to get display math mode, or \dm to get a inline typesetting but keeping display sizing and rules.

Inline math mode

There are four equivalent commands to get inline math mode: \$, \m, \math, \mathematics to get display inline math mode.

For examples $x$ gives

while $2$ gives

Notice that the x is in italic while the 2 is upright. This is the usual mathematic convention.

Numbering Formulae \startformula

ConTeXt provides an easy way to number the display maths equations. Simply, insert the \startformula in the \startplaceformula environment, and you will get numbered equations.

The \startplaceformula command is optional, and produces the equation number; leaving it off produces an unnumbered equation.

Referencing formulae \in and \at

Equations can be referred to by simply adding a label to \startplaceformula and using command for referencing :

By default, only the formula number appears as a reference. This can be changed by using \definereferenceformat. For example, to create a command \eqref which shows the formula number in brackets, use

Sub-Formulae with \startsubformulas and \startalign

To use subformula numbering, you have to use \startsubformulas/\stopsubformulas and \startalign. For example

Combination of formulae with \startformulas

To use subformula numbering, you have to use \startsubformulas/\stopsubformulas and \startalign. For example

List of Formulae with \placelist

Since MKIV there are two types of formulas: numbered and named. Numbered formulas have the number as a reference and caption next to the formula and are listed only with the number and pagenumber in the list of formulas. Named formulas use the name as the caption instead of a number and are referenced with the name and pagenumber in the list of formulas.

\startplaceformula comes with the following key options: bookmark, list, reference, suffix, title. The title option changes the formula to a named formula (no number).

Formating with \setupformulas

Format of numbers

You can use \setupformulas to change the format of numbers. For example to get bold numbers inside square brackets use

To get equations also numbered by section, use the command \setupnumbering \setupnumbering[way=bysection] in the preamble of your document. This means that the section number preceeds the formula number. Add also \setupnumber \setupnumber[formula][way=bysection] to reset with each new section.

To get alphabets instead of numbers, use again \setupformulas

Formula alignment

Normally a formula is centered, but in case you want to align it left or right, you can set up formulas to behave that way. Normally a formula will adapt its left indentation to the environment:


In the next examples we explicitly align formulas with \setupformulas, to the left (raggedleft), center (middle, and right (raggedright):

With formula numbers the code is:

When tracing is turned on (\showboxes) you can visualize the bounding box of the formula,

As you can see, the dimensions are the natural ones, but if needed you can force a normalized line:

You can influence the placement of the whole box with the parameters leftmargin and rightmargin.

You can also inherit the margin from the environment.

The distance between the formula and the number is only applied when the formula is left or right aligned.

Shaded background for part of a displayed equation

(see also Frames)

One may define a specific framed command, for example coloredmath, with \definemathframed.

Formatting numbers and units

Formatting numbers uses \digits, \setdigitmode and \setdigitorder.

See

Notes

Evaluating expressions in ConTeXt

See also Evaluation of expressions (i.e. doing math).

Note to Plain TeX Users

ConTeXt is plain TeX compatible. So, if you have any old document written in plain TeX, it will work with ConTeXt. This does not mean that you will get pixel by pixel identical output with ConTeXt. For inline math, everything that you learnt for plain TeX is also true for ConTeXt. However, display math is significantly different. Do not use $$ .... $$ to write display math formulas in ConTeXt, since you will not get the correct spacing around the formulas. Instead use \startformula and \stopformula.

Note to AMSTeX/LaTeX Users

ConTeXt offers almost all the features that are present in AMSTeX and LaTeX. However, ConTeXt syntax is different. See this My Way for how to 'translate' from amsmath syntax to ConTeXt syntax. LaTeX Math in ConTeXt gives some brief ideas on how to get the LaTeX syntax to run in ConTeXt.

Other Methods

To do