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
Mathematical formula We can use LaTeX to write mathematical equations in Markdown. To write inline LaTeX formula use a single $ before and after the equation and use a double $ to display equations.
You can now use LaTeX style syntax to render math expressions within Markdown inline (using $ delimiters) or in blocks (using $$ delimiters).
Math formulas are easy to write using Markdown, either using the inline mode or the displayed formulas mode. With the inline mode, formulas are inlined in the current paragraph whereas with the displayed mode, they appear as centered and emphasized.
Any markdown flavor is indeed a better option than LaTeX to focus on contents and format a document easily with a decent format, no doubt. But the paradise vanishes when you need more than standards sections and bulleted list. Simple formatting options such as centering an image or making two columns are not available.
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:
$$ ... $$
(or \[ ... \]
environments (unless you use a multi-line math environment, see below).cases
environment, use &
for horizontal alignment of different parts in every case\\
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 \\
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With