20

I'm having some difficulty left aligning equations in R Markdown (i.e. putting the equation on the far left side of page, and aligning subsequent lines). I've generally determined that I want to set the [fleqn] option in the amsmath package to left align all equations, but putting the following in my YAML header gives an error

'Option clash for package amsmath'

---
author: "ME"
date: "February 26, 2015"
header-includes:
  - \usepackage[fleqn]{amsmath}
output: pdf_document
---

A section from my document:

$$
\begin{aligned}
Bias(\hat{\theta})  &= E(\hat{\theta}) - \theta \\
                    &= E(2 \bar{X} -1) - \theta \\
                    &= \frac{2}{n}\sum_{i=1}^n E(X_i) -1 -\theta \\
                    &= 2E(X) - 1 - \theta \\
                    &= 2 \cdot \frac{\theta+1}{2} - 1 - \theta \\
                    &= 0 \\
\end{aligned}
$$

Thanks

Steve Reno
  • 1,304
  • 3
  • 14
  • 21
  • Looks like this was already asked here http://stackoverflow.com/questions/25802590/stargazer-left-align-latex-table-columns – Danny M. Feb 26 '15 at 17:48
  • 2
    Not `$` nor `$$`, use `\[`...`\]` instead. See [Why is `\[` … `\]` preferable to `$$`?](http://goo.gl/GvmWy) – Werner Feb 26 '15 at 18:43
  • 1
    Using $ instead of $$ will change some formulas : summation indices will be put right to the summation sign instead of upper and below. So you do this change at the price of "degrading" (in a very strict point of view, that's true) your document. – Marc Le Bihan Aug 26 '19 at 05:30

3 Answers3

21

Try using $...$ instead of $$...$$.

In the case of $$...$$ in Rmarkdown is center aligned by default

Lovetoken
  • 438
  • 4
  • 11
5

This question has already been asked here: R Markdown Math Equation Allignment

The below works for me:

$\begin{aligned}
Bias(\hat{\theta})  &= E(\hat{\theta}) - \theta \\
                    &= E(2 \bar{X} -1) - \theta \\
                    &= \frac{2}{n}\sum_{i=1}^n E(X_i) -1 -\theta \\
                    &= 2E(X) - 1 - \theta \\
                    &= 2 \cdot \frac{\theta+1}{2} - 1 - \theta \\
                    &= 0 \\
\end{aligned}$
stemgal
  • 83
  • 1
  • 1
  • 2
    I believe that post only addresses how to align elements of the equation. I believe here they're asking how to align equations to the left of the page. Your code aligns the = symbol for each line, but it will still be centered on the page by default. – Will C Jun 10 '19 at 17:01
  • Your answer nor the answers in your link do not help in left aligning equations in R Markdown. – gosuto Oct 15 '19 at 19:32
1
$ L(x_{1}, x_{2}, ..., x_{n}| \mu) =  \mu * (1-\mu) $

$log (\ L(x_{1}, x_{2}, ..., x_{n}| \mu)) = h * log(\mu) + (m-h) * log(1-\mu) $

$\frac{\partial  log(\ L(x_{1}, x_{2}, ..., x_{n}| \mu)}{\partial(\mu)}  = \frac{h}{\mu} + \frac{h-m}{1-\mu} $

#### We set this to zero to find the value that maximizes the likelihood 

$\frac{h}{\mu} + \frac{h-m}{1-\mu} = 0 $

$h = m * \mu $

$\mu = \frac{h}{m} $
Mona Jalal
  • 34,860
  • 64
  • 239
  • 408