Equation trouble in Rmarkdown - r

Keep getting a message:
! Missing { inserted.
<to be read again>
\left
l.70 \end{frame}
when running the R markdown file below (in RStudio). I have spent a few hours pulling my hair out trying to figure out why? The equation renders in the preview.
---
title: "Compile Please"
output: beamer_presentation
---
## This Equation is Poison for Rmd
$$ f(x) = \left( 1 + \frac{x}{m - \alpha} \right) ^ \left( x \right) $$

It may be picky about spaces there. This "tighter" version with explicit parens to protect that exponentiated works here:
$$ f(x) = \left( 1 + \frac{x}{m - \alpha} \right)^{\left( x \right)} $$

Related

"missing { inserted" in latex equation mode

I am writing an equation in latex.
\begin{equation}
\label{eq:beta}
\beta_{ji}=softmax(w{_b^T}tanh(W_3v{_j^'}+W{_4q^'}))
\end{equation}
But the equation number is not displaying at the right side, also the latex giving me an error
missing { inserted
my equation is displayed here
what could be the possible reason
In LaTeX, you need to specify inside curly brackets separately what string is the subscript and what string is the superscript. Here there are three examples of how this influences the output result:
\begin{equation}
\label{eq:beta}
\beta_{ji} = softmax( w_{b}^{T} tanh (W_{3} v_{j}^{'} + W_{4q}^{'}) )
\end{equation}
\begin{equation}
\label{eq:beta}
\beta_{ji} = softmax( {w_{b}}^{T} tanh (W_{3} v_{j^{'}} + W_{4q^{'}}) )
\end{equation}
\begin{equation}
\label{eq:beta}
\beta_{ji} = softmax( {w^{T}}_{b} tanh (W_{3v_{j^{'}}} + {W^{'}}_{4q}) )
\end{equation}

Rmarkdown not building model equation to word

This is my code for linear model
model = lm(UCBRpk~ pksa + dist)
summary(model)
equatiomatic::extract_eq(model,wrap = TRUE)
equatiomatic::extract_eq(model, wrap = TRUE,use_coefs = TRUE)
I got the equation when knitting to html and pdf, but unable in word
This is the usual error message each time I knit to word:
unexpected "&"
expecting "%", "\\label", "\\nonumber" or whitespace
[WARNING] Could not convert TeX math '
\begin{aligned}
\operatorname{\widehat{SCBRcp}} &= 43.34 + 3.53(\operatorname{cpa}_{\operatorname{10\%}}) + 2.68(\operatorname{cpa}_{\operatorname{2\%}}) + 4.67(\operatorname{cpa}_{\operatorname{4\%}})\ + \\
&\quad 8.9(\operatorname{cpa}_{\operatorname{6\%}}) + 6.24(\operatorname{cpa}_{\operatorname{8\%}}) + 3.15(\operatorname{dist}_{\operatorname{A2}}) - 1.81(\operatorname{dist}_{\operatorname{A3}})
\end{aligned}
', rendering as TeX:
d}
tldr;
Seems like a bug with equatiomatic; I recommend opening an issue.
The issue is with the \operatorname calls in the LaTeX code produced by equatiomatic. I don't know whether the culprit is MathJax (since the expression itself is valid LaTeX) but a more minimal example reproducing your issue is as follows:
---
title: "Untitled"
output: word_document
---
$$
\begin{aligned}
\operatorname{\widehat{x}} &= a + b\ + \\
&\quad c + d
\end{aligned}
$$
which upon knitting produces the following warning
[WARNING] Could not convert TeX math '
\begin{aligned}
\operatorname{\widehat{x}} &= a + b\ + \\
&\quad c + d
\end{aligned}
', rendering as TeX:
ligned}
^
unexpected "&"
expecting "%", "\\label", "\\nonumber" or whitespace
I am not familiar with equatiomatic but in my (LaTeX) opinion, the translation of those equations into LaTeX should use \mathrm rather than \operatorname (see e.g. What's the difference between \mathrm and \operatorname? for a discussion and examples on how these two differ; amongst other things, \operatorname treats punctuation symbols like text whereas \mathrm treats them as math symbols. Perhaps -- and I'm 100% speculating here -- that's the reason why the equatiomatic devs decided to use \operatorname rather than \mathrm).
If I replace all \operatorname calls with \mathrm your example expression compiles fine.
---
title: "Untitled"
output: word_document
---
$$
\begin{aligned}
\mathrm{\widehat{SCBRcp}} &= 43.34 + 3.53(\mathrm{cpa}_{\mathrm{10\%}}) + 2.68(\mathrm{cpa}_{\mathrm{2\%}}) + 4.67(\mathrm{cpa}_{\mathrm{4\%}})\ + \\
&\quad 8.9(\mathrm{cpa}_{\mathrm{6\%}}) + 6.24(\mathrm{cpa}_{\mathrm{8\%}}) + 3.15(\mathrm{dist}_{\mathrm{A2}}) - 1.81(\mathrm{dist}_{\mathrm{A3}})
\end{aligned}
$$
So long story short, what can you do? Other than manually editing the output of equatiomatic I recommend opening an issue on the equatiomatic GH site, and cross-linking to this post on Stack Overflow.

Refer variable to LATEX function

Hi I am writing an R Markdown document in which there is a Latex function. I would like to refer the variable in the LATEX to the variable in the R code so that it automatically update the LATEX if the R code generates a new results for the variable.
Rcode:
var = rnorm(1,0)
Latex part:
$$ f(x) = beta_{0}*var $$
I have already looked these links but they doesn't work for me.
https://github.com/yihui/knitr/blob/master/inst/examples/knitr-minimal.Rmd;
Is there an R Markdown equivalent to \Sexpr{} in Sweave?; https://tex.stackexchange.com/questions/362119/passing-variable-from-r-to-latex
You can do
```{r, echo=FALSE, results='asis'}
var <- rnorm(1)
cat(sprintf("$$f(x) = \\beta_0*%s$$", var))
```
I believe this question has been answered in the post Is there an R Markdown equivalent to \Sexpr{} in Sweave? You can use inline R expressions `r `, e.g.
$$f(x) = \beta_{0} + `r var` * \beta_{1} + `r var2` * \beta_{2} + e$$
where var and var2, etc. are your variables in R.

Rmarkdown to Epub - how to get latex to show up?

I've been writing some documents with Knitr and Rmarkdown to .pdf, but it turns out I need to have then in .epub format instead.
I had been running a command like this to convert the .md files to .epub.
pandoc --mathjax -s --highlight-style tango file.md --to epub -o output.epub
That seems to have some issue handling latex code, though. Inline latex code with $x_1$, for example, seems to work fine. However, for latex blocks like:
$$
x_1 = 1, x_2 = 1 \\
h(x) = f(-20 + 15 + 17) \\
h(x) = f(12) \approx 1 \\
$$
It just displays the raw latex:
\[ x\_1 = 1, x\_2 = 1 \\\\ h(x) = f(-20 + 15 + 17) \\\\ h(x) = f(12) \\approx 1 \\\\ \]
Am I using latex for multi-line equations wrong in Rmarkdown? Is there a recommended way to get big chunks of latex working with Epub?
From Creating an ebook with pandoc
:
Pandoc has an EPUB3 writer. It renders LaTeX math into MathML, which EPUB3 readers are supposed to support (but unfortunately few do). Use pandoc -t epub3 to force EPUB3 output, as opposed to EPUB2 which is the default.
Of course, this isn't much help if you want EPUB2 output or target readers that don't support MathML. Then you should try using the --webtex option, which will use a web service to convert the TeX to an image.
Though I can't really reproduce your issue (at least the line breaks are discarded) -- eventually it's worth to use some more latex code for your equation:
\begin{equation}
\begin{aligned}
\label{eq7}
x_1 = 1, x_2 = 1 \\
h(x) = f(-20 + 15 + 17) \\
h(x) = f(12) \approx 1 \\
\end{aligned}
\end{equation}

adding equation to the R plot

I was wondering how I can paste the following equation as legend or text box to my plot in R (shown below in Latex format):
$$
x_i\stackrel{{\rm iid}}{\sim}{\rm Normal}\big(\mu-\frac \alpha 2,\sigma^2\big),\quad i = 1,\ldots, N_x \\
y_i\stackrel{{\rm iid}}{\sim}{\rm Normal}\big(\mu+\frac \alpha 2,\sigma^2\big),\quad i = 1,\ldots, N_y
$$
You can try the following
library(latex2exp)
plot(TeX("$x_i$ \t $\\overset{iid}{\\sim}$ \t $Normal$
$\\left(\\mu -\\frac{\\alpha}{2} ,\\sigma^2\\right)$,\t$i = 1,\\ldots, N_x$"))
to get the following

Resources