2

In Rmarkdown, knitting to PDF, if I write an equation like

\begin{equation}
y = x^2
\end{equation}

then I get a numbered equation in the PDF output.

If I write an equation like

$$ y=x^3 $$

Then the equation isn't numbered. I like being able to control which equations are numbered and which aren't, so having two ways to do it, one of which produces numbering, is good. But, the equations that I write with \begin{}....\end{} don't get a rendered preview in the editor in Rstudio, while the equations written with $$...$$ do get rendered. Is there some way to get the numbering without losing the previewing?

Phil
  • 7,287
  • 3
  • 36
  • 66
Ben S.
  • 3,415
  • 7
  • 22
  • 43

1 Answers1

7

Pandoc automatically changes your $$ delimiters to LaTeX-style \[ and \], so if you redefine those, you can get numbered equations. For example:

---
output: pdf_document
---

\renewcommand{\[}{\begin{equation}}
\renewcommand{\]}{\end{equation}}

This equation is numbered:

$$
x = y^2
$$

This one isn't:

$$
x = y^3 \nonumber
$$

If you are using some template that doesn't do the conversion to \[ and \] you can enter those yourself, but RStudio does previewing differently with those, just showing popups, not a static preview.

Edited to add: If you want the default to remain unnumbered, I don't think there's an inverse to \nonumber already defined. You could probably work out a LaTeX macro to do it, or if there are only a few numbered equations, just do them manually using \tag{number}, e.g.

---
output: pdf_document
---

This equation is fixed as equation (2):

$$
x = y^2 \tag{2}
$$

This one isn't:

$$
x = y^3
$$

Maybe someone else will let you know how to do auto-numbering as well.

user2554330
  • 37,248
  • 4
  • 43
  • 90