Home RStudio IDE Shiny Projects About Blog

# Equations in R Markdown

With R Markdown , you can embed LaTeX and MathML equations directly into your document. Equations are displayed using the MathJax JavaScript library. Note that this library is loaded from the MathJax website so readers of your document must be online to see the rendered equations.

## LaTeX Inline Equations

To include an inline LaTeX equation you enclose the equation in $delimiters, for example: In order to avoid conflicts with currency specifications, the following syntactic rules apply to the use of$ delimiters:

• The equation text must be directly attached to the $characters with no whitespace in between. • The closing$ must not be followed by a number, letter, or back-tick.
• The equation text can contain at most two line breaks.

To prevent a $from being treated as an equation delimiter you can escape it using a backslash (e.g. \$).

Inline equations can span multiple lines. Note however that within the RStudio editor you won't see syntax highlighting for inline equations that span across lines.

## LaTeX Display Equations

To include a LaTeX display equation you enclose the equation in $$delimiters, for example: Display equations can span an arbitrary number of lines and unlike inline equations do not have a requirement that the equation text be directly attached to the delimiters. Display equations are rendered as a centered block element within the generated web page. ## Alternative Syntax for LaTeX Equations The syntax described above for inline and display equations is based on conventions used for embedding equations in LaTeX documents. It's also compatible with the syntax used by org-mode and the pandoc markdown engine. However, depending on how you are publishing web content you may wish to use one the alternate syntaxes described below. ### WP LaTeX The WP LaTeX WordPress plugin supports a variation of the traditional  and$$ delimiters for embedding equations. For example:

You might choose to use this syntax if you intend to eventually publish your markdown into a WordPress blog.

### MathJax Native

You can also use the native MathJax delimiters for inline and display equations. For example:

Note that when R Markdown processes $ or $latex style delimiters within a document they are written into the target HTML file using the native MathJax delimiters shown above.

## MathML Equations

To insert MathML equations, wrap your equation inside a standard $ tag. For example, to insert the quadratic formula you would use: <math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> <mrow> <mi>x</mi> <mo>=</mo> <mfrac> <mrow> <mo>&#x2212;</mo> <mi>b</mi> <mo>&#xB1;</mo> <msqrt> <mrow> <msup> <mi>b</mi> <mn>2</mn> </msup> <mo>&#x2212;</mo> <mn>4</mn> <mi>a</mi> <mi>c</mi> </mrow> </msqrt> </mrow> <mrow> <mn>2</mn> <mi>a</mi> </mrow> </mfrac> </mrow>$