Consistent math equation numbering in bookdown across pdf, docx, html output

不羁的心 提交于 2019-12-11 05:39:30

问题


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.

DOCX: missing equation numbers.

HTML: missing equation numbers.

Notes:

  • The output was generated using pdf_book, word_document2, gitbook.
  • This is related to an unanswered question of mine: Align environment in R Markdown which works for both docx and pdf output?

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).

回答1:


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:

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.



来源:https://stackoverflow.com/questions/55923290/consistent-math-equation-numbering-in-bookdown-across-pdf-docx-html-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!