Search code examples
rr-markdownknitrdocxbookdown

Why inclusion of external Rmd-file with latex equation environment leads to different DOCX output while using rmarkdown and bookdown?


Below is the simplest Rmd file which I'm knitting to MS Word document using latest RStudio:

---
output:
  word_document: default
  bookdown::word_document2: default
---

```{sh, echo=FALSE, comment=''}
cat ~/latex-test.Rmd
```

The contents of ~/latex-test.Rmd are as follows:

Auto-numbered equation:

$$ f(x) = \sin(x) $$ 

---

LaTeX equation without label (using `\begin{equation}...\end{equation}`)

\begin{equation}
h(z) = \sinh(z)
\end{equation}

---

LaTeX equation with bookdown-supported label (using `\begin{equation}...(\#eq:label)\end{equation}`)

\begin{equation}
  f\left(k\right) = \binom{n}{k} p^k\left(1-p\right)^{n-k}
  (\#eq:binom)
\end{equation}

---

LaTeX equation with Xaringan-supported label (using `\begin{equation}...\label{label}\end{equation}`):

\begin{equation}
  g\left(k\right) = \binom{n}{k} p^k\left(1-p\right)^{n-k}
\label{binom2}
\end{equation}

Knitting to word_document: default gives exactly the same contents of docx file as contained in ~/latex-test.Rmd.
So I will not include it here to save the space.

But knitting to bookdown::word_document2: default gives extra $$...$$ which surround equation environments:

bookdown docx output

(I have highlighted the unexpected $$ manually)

Why this happening? What am I doing wrong? Is it a bug?


Notes:

  1. the outputs of html_document: default and bookdown::html_document2: default are equal and correct.
  2. the outputs of odt_document: default and bookdown::odt_document2: default are incorrect too.

Solution

  • This is a bug in bookdown that I just fixed on Github. You may try the development version of bookdown via remotes::install_github('rstudio/bookdown').