mathlete
mathlete

Reputation: 6682

Why does \\ not break lines in this R markdown example?

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?

Upvotes: 2

Views: 888

Answers (2)

mathlete
mathlete

Reputation: 6682

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 \[...\])

Upvotes: 0

Alex
Alex

Reputation: 4995

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

Upvotes: 1

Related Questions