KnitR/Pitfalls
Learning about common pitfalls for advanced users provide insights in the structure of document generation. The provide solutions are templates for generic problem solving in document generation in KnitR. This section is not meant to be a debugging resource. So add KnitR problems with additional comments, so that learners understand more about the basic principles of dynamic document generation.
PDF Output
PDF output uses LateX to render the output:
Lower Symbol in LaTeX Expression in R-Markdown
- Date: 2019/07/05
- Topic: Math delimiters and Symbols
Document with Bug
The mathematical expression with a lower sign causes a problem in the PDF rendering of the following R-markdown source document:
---
title: "Document Test PDF output"
author: "Bert Niehaus"
date: "9 7 2019"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
The equation $y_{k} < \alpha$ is not rendered properly in R-Markdown with PDF output
Expected output
The latex expression should render to:
- The equation is not rendered properly in R-Markdown with PDF output
Error Message
! Missing $ inserted.
<inserted text>
$
l.92 The equation \$ y\_\{k\} \textless{} \alpha
Explanation: the lower symbol is replace by the textless command, which is OK when the less symbol is used in regular text mode. But if the replacement is performed in the math environment in R-Markdown, this replacement causes a problem for document rendering due to fact that the textless-command in LaTeX is meant to used in text mode and not in a math environment.
Solution
Do not wrap the inline math not with a single Dollar-character replace the math environment delimiters with \( math expression \) .
The equation \( y_{k} < \alpha \) is norw rendered properly in R-Markdown with PDF output
Lesson learnt
The general recommendation is to use the Dollar symbol "$" as an inline math delimiter in R-Markdown outside. Use \( as left inline math delimiter and \) as right delimiter. Wrap the mathematical expression with two blanks.