18

I have been working in R Markdown, which supports LaTeX, including in display mode. I had the following snippet for displaying aligned equations

$$
\begin{align}
a_1 &= \beta_0 \\
b_1 &= \beta_1 \\
c_1 &= \beta_2 \\
d_1 &= \beta_3
\end{align}
$$

This worked great for me when I chose to output as HTML. I believe this uses MathJax.

However, when I changed the output format to PDF, I get the following error message:

processing file: dummy.Rmd output file: dummy.knit.md

! Package amsmath Error: Erroneous nesting of equation structures; (amsmath) trying to recover with `aligned'.

See the amsmath package documentation for explanation. Type H for immediate help. ...

l.87 \end{align}

pandoc: Error producing PDF from TeX source Error: pandoc document conversion failed with error 43 Execution halted

When I change this to use aligned instead of align, it works with both.

This is an entire .Rmd file that shows the problem:

---
title: "dummy"
author: "Harold Ship"
date: "23 December 2015"
output: pdf_document
---


$$
\begin{align}
a_1 &= \beta_0 \\
b_1 &= \beta_1 \\
c_1 &= \beta_2 \\
d_1 &= \beta_3
\end{align}
$$

My question is, what is the difference between align and aligned, and why does align not work here with pandoc?

2
  • 6
    Welcome to TeX.SX! We have this question a lot of times. align is a full math environment, which must not be enclosed within (the deprecated!) $$ form or $. aligned on the other hand requires to appear in a math-mode enabling environment. I don't know why mathjax does allow this syntax, but pandoc uses LaTeX externally (as far as I know) and the error is reported.
    – user31729
    Commented Dec 24, 2015 at 7:29
  • 1
    What does the R Markdown code look like to achieve the LaTeX output you show?
    – Werner
    Commented Dec 24, 2015 at 7:50

2 Answers 2

22

So, just to be clear (as it says this is unanswered), just write the align (without $$):

\begin{align}
a_1 &= \beta_0 \\
b_1 &= \beta_1 \\
c_1 &= \beta_2 \\
d_1 &= \beta_3
\end{align}

and not

$$
\begin{align}
a_1 &= \beta_0 \\
b_1 &= \beta_1 \\
c_1 &= \beta_2 \\
d_1 &= \beta_3
\end{align}
$$
2
  • 2
    ok, but why does this happen? Commented Sep 30, 2020 at 0:51
  • 2
    Because \begin{align} puts you in math mode already, you don't need the $$ to switch to math mode
    – John M
    Commented Oct 29, 2020 at 16:57
1

I'm having the same issue. Yes, it should be

\begin{align}
\end{align}

from Latex point of view. But "$$" is needed in Markdown files by tools like Obsidian.

So far I haven't got any solution but fix it by some scripts before exporting PDF.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .