简体   繁体   中英

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:

这是输出

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

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}

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM