2

The file ./vignettes/foo.Rmd in an R package contains:

---
title: Foo
author: Marius Hofert
vignette: >
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteIndexEntry{Foo}
---
\[
\begin{align}
    X_t &= \mu_t + \sigma_t Z_t\\
  \mu_t &= \mu + \sum_{k=1}^{p_1} \phi_k  (X_{t-k}-\mu) +
            \sum_{k=1}^{q_1} \theta_k (X_{t-k}-\mu_{t-k})\sigma_t^2\\
        &= \alpha_0 + \sum_{k=1}^{p_2} \alpha_k (X_{t-k}-\mu_{t-k})^2 +
            \sum_{k=1}^{q_2} \beta_k \sigma_{t-k}^2.
            \end{align}
\]

However, this is the output:

this is the output

So the line breaks (via \\) seem to be ignored. Why?

Marius Hofert
  • 6,546
  • 10
  • 48
  • 102
  • I am not so familiar with rmarkdown. But your code would produce an error in Latex. `\begin{align} ... \end{align}` already starts math mode. Therefore `\[...\]` would not be necessary. Could that be the problem? – Alex Oct 18 '15 at 22:02

2 Answers2

1

In my R version it crashed when I tried to include amsmath. Somehow it seems to be already loaded.

As I already mentioned in the comment omitting \[ ... \] worked for the code below.

---
title: "Document title"
author: "Author's name"
output: pdf_document
---
\begin{align}
 X_t  &= \mu_t + \sigma_t Z_t\\
\mu_t &= \mu + \sum_{k=1}^{p_1} \phi_k  (X_{t-k}-\mu) +
         \sum_{k=1}^{q_1} \theta_k (X_{t-k}-\mu_{t-k})\sigma_t^2\\
      &= \alpha_0 + \sum_{k=1}^{p_2} \alpha_k (X_{t-k}-\mu_{t-k})^2 +
         \sum_{k=1}^{q_2} \beta_k \sigma_{t-k}^2.
\end{align}

enter image description here

Alex
  • 4,925
  • 2
  • 32
  • 48
  • See my update, that wasn't the problem. By the way, I don't have the `output: pdf_document` tag (I output to .html). Is that the reason? – Marius Hofert Oct 19 '15 at 02:22
  • No, at least not for me. I took your code, deleted `header-includes: - \usepackage{amsmath}` and it works fine. – Alex Oct 19 '15 at 06:54
  • The Error with the `\[...\]` only occurs when producing an PDF. Was that the first time in that document were you used the `align` enviornment? Did it work before? – Alex Oct 19 '15 at 07:03
  • If I remove `header-includes...`, I get the same result as in my update. Also, this is the first/only `align` in my `.Rmd`. When I simply copy your header to the file (with `output: pdf_document`), the vignette is not even built. I'm not sure if this is relevant, but (since I'm new to R markdown) this is my workflow: 1) work on the `foo.Rmd` 2) R CMD build package 3) Open the `.tar.gz` 4) open ./inst/doc/foo.html. – Marius Hofert Oct 19 '15 at 11:36
  • I updated the question with a full MWE. Note that on https://canvas.uw.edu/files/26927532/download?download_frd=1&verifier=qL2B2DJ1NYkuyRQPB60JdUCjFavHQ4iQ3gHbVME7 they also embed the `align` environment in `\[...\]`. – Marius Hofert Oct 19 '15 at 11:47
  • By the way, the same happens (no line break) if I use `$$` and `\begin{array}...\end{array}` – Marius Hofert Oct 19 '15 at 17:00
0

The following worked:

---
title: Foo
author: Marius Hofert
vignette: >
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteIndexEntry{Foo}
---
\[
\begin{align}
    X_t &= \mu_t + \sigma_t Z_t\\\\
  \mu_t &= \mu + \sum_{k=1}^{p_1} \phi_k  (X_{t-k}-\mu) +
            \sum_{k=1}^{q_1} \theta_k (X_{t-k}-\mu_{t-k})\sigma_t^2\\\\
        &= \alpha_0 + \sum_{k=1}^{p_2} \alpha_k (X_{t-k}-\mu_{t-k})^2 +
            \sum_{k=1}^{q_2} \beta_k \sigma_{t-k}^2.
            \end{align}
\]

What I found out (much) later is that I was missing the R package rmarkdown. With that, one indeed doesn't need to escape backslashes and only needs to provide \begin{align}..\end{align} (without \[...\])

Marius Hofert
  • 6,546
  • 10
  • 48
  • 102