KnitR/mathematical expressions

< KnitR

Introduction

Mathematical formulas/expressions can be display in R-Markdown

  • inline in the text by \( formula \) or $ formula $ or
  • as a centered equation surrounded double dollar signs $$ formula $$

using the LaTeX notation. The mathematical formulas are defined in the LaTeX notation.

Inline-Math - Mathematical Expression in the Text

To display a mathematical formula in the text (e.g. ), you can use the LaTeX code also used in Mediawiki in the R-Markdown document. The LaTeX notation of the mathematical expression is then surrounded by the opening delimiter \( and the closing delimiter \) . The expression is inserted in the R-Markdown text as follows:

The formula for the square root is \( \sqrt{x} \)

This text location, after execution of KnitR, is displayed in the PDF output as

The formula for the square root is

Block-Math - Centered Mathematical Formulas

To display a mathematical formula as a centered equation, the mathematical formula itself is defined in LaTeX notation. The delimiters for the centered mathematical formulas are the double dollar signs "$$". The following formula should be displayed in the R-Markdown document after knitr: In the KnitR document, the Block-Math formula is inserted as follows:

$$
  \frac{1}{1-q} = \sum_{n=0}^{\infty} q^n
$$

This is displayed in the PDF output as the above centered mathematical formula for the geometric series.

Alternative Inline-Math-Delimiter

The alternative inline-Math-Delimiter is the dollar sign $. This can also be used as an alternative to display mathematical formulas in the text.

The formula for the square root is $x^2$.

This is displayed in the PDF output as

"The formula for the square root is ""

Alternative Block-Math-Delimiter

The alternative block-Math-Delimiter can be used as opening and closing tags \[ formula \] . This can also be used as an alternative to display mathematical formulas in the text.

The formula for the geometric series is:
\[
  \frac{1}{1-q} = \sum_{n=0}^{\infty} q^n
\]

This is displayed in the PDF output as

"The formula for the geometric series is "

Here is the translation of the German text into English:

Hier ist der Text in englischer Sprache, mit der Mediawikisyntax:

Output of Calculated Matrices in the Text

The following section shows how to perform operations in R on matrices and vectors as column vectors and display the result as a matrix.

Calculated Matrices LaTeX Formulas

A matrix in R is a data structure. Mathematical expressions are defined in LaTeX in both Mediawiki and R-Markdown. The following function generates a matrix as a string (String) that can be used in KnitR in mathematical formulas and then contains the numerical value of the matrix calculated with R.

matrix2latex <- function(pA) {
  # Check if pA is a matrix
  if (!is.matrix(pA)) {
    stop("pA must be a matrix")
  }
  
  ### Close the pmatrix environment 
  latex_string <- "\\begin{pmatrix} \n"
  #### Add the rows of the matrix
  for (i in 1:nrow(pA)) {
    for (j in 1:ncol(pA)) {
      latex_string <- paste0(latex_string, " ", pA[i, j], " ")
      if (j < ncol(pA)) {
        latex_string <- paste0(latex_string, " & ")
      }
    }
    latex_string <- paste0(latex_string, "\\\\ \n")
  }
  
  ### Close the pmatrix environment 
  latex_string <- paste0(latex_string, "\\end{pmatrix}")
  
  return(latex_string)
}

Matrices as LaTeX Formulas

Now, we can test the output of the matrix in LaTeX. This is done in 3 steps:

  • Calculation of a matrix in R
### Calculation in a matrix in R

# Create a matrix
A <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, ncol = 3)
x <- matrix(c(-1, 1, -2), nrow = 3, ncol = 1)
y <- A %*% x

### Conversion to LaTeX strings
# Convert the matrix A to a LaTeX string
latex4A <- matrix2latex(A)
latex4x <- matrix2latex(x)
latex4y <- matrix2latex(y)

Output of Results in Formula

Now, the matrices and the result of a matrix multiplication defined in the code chunk are displayed in LaTeX. The syntax in R-Markdown is as follows. KnitR then replaces the variable with the calculated LaTeX output of the matrices and vectors.

$$
  A \cdot x = `r latex4A` \cdot `r latex4x` = `r latex4y` 
$$

Example Output of the Matrix

The matrix and the column vector were defined in the R code and the column vector was calculated with R using y <- A %*% x:

See also