I'm trying to create nicely formatted equations in RMarkdown, but I cannot seem to get it to Knit without errors. The LaTeX chunk I have looks like this:
---
title: "Untitled"
author: "KirkD-CO"
date: "September 18, 2019"
output: pdf_document
---
$$
f(x) = \beta_0 + \beta_1 x + \beta_2 x^2 + \beta_3 x^3 + \beta_4 (x-\xi)^3_+
\\
\begin{equation}
(x-\xi)^3_+ =
\begin{cases}
(x-\xi)^3 \ , \ x>\xi
\\
0 \ \ \ \ \ \ \ \ \ \ \ \ \ , \ x\leq\xi
\end{cases}
\end{equation}
\\
\
\\
\begin{aligned}
(x \leq \xi) \Rightarrow f(x) &= \beta_0 + \beta_1 x + \beta_2 x^2 + \beta_3 x^3
\\
(x > \xi) \Rightarrow f(x) &= \beta_0 + \beta_1 x + \beta_2 x^2 + \beta_3 x^3 + \beta_4(x^3 -3x^2\xi + 3x\xi^2 - \xi^3)
\\
&=(\beta_0 + \beta_4\xi^3) + (\beta_1 + 3\beta_4\xi^2)x + (\beta_2 - 3\beta_4)
\end{aligned}
$$
And in RStudio I see this:
But when I try to Knit it, I get this error:
! LaTeX Error: Bad math environment delimiter.
I've search Google and StackOverflow and found many similar problems with answers suggesting using [ ] instead of \begin{equation}, other suggest using only one $ instead of $$, and still others refer to a variety of LaTeX packages, a few of which I've tried and wound up in installation purgatory.
Any suggestion on a straight forward way to get Kniter to Knit a PDF with the output RStudio displays?
EDIT: I'm using R 3.5.3 and Kniter 1.22 on Fedora 30.
EDIT2: Under Tools -> Global Options -> Sweave -> Typeset LaTeX into PDF using: is set to pdfLaTeX
Following up from the comments, here is a cleaned version:
---
title: "Untitled"
author: "KirkD-CO"
date: "September 18, 2019"
output: pdf_document
---
$$
f(x) = \beta_0 + \beta_1 x + \beta_2 x^2 + \beta_3 x^3 + \beta_4 (x-\xi)^3_+
$$
$$
(x-\xi)^3_+ =
\begin{cases}
(x-\xi)^3\, ,& x>\xi \\
0 ,& x\leq\xi
\end{cases}
$$
$$
\begin{aligned}
(x \leq \xi) \Rightarrow f(x) &= \beta_0 + \beta_1 x + \beta_2 x^2 + \beta_3 x^3 \\
(x > \xi) \Rightarrow f(x) &= \beta_0 + \beta_1 x + \beta_2 x^2 + \beta_3 x^3 + \beta_4(x^3 -3x^2\xi + 3x\xi^2 - \xi^3) \\
&=(\beta_0 + \beta_4\xi^3) + (\beta_1 + 3\beta_4\xi^2)x + (\beta_2 - 3\beta_4)
\end{aligned}
$$
Producing
Some specific comments:
Put separate LaTeX math mode chunks into separate $$ ... $$ (or \[ ... \] environments (unless you use a multi-line math environment, see below).
Inside a cases environment, use & for horizontal alignment of different parts in every case
Don't use \\ unless you're using some multi-line math environment (like cases, aligned); conversely, if you do use a multi-line math environment, you must separate lines with \\.
Related
I have a following problem. I prepare a project in R and always get a problem that after knitting formulas are partially outside of pdf document.
Formula issue formatting description
There is a post with similar problem, but it didn't help (R Markdown Knitr to PDF Code Outside Margin)
I have also tried adding "\\", but it also didn't help.
Is there any easy solution about how to organize auto line breaks in formulas in Rmarkdown? I don't want to convert html to pdf since I would lose all internal references then.
I will be very grateful if someone would help!!!
Update:
Here is a sample formula for rmarkdown:
---
title: "Code Sample"
output:
pdf_document: default
html_document: default
---
1) $$ (\sum_{i = 1}^{N} \|x_i - \mu - V_q \lambda_i\|^2)'_\mu = \\
(\sum_{i = 1}^{N} (x_i - \mu - V_q \lambda_i)^T (x_i - \mu - V_q \lambda_i))'_\mu = \\
= \sum_{i = 1}^{N} ((x_i - \mu - V_q \lambda_i)^T (x_i - \mu - V_q \lambda_i))'_\mu = \\
= \sum_{i = 1}^{N} 2 (x_i - \mu - V_q \lambda_i) \cdot (-1) = \\
= \sum_{i = 1}^{N} -2 \cdot (x_i - \mu - V_q \lambda_i) \\
\text{since the degree was 2 and the sign of $\mu$ was '-' (minus)} $$
Welcome to SO, #Irina Mironova!
You could try this, using & to align and \\ to break lines:
---
title: "Aligned equations"
author: "bttomio"
date: "4/7/2021"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
\begingroup\small
\begin{equation}
\begin{aligned}
ER_t
= & \lambda_1 + \sum_{i=0}^k\alpha_{11i}ER_{t-1} + \sum_{j=k+1}^p\alpha_{12j}ER_{t-j} + \sum_{i=0}^k\beta_{11i}CT_{t-1} + \sum_{j=k+1}^p\beta_{12j}CT_{t-j} \\
+ & \sum_{i=0}^k\gamma_{11i}IRD_{t-1} + \sum_{j=k+1}^p\gamma_{12j}IRD_{t-j} + \sum_{i=0}^k\delta_{11i}VIX_{t-1} + \sum_{j=k+1}^p\delta_{12j}VIX_{t-j} \\
+ & \sum_{i=0}^k\phi_{11i}SM_{t-1} + \sum_{j=k+1}^p\phi_{12j}SM_{t-j} + \sum_{i=0}^k\psi_{11i}SMUS_{t-1} + \sum_{j=k+1}^p\psi_{12j}SMUS_{t-j} +\varepsilon_{1t}
\end{aligned}
\end{equation}
\endgroup
-output
UPDATE
Following the equation supplied by the OP, here is a working example:
---
title: "Code example"
author: "bttomio"
date: "4/9/2021"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
\begingroup
\begin{equation}
\begin{aligned}
(\sum_{i = 1}^{N} \|x_i - \mu - V_q \lambda_i\|^2)'_\mu = \\
(\sum_{i = 1}^{N} (x_i - \mu - V_q \lambda_i)^T (x_i - \mu - V_q \lambda_i))'_\mu = \\
\sum_{i = 1}^{N} ((x_i - \mu - V_q \lambda_i)^T (x_i - \mu - V_q \lambda_i))'_\mu = \\
\sum_{i = 1}^{N} 2 (x_i - \mu - V_q \lambda_i) \cdot (-1) = \\
\sum_{i = 1}^{N} -2 \cdot (x_i - \mu - V_q \lambda_i) \\
\text{since the degree was 2 and the sign of $\mu$ was '$-$' (minus)}
\end{aligned}
\end{equation}
\endgroup
-output
What happens when you use specific margins in PDF output?
---
title: "Habits"
author: John Doe
date: March 22, 2005
geometry: "left=3cm,right=3cm,top=2cm,bottom=2cm"
output: pdf_document
---
So I am trying to get Latex in my Jupyter Lab going.
If I insert this
\begin{eqnarray}
y & = & ax^2 + bx + c \\
f(x) & = & x^2 + 2xy + y^2
\end{eqnarray}
it gets rendered as expected
But
\begin{itemize}
\item First.
\item Second.
\end{itemize}
gets rendered as the text
I already installed #jupyterlab/latex extension
I have a latex equation as part of an RMarkdown document that I'm knitting to an HTML file using knitr. I would like to use the LaTeX eqnarray to align the equality signs. However, I would also like to change font sizes from the first equation to the next.
This works, but equality signs are not lined up:
---
title: "Test"
author: "Eric"
date: "5/14/2020"
output: html_document
---
\[
\large a = b \times (c + d)\\
{\small\begin{eqnarray}
a &=& \mathrm{var ~a}\\
b &=& \mathrm{var ~b}\\
c &=& \mathrm{var ~c}
\end{eqnarray}}
\]
The result:
I tried many different arrangements of brackets and terms. Here is an example that does not work:
---
title: "Test"
author: "Eric"
date: "5/14/2020"
output: html_document
---
#### Try to line up equal signs AND change font sizes (doesn't work)
\[
\begin{eqnarray}
\large a &=& b \times (c + d)\\
{\small
a &=& \mathrm{var ~a}\\
b &=& \mathrm{var ~b}\\
c &=& \mathrm{var ~c}}
\end{eqnarray}
\]
#### Try again (doesn't work):
\[
\begin{eqnarray}
{\large a &=& b \times (c + d)}
{\small
\\a &=& \mathrm{var ~a}\\
b &=& \mathrm{var ~b}\\
c &=& \mathrm{var ~c}}
\end{eqnarray}
\]
How do I line up the equal signs using eqnarray while changing font sizes?
This seems to be what you'd want:
\[
\small
\begin{eqnarray}
{\large a}\ & {\large =} & {\large b \times (c + d)}\\
a &=& \mathrm{var ~a}\\
b &=& \mathrm{var ~b}\\
c &=& \mathrm{var ~c}
\end{eqnarray}
\]
The spacing for the a is a little bit off, which is why I added the \ after it. You might want to fiddle with that.
There is the following piece of code on the official Sphinx documentation:
.. math::
:nowrap:
\begin{eqnarray}
y & = & ax^2 + bx + c \\
f(x) & = & x^2 + 2xy + y^2
\end{eqnarray}
I tried for a long time to get it to work, until I changed it to:
.. math::
:nowrap:
$$
\\begin{eqnarray}
y & = & ax^2 + bx + c \\\\
f(x) & = & x^2 + 2xy + y^2
\\end{eqnarray}
$$
How could it be that the official Sphinx documentation is wrong?
Or is something obscure happening here? Even Sagemath documentation seems to suggest that $$ need to be used (see "If you wish to explicitly not wrap the MATH block [...]")
Or is this a bug?
(I use Sphinx 2.2.0 and in my index.rst file I use autofunction for the function whose docstring contains the equation above.)
The Sphinx documentation states that when you put math markup in Python docstrings read by autodoc, you either have to double all backslashes, or use Python raw strings (r"raw"). That happens because Python will interpret the string before Sphinx has the opportunity to parse it.
So, if you want to insert math in a docstring, the following should work:
.. math::
:nowrap:
\\begin{eqnarray}
y & = & ax^2 + bx + c \\\\
f(x) & = & x^2 + 2xy + y^2
\\end{eqnarray}
You may simplify it further with:
.. math::
y & = ax^2 + bx + c \\\\
f(x) & = x^2 + 2xy + y^2
The :nowrap only hints Sphinx that it should not wrap the provided equations in a LaTeX "split" environment (See Section 3 of the amsmath Manual). Instead, you must supply your own as indicated in the first example of this post.
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
Try using $...$ instead of $$...$$.
In the case of $$...$$ in Rmarkdown is center aligned by default
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}$
$ 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} $