Equations in R Markdown
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.
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.
You can also use the native MathJax delimiters for inline and display equations. For example:
Note that when R Markdown processes
$latex style delimiters within a document they are written into the target HTML file using the native MathJax delimiters shown above.
MathML EquationsTo insert MathML equations, wrap your equation inside a standard
<math>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>−</mo> <mi>b</mi> <mo>±</mo> <msqrt> <mrow> <msup> <mi>b</mi> <mn>2</mn> </msup> <mo>−</mo> <mn>4</mn> <mi>a</mi> <mi>c</mi> </mrow> </msqrt> </mrow> <mrow> <mn>2</mn> <mi>a</mi> </mrow> </mfrac> </mrow> </math>