3

QUESTION:

In R Markdown, what is the right way to add a LaTeX align-like environment (with and without equation numbering) which will compile and display for both docx and pdf output?

DETAIL:

Option 1 below is what I'm going with. But I'd still like the option to have equation numbering and not give up that functionality when I move between docx and pdf output.

This compiles and displays in both docx and pdf output. Hooray! But what if I want equation numbering?

\[
\begin{aligned}
 AR(p): Y_i &= c + \epsilon_i + \phi_i Y_{i-1} \dots \\
 Y_{i} &= c + \phi_i Y_{i-1} \dots
\end{aligned}
\]

This will not compile for pdf or docx output.

\[
\begin{aligned*}
 AR(p): Y_i &= c + \epsilon_i + \phi_i Y_{i-1} \dots \\
 Y_{i} &= c + \phi_i Y_{i-1} \dots
\end{aligned*}
\]

These compile for both docx and pdf output. But these only display in pdf output.

\begin{align}
 AR(p): Y_i &= c + \epsilon_i + \phi_i Y_{i-1} \dots \\
 Y_{i} &= c + \phi_i Y_{i-1} \dots
\end{align}

\begin{align*}
 AR(p): Y_i &= c + \epsilon_i + \phi_i Y_{i-1} \dots \\
 Y_{i} &= c + \phi_i Y_{i-1} \dots
\end{align}

These will compile and display for docx output. But these will not even compile for pdf output.

\[
\begin{align}
 AR(p): Y_i &= c + \epsilon_i + \phi_i Y_{i-1} \dots \\
 Y_{i} &= c + \phi_i Y_{i-1} \dots
\end{align}
\]

\[
\begin{align*}
 AR(p): Y_i &= c + \epsilon_i + \phi_i Y_{i-1} \dots \\
 Y_{i} &= c + \phi_i Y_{i-1} \dots
\end{align*}
\]
lowndrul
  • 3,715
  • 7
  • 36
  • 54

1 Answers1

1

The following works for me:

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

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

\begin{equation*}
\begin{aligned}
 AR(p): Y_i &= c + \epsilon_i + \phi_i Y_{i-1} \dots \\
 Y_{i} &= c + \phi_i Y_{i-1} \dots
\end{aligned}
\end{equation*}

\begin{align*}
 AR(p): Y_i &= c + \epsilon_i + \phi_i Y_{i-1} \dots \\
 Y_{i} &= c + \phi_i Y_{i-1} \dots
\end{align*}

I am using the *-environments to get unnumbered equations in PDF. To get numbererd equations, you should use the environments without * and add labels.

Ralf Stubner
  • 26,263
  • 3
  • 40
  • 75
  • Adding a caveat to the answer. To have a multi-line environment with equation numbers on *some* of the lines, which works for all of pdf/docx/html output, you *must* have either a `\nonumber` or `(#eq:)` on *each* line. Via the *add labels* link in the answer, the environment which generates equation (2.2) will *not* work for docx output. Must use `\nonumber` in place of `\notag`. – lowndrul Jun 04 '19 at 14:36