Substituting variables into displayed maths


This page is about substituting variables into mathematical expressions. You can substitute text strings into plain text using curly braces; see Substituting variables into content areas for a description of the different methods of substituting variables into question text.

In Numbas, maths is displayed using LaTeX. For help with LaTeX, see LaTeX notation.

LaTeX is purely a typesetting language and is ill-suited for representing meaning in addition to layout. For this reason, dynamic or randomised maths expressions must be written in JME syntax and converted to LaTeX. Numbas provides two new LaTeX commands to do this for you.

To substitute the result of an expression into a LaTeX expression, use the \var command. Its parameter is a JME expression, which is evaluated and then converted to LaTeX.

For example:

\[ \var{2^3} \]


\[ 8 \]

and if a variable called x has been defined to have the value 3:

\[ 2^{\var{x}} \]


\[ 2^{3} \]

This simple substitution doesn’t always produce attractive results, for example when substituted variables might have negative values. If \(y=-4\):

\[ \var{x} + \var{y} \]


\[ 3 + -4 \]

To deal with this, and other more complicated substitutions, there is the \simplify command.

The main parameter of the \simplify command is a JME expression. It is not evaluated - it is converted into LaTeX as it stands. For example:

\[ \simplify{ x + (-1/y) } \]


\[ x - \frac{1}{y} \]

Variables can be substituted in by enclosing them in curly braces. For example:

\[ \simplify{ {x} / {y} } \]

produces, when \(x=2,y=3\):

\[ \frac{ 2 }{ 3 } \]

The \simplify command automatically rearranges expressions, according to a set of simplification rules, to make them look more natural. Sometimes you might not want this to happen, for example while writing out the steps in a worked solution.

The set of rules to be used is defined in a list enclosed in square brackets before the main argument of the \simplify command. You can control the \simplify command’s behaviour by switching rules on or off.

For example, in:

\[ \simplify{ 1*x } \]

I have not given a list of rules to use, so they are all switched on. The unitFactor rule cancels the redundant factor of 1 to produce:

\[ x \]

while in:

\[ \simplify[!unitFactor]{ 1*x } \]

I have turned off the unitFactor rule, leaving the expression as it was:

\[ 1 x \]

When a list of rules is given, the list is processed from left to right. Initially, no rules are switched on. When a rule’s name is read, that rule is switched on, or if it has an exclamation mark in front of it, that rule is switched off.

Sets of rules can be given names in the question’s Rulesets section, so they can be turned on or off in one go.

Display options

The \simplify and \var commands take an optional list of settings, separated by commas. These affect how certain elements, such as numbers or vectors, are displayed.

The following display options are available:


This rule doesn’t rewrite expressions, but tells the maths renderer that you’d like non-integer numbers to be displayed as fractions instead of decimals.

Example: \var[fractionNumbers]{0.5} produces \(\frac{1}{2}\).

This rule doesn’t rewrite expressions, but tells the maths renderer that you’d like vectors to be rendered as rows instead of columns.

Simplification rules

As well as the display options, the \simplify command takes an optional list of names of simplification rules to use, separated by commas. These rules affect how the command rearranges the expression you give it.

Lists of simplification rule names are read from left to right, and rules are added or removed from the set in use as their names are read. To include a rule, use its name, e.g. unitfactor. To exclude a rule, put an exclamation mark in front of its name, e.g. !unitfactor.

Rule names are not case-sensitive: any mix of lower-case or upper-case works.

To turn all built-in rules on, use the name all. To turn all built-in rules off, use !all.

If you don’t give a list of options, e.g. \simplify{ ... }, all the built-in rules are used. If you give an empty list of options, e.g. \simplify[]{ ... }, no rules are used.

For example, the following code:

\simplify[all,!collectNumbers,fractionNumbers]{ 0.5*x + 1*x^2 - 2 - 3 }

turns on every rule, but then turns off the collectNumbers rule, so every rule except collectNumbers can be applied. Additionally, the display option fractionNumbers is turned on, so the 0.5 is displayed as \(\frac{1}{2}\).

Altogether, this produces the following rendering: \(\frac{1}{2} x + x^2 - 2 - 3\)

The following simplification rules are available:


These rules are always turned on, even if you give an empty list of rules. They must be actively turned off, by including !basic in the list of rules. See this behaviour in action.

  • +xx (get rid of unary plus)
  • x+(-y)x-y (plus minus = minus)
  • x-(-y)x+y (minus minus = plus)
  • -(-x)x (unary minus minus = plus)
  • -xeval(-x) (if unary minus on a complex number with negative real part, rewrite as a complex number with positive real part)
  • x+yeval(x+y) (always collect imaginary parts together into one number)
  • -x+y-eval(x-y) (similarly, for negative numbers)
  • (-x)/y-(x/y) (take negation to left of fraction)
  • x/(-y)-(x/y)
  • (-x)*y-(x*y) (take negation to left of multiplication)
  • x*(-y)-(x*y)
  • x+(y+z)(x+y)+z (make sure sums calculated left-to-right)
  • x-(y+z)(x-y)-z
  • x+(y-z) ``(x+y)-z'
  • x-(y-z) > (x-y)+z
  • (x*y)*zx*(y*z) (make sure multiplications go right-to-left)
  • n*ieval(n*i) (always collect multiplication by \(i\))
  • i*neval(n*i)

Cancel products of 1

  • 1*xx
  • x*1x

Cancel exponents of 1

  • x^1x

Cancel fractions with denominator 1

  • x/1x

Cancel products of zero to zero

  • x*00
  • 0*x0
  • 0/x0

Omit zero terms

  • 0+xx
  • x+0x
  • x-0x
  • 0-x-x

Cancel exponents of 0

  • x^01

Rearrange expressions so they don’t start with a unary minus

  • -x+yy-x
  • -00

Collect together numerical (as opposed to variable) products and sums. The rules below are only applied if n and m are numbers.

  • -x-y-(x+y) (collect minuses)
  • n+meval(n+m) (add numbers)
  • n-meval(n-m) (subtract numbers)
  • n+xx+n (numbers go to the end of expressions)
  • (x+n)+mx+eval(n+m) (collect number sums)
  • (x-n)+mx+eval(m-n)
  • (x+n)-mx+eval(n-m)
  • (x-n)-m)x-eval(n+m)
  • (x+n)+y(x+y)+n (numbers go to the end of expressions)
  • (x+n)-y(x-y)+n
  • (x-n)+y(x+y)-n
  • (x-n)-y(x-y)-n)
  • n*meval(n*m) (multiply numbers)
  • x*nn*x (numbers go to left hand side of multiplication, unless \(n=i\))
  • m*(n*x)eval(n*m)*x

Cancel fractions to lowest form. The rules below are only applied if n and m are numbers and \(gcd(n,m) > 1\).

  • n/meval(n/gcd(n,m))/eval(m/gcd(n,m)) (cancel simple fractions)
  • (n*x)/m(eval(n/gcd(n,m))*x)/eval(m/gcd(n,m)) (cancel algebraic fractions)
  • n/(m*x)eval(n/gcd(n,m))/(eval(m/gcd(n,m))*x)
  • (n*x)/(m*y)(eval(n/gcd(n,m))*x)/(eval(m/gcd(n,m))*y)

Cancel any power of zero

  • 0^x0

Numbers go to the left of multiplications

  • x*nn*x
  • x*(n*y)n*(x*y)

Collect products of square roots

  • sqrt(x)*sqrt(y)sqrt(x*y)

Collect fractions of square roots

  • sqrt(x)/sqrt(y)sqrt(x/y)

Cancel square roots of squares, and squares of square roots

  • sqrt(x^2)x
  • sqrt(x)^2x
  • sqrt(n)eval(sqrt(n)) (if n is a square number)

Simplify some trigonometric identities

  • sin(n)eval(sin(n)) (if n is a multiple of \(\frac{\pi}{2}\))
  • cos(n)eval(cos(n)) (if n is a multiple of \(\frac{\pi}{2}\))
  • tan(n)0 (if n is a multiple of \(\pi\))
  • cosh(0)1
  • sinh(0)0
  • tanh(0)0

Evaluate powers of numbers. This rule is only applied if n and m are numbers.

  • n^meval(n^m)

Display-only JME functions

There are a few “virtual” JME functions which can not be evaluated, but allow you to express certain constructs for the purposes of display, while interacting properly with the simplification rules.

int(expression, variable)

An indefinite integration, with respect to the given variable.

  • int(x^2+2,x)\(\displaystyle{\int \! x^2+2 \, \mathrm{d}x}\)
  • int(cos(u),u)\(\displaystyle{\int \! \cos(u) \, \mathrm{d}u}\)
defint(expression, variable, lower bound, upper bound)

A definite integration between the two given bounds.

  • defint(x^2+2,x,0,1)\(\displaystyle{\int_{0}^{1} \! x^2+2 \, \mathrm{d}x}\)
  • defint(cos(u),u,x,x+1)\(\displaystyle{\int_{x}^{x+1} \! \cos(u) \, \mathrm{d}u}\)
diff(expression, variable, n)

\(n\)-th derivative of expression with respect to the given variable

  • diff(y,x,1)\(\frac{\mathrm{d}y}{\mathrm{d}x}\)
  • diff(x^2+2,x,1)\(\frac{\mathrm{d}}{\mathrm{d}x} \left (x^2+2 \right )\)
  • diff(y,x,2)\(\frac{\mathrm{d}^{2}y}{\mathrm{d}x^{2}}\)
partialdiff(expression, variable, n)

\(n\)-th partial derivative of expression with respect to the given variable

  • partialdiff(y,x,1)\(\frac{\partial y}{\partial x}\)
  • partialdiff(x^2+2,x,1)\(\frac{\partial }{\partial x} \left (x^2+2 \right )\)
  • partialdiff(y,x,2)\(\frac{\partial ^{2}y}{\partial x^{2}}\)
sub(expression, index)

Add a subscript to a variable name. Note that variable names with constant subscripts are already rendered properly – see Variable names – but this function allows you to use an arbitray index, or a more complicated expression.

  • sub(x,1)\(x_{1}\)
  • sub(x,n+2)\(x_{n+2}\)

The reason this function exists is to allow you to randomise the subscript. For example, if the index to be used in the subscript is held in the variable n, then this:

\simplify{ sub(x,{n}) }

will be rendered as


when n = 1.

sup(expression, index)

Add a superscript to a variable name. Note that the simplification rules to do with powers won’t be applied to this function, since it represents a generic superscript notation, rather than the operation of raising to a power.

  • sup(x,1)\(x^{1}\)
  • sup(x,n+2)\(x^{n+2}\)