39

I am writing some multi-line equations in R Markdown - LaTeX, using auto-numbering and \begin{align}. Here's a working the example:

---
title: "test"
output: html_document
---

(@eq01) $$
\begin{align}
y = x^2 \\
y = x^3 \\
y = \sqrt[2]{x}
\end{align}
$$

This works great when the output is html_document. Here's the result:

html_doc

But when I change the output document to pdf:

output: pdf_document

I get the following error (I am using RStudio latest Version 0.98.1056):

error

I've been trying to read the documentation as suggested in the error message, but I do not seem to get a handle on it. I've checked Stack Overflow and Google and although there are some related posts/questions (for example here, here, here), none of them solve the problem (or apply to my problem).

I've also tried to tweak everything. The most evident solution would be to get rid of the \begin{align} environment,

(@eq01) $$
y = x^2 \\
y = x^3 \\
y = \sqrt[2]{x}
$$

but it does not work for two reasons. First, the html version does not work as nicely because the auto-numbering does not appear centered in the multi-line equation, but rather on the first line (and I don't like it like that).

html output without the begin align

Second, although the pdf version in this case does compile and produce the pdf, it does not recognize that it is a multi-line equation (it's like it does not recognize the new line command \).

pdf

Any ideas are really appreciated. I've been struggling with this for a while and I cannot find a solution. I kinda love R Markdown because it really integrates the analysis with writing and communicating in a single tool (rather than using many different tools going back and forth). However, it seems there is still a long way to go before we can write one single source file and that it renders appropriately in several different output formats.

Community
  • 1
  • 1
Hernando Casas
  • 2,837
  • 4
  • 21
  • 30
  • 2
    Try and remove the outer `$$`...`$$`, since `align` initiates math-mode on its own. – Werner Sep 26 '14 at 01:58
  • 1
    Thx @Werner. I've tried using only LaTeX and it works, but for pdf output only and I was trying to get the same syntax to work alike in thml and pdf output. For example, this works for pdf output `\begin{equation} \begin{aligned} y = x^2 \\ y = x^3 \\ y = \sqrt[2]{x} \end{aligned} \end{equation} ` But it does not render the equations in html output. Also, the auto-numbering using only LaTeX does not get along with equation numbering using "(@eq) $$" – Hernando Casas Sep 26 '14 at 06:30
  • Again, `\begin{equation}`...`\end{equation}` starts math-mode, which is not what `align` wants. – Werner Sep 26 '14 at 13:46
  • using only \begin{aligned} throws an error: "! Package amsmath Error: \begin{aligned} allowed only in math mode." – Hernando Casas Sep 26 '14 at 13:56
  • 1
    You've changed it from `align` to `aligned` - there's a difference. – Werner Sep 26 '14 at 13:58
  • And using both `\begin{equation} \begin{aligned}` as is usually done in raw LaTeX using amsmath package, works good but only for PDF and not for html. What I am trying to find is a syntax in RMarkdown for multi-line equations that works ok for both, html and pdf. – Hernando Casas Sep 26 '14 at 14:01
  • you're right, in my original example I used align when I actually intended to use aligned, ..., sorry for the confusion, ..., anyway, I cannot find a syntax that works both for pdf and html output – Hernando Casas Sep 26 '14 at 14:07
  • I don't know RMarkdown, so I can't help with that (and therefore the HTML part). – Werner Sep 26 '14 at 14:12
  • 1
    `aligned` looks working well with both PDF and HTML. What is the problem? – kohske Oct 01 '14 at 06:37

1 Answers1

71

I was receiving the same error when trying to send an aligned block to PDF. Try changing the following:

$$
\begin{align}
y = x^2 \\
y = x^3 \\
y = \sqrt[2]{x}
\end{align}
$$

to the following:

$$
\begin{aligned}
y = x^2 \\
y = x^3 \\
y = \sqrt[2]{x}
\end{aligned}
$$

\begin{align} is a self-contained math environment, whereas \begin{aligned} needs to be placed inside an existing math environment. Since Rmd delineates math sections with $$...$$, it seems like \begin{align} was trying to start a second math environment within the first and causing problems.

Tyr Wiesner-Hanks
  • 1,343
  • 11
  • 10
  • 1
    Thanks from me too! :) – MissMonicaE Apr 24 '17 at 19:22
  • 1
    This worked for me too, but within a (Python) jupyter-notebook. The notebook is already in a math environment, so by doing `align` it gave me an error. I had to use `aligned`. My search brought me here which fixed the error so thank you! – rayryeng Nov 05 '17 at 17:42
  • 1
    The problem with this solution is that it doesn't seem to number the equations. – StatsStudent Sep 18 '20 at 16:21
  • 2
    Use `\begin{align}` directly without `$$...$$` to get the equation numbers. It might not render correctly on a preview window (e.g. VSCode), but `pandoc` recognizes it. – Fanchen Bao Mar 04 '22 at 22:39
  • Working on an RMarkdown PDF doc for the first time in years and got this error. DDGed the error message and got sent back to my own StackOverflow answer. Everything is a wheel within a wheel – Tyr Wiesner-Hanks Jun 14 '23 at 17:42