19
$$
r  = \frac{1}{n-1} \sum_{i=1}^{n} \frac{(X_i - \bar{X})(Y_i - \bar{Y})}{S_xS_y}
$$

The rendered equation is not numbered. Using \begin{aligned} and \end{aligned} generates an error.

I am also using knitr in RStudio, knitting to PDF using pandoc and xelatex engine.

Thank you.

Kranja
  • 331
  • 1
  • 2
  • 8

2 Answers2

16

As pointed out amsmath is already in the default template, so you can invoke the equation environment

\begin{equation}
y = mx+b
\end{equation}

This will automatically number equations in order of use. To use another character, say *,

\begin{equation}
\tag{*}
y = ax^2 + bx + c
\end{equation}

To leave equation unmarked simply leave tag{} blank.

For differences between align and equation see this answer

Tcfkaj
  • 317
  • 1
  • 9
  • 1
    The `\usepackage{amsmath}` header in the YAML is what I was missing. – Kranja Oct 23 '18 at 08:57
  • I added `\usepackage{amsmath}` header in the YAML and it worked whether it is included or not. My final code looks like this: ``` --- title: Test Doc header_includes: - \usepackage{amsmath} output: pdf_document --- \begin{align} r = \frac{1}{n-1} \sum_{i=1}^{n} \frac{(X_i - \bar{X})(Y_i - \bar{Y})}{S_xS_y} \end{align} ``` – Kranja Oct 23 '18 at 09:21
  • tarleb, thanks for the input. I was aware it was `header-includes` however with my every growing YAML for certain document types, changing from `header_includes` to `header-includes` broke the loading of other packages. Turns out this is because I do not need specify `\usepackage{amsmath}`. – Tcfkaj Oct 23 '18 at 15:11
  • So kranja, it is best not to specify `amsmath` and use `header-includes` not `header_includes` for loading other packages. – Tcfkaj Oct 23 '18 at 15:12
  • Thank you @jared-t This helps to clarify the sequencing - YAML and main body. – Kranja Oct 24 '18 at 16:44
11

Try align not aligned

\begin{align}
    r  = \frac{1}{n-1} \sum_{i=1}^{n} \frac{(X_i - \bar{X})(Y_i - \bar{Y})}{S_xS_y}
\end{align}
Charco Hui
  • 209
  • 3
  • 4
  • Thank you. It worked. I was including $$...$$ and that seems to have been breaking things. – Kranja Oct 23 '18 at 09:07
  • @Kranja Thanks for this comment. My difficulty was solved by omitting the `$$` and using `\begin{equation}...`. Evidently using both causes `! LaTeX Error: Bad math environment delimiter.` – flies Oct 13 '22 at 15:27