Inline code and inline LaTeX, R Markdown Pandoc er

2019-07-23 04:27发布

问题:

I've run into this issue a couple times now, but whenever I try to include both inline LaTeX and inline R code in my Rmd file, I get Pandoc error 43. Never been able to find a workaround other than just manually adding in what the R code would have been.

---
title: "HW4"
date: "2/23/2018"
header-includes:
  - \usepackage{amsmath}
  - \usepackage{amsthm}
output: 
  pdf_document
---

```{r}
x_bar <- 4
```

The blue line above is the true $\lambda = 4$, and the red line is our 
$\hat{\lambda}_{MLE} = \bar{x} = $ `r x_bar`.

Error:

l.192 \$ \hat{\lambda}

pandoc: Error producing PDF
Error: pandoc document conversion failed with error 43
Execution halted

回答1:

Move the inline R expression inside the $ symbols:

```{r}
x_bar <- 4
```

$\hat{\lambda}_{MLE} = \bar{x} = `r x_bar`$.

Rmd output:

Source: https://github.com/rstudio/rmarkdown/issues/385#issuecomment-77309571

Alternatively, leave the inline R code outside but delete the trailing space before the second $.