2

In bookdown, is there a LaTeX math environment which numbers each equation, regardless of whether the ouptut is .pdf, .docx, .html? Adding this LaTeX:

\begin{align}
   X &= Y \\
   Z &= W
\end{align}

Into the bookdown-demo outputs the following:

PDF: works as expected.

pdf

DOCX: missing equation numbers.

docx

HTML: missing equation numbers.

gitbook

Notes:

UPDATE: Merging Ralf's answer below along with other learnings of mine, in bookdown, the following all work consistently and as expected across .pdf, .docx, .html output.

Add a single un-numbered equation:

\begin{equation*}
  X = Y
\end{equation*}

Add a single numbered equation:

\begin{equation}
  X = Y
  (\#eq:eq02)
\end{equation}

I refer to previous, equation \@ref(eq:eq02).

Add multiple un-numbered equations:

\begin{align*}
  X &= Y \\
  Z &= W   
\end{align*}

Add multiple equations with numbering for each:

\begin{align}
  X &= Y (\#eq:eq05)\\
  Z &= W (\#eq:eq06)  
\end{align}

I refer to previous, equation \@ref(eq:eq05) and equation \@ref(eq:eq06).

Add multiple equations with a single numbering for all:

\begin{equation}
   \begin{aligned}
      X &= Y \\
      Z &= W   
   \end{aligned}
   (\#eq:eq04)
\end{equation}

I refer to previous, equation \@ref(eq:eq04).
lowndrul
  • 3,715
  • 7
  • 36
  • 54
  • [This looks promising](https://stackoverflow.com/q/47985267/903061). It doesn't provide details, but suggests that with `bookdown` HTML equation numbering can work as well. – Gregor Thomas Apr 30 '19 at 14:45
  • That might just have to be the hack if there's nothing else. But I'd prefer to number each equation within an environment like `align`, `split`, etc., rather than set up separate environments for each equation. – lowndrul Apr 30 '19 at 14:48
  • Is there a way of making this work in Rmarkdown documents that are not using bookdown? I tried the code above, and could not get it to knit to docx with numbered equations. – Jeremy K. Oct 06 '19 at 08:28

1 Answers1

5

For equation numbering support in bookdown you need to assign labels. the following works for me:

---
output:
  bookdown::html_document2: default
  bookdown::word_document2: default
  bookdown::pdf_document2: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

\begin{equation}
\begin{aligned}
  X &= Y \\
  Z &= W
\end{aligned}
(\#eq:eq1)
\end{equation}

\begin{align}
  X &= Y (\#eq:eq2) \\
  Z &= W (\#eq:eq3)
\end{align}


See Equation \@ref(eq:eq1) or Equations \@ref(eq:eq2) and \@ref(eq:eq3).

HTML Output:

enter image description here

PDF output is similar. Word output as seen in LibreOffice is pretty bad, but the equation numbers including references are there. So I guess this is a local or LibreOffice specific problem.

Community
  • 1
  • 1
Ralf Stubner
  • 26,263
  • 3
  • 40
  • 75