R Markdown equation preview for \newcommand - r

I have an R markdown file with several custom latex commands, defined via \newcommand. I really like the preview in RStudio, but it doesn't seem to be able to render these commands or those imported from packages. In the following example, neither the custom command \expect nor the package command \bm is shown in the preview. Instead, they are shown in red letters in the preview.
Is there any way to have them rendered in the preview?
---
header-includes:
- \usepackage{bm}
output:
pdf_document
---
\newcommand{\expect}[1]{\mathbb{E}\left[ #1 \right]}
$$
\expect{\bm{X}} = \lambda
$$

Related

Rmarkdown beamer_presentation: "missing \item" LaTex error if an R chunk is included and fancybox used

I want to use the LaTeX \ovalbox{} command from the fancybox package in an Rmarkdown beamer_presentation, knitted from within RStudio. However, as soon as I add a simple R chunk, I get an "! LaTeX Error: Something's wrong--perhaps a missing \item."
This is a minimum reproducible example:
---
output:
beamer_presentation
header-includes: |
\usepackage{fancybox}
---
## Slide 1
\ovalbox{an oval box}
```{r}
print("x")
```
I can hand-solve the problem afterwards in the .tex file by moving the \usepackage{fancybox} command before the \usepackage{fancyvrb} command which is automatically inserted during the knitting.
Any suggestions how to avoid this problem in the first place?
Seems the packages are fighting over how to mess with verbatim environments.
Normally one could simply load them in a different order, but of course rmarkdown makes such a simple task a lot more complicate. You can make your document compile using this hack (don't try to use any of the verbatim commands from fancybox, this might explode...):
---
output:
beamer_presentation
header-includes: |
\let\VerbatimEnvironmentsave\VerbatimEnvironment
\usepackage{fancybox}
\let\VerbatimEnvironment\VerbatimEnvironmentsave
---
## Slide 1
\ovalbox{an oval box}
```{r}
print("x")
```

Tables missing when I convert Rmarkdown document with latex tables to docx

I have a document that contains a lot of LaTeX tables. I am trying to convert it to DocX so that I can share it with others. When I convert the document to DocX, the tables are missing. Has anyone been successful in being able to do this?
Here is an example to reproduce my issue.
---
title: "R Notebook"
output:
word_document: default
html_notebook: default
header-includes:
- usepackage{longtable}
---
This is an [R Markdown](http://rmarkdown.rstudio.com) Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing this chunk by clicking the *Run* button within the chunk or by placing your cursor inside it and pressing *Cmd+Shift+Enter*.
```{r}
plot(cars)
```
Add a new chunk by clicking the *Insert Chunk* button on the toolbar or by pressing *Cmd+Option+I*.
When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the *Preview* button or press *Cmd+Shift+K* to preview the HTML file).
The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike *Knit*, *Preview* does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.
The table.Rmd document contains the following:
---
title: "R Notebook"
header-includes:
- usepackage{longtable}
---
\begin{longtable}{ p{12cm} r }
Statement & XXX \\
\hline
line 1 & 100\% \\
line 2 & 100\% \\
line 3 & 88\% \\
\end{longtable}
I am not a Latex user. But I think you may have a very small, but important syntax error in your YAML header, you have:
---
title: "R Notebook"
header-includes:
- usepackage{longtable}
---
But the Rmarkdown Cookbook shows latex header-includes: as having TWO spaces indentation, which I think is important in terms of YAML, not sure if that is the root cause of your latex tables not rendering, but something I noticed.
---
title: Adding a Logo to LaTeX Title
header-includes:
- \usepackage{titling}
---
However, I may be able to offer an alternative solution. Rmarkdown plays very nicely with Tables and HTML and allows for dynamic output. using the YAML option always_allow_html: true allows for a nice HTML type table rendered inside your Word document that looks very professional and aesthetically appealing, with only 3 lines of code. Hopefully it wouldn't be too much work to convert your Latex tables to. Although this may not be an exact answer for you, it is another way to generate tables in Rmarkdown into Word .Docx files.
---
title: "R notebook"
author: John Doe
date: March 22, 2005
output: word_document
always_allow_html: true
---
```{r echo=FALSE}
library(kableExtra)
mtcars %>%
knit_print()
```

Is $$ deprecated in R Markdown LaTeX?

I was reading here that $$ is deprecated in LaTeX and replaced with \[ and \]. It seems the opposite when I use R Markdown in R Studio though.
If I wrap an equation in $$ it will display block style, live preview, in my R Markdown (in R Studio). If I use \[ and \] it will still knitr fine, but it won't showup R Studio live preview. See below.
---
title: "Untitled"
author: "March 2019"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
\[x = R + F\]
$$x = R + E$$
The linked Q&A is correct. $$ is deprecated for LaTeX. However, you are not writing LaTeX but Rmarkdown, which is processed by pandoc. In the pandoc manual we find:
TeX math will be printed in all output formats. How it is rendered depends on the output format:
LaTeX
It will appear verbatim surrounded by \(...\) (for inline math) or \[...\] (for display math).
So you can use $$...$$ in Rmarkdown documents and still have the correct output when converting to PDF via LaTeX. The other form works due to another pandoc extension. If you need cross-references for equations, you should use bookdown, though.

Render latex text from within r function in rmarkdown?

I am working within RStudio 1.1.383 using rmarkdown. I am trying to render to Latex a string that is created within a knitr chunk so that it appears as latex code within the document.
So far I have experimented with the 'results=' options in the chunk header and find that results='asis' enables it to be rendered once the document is knitted, but I have not been able to find a way to enable the result to be rendered using the preview feature that allows you to run a single chunk and see the results within the .Rmd editor.
Any help on this matter would be appreciated.
A minimal example is included below that should be copied into a .Rmd file before rendering.
Thanks in advance,
Michael
---
title: "Minimal Example"
output: html_document
---
```{r}
str <- "$$\\alpha \\cdot \\beta = \\delta$$"
# this doesn't show in the preview 'run chunk feature'
cat(str, "\n")
# neither does this
writeLines(str)
```

Rmarkdown and rstudio - reveal js print pdf.css error

After I have created a revealjs-presentation in my Rstudio-project, i proceed to open it. This works fine except that I get an error in my browser (Chrome) that pdf.css doesn't exist. Which it doesn't, because the presentation is looking for it in a sub-folder in my project directory. Now, I can add pdf.css according to the path that fails and the presentation works...until I re-run it and the added directory is deleted (I guess the knit process delete the aformentioned sub-folder?)
This is a minor problem but there seems to be something buggy going on here, anyone got any idea what I could do (besides re-installing everything)?
Example at the bottom
---
title: "Untitled"
output: revealjs::revealjs_presentation
---
## R Markdown
This is an R Markdown presentation. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document.
## Slide with Bullets
- Bullet 1
- Bullet 2
- Bullet 3
## Slide with R Code and Output
```{r}
summary(cars)
```
## Slide with Plot
```{r, echo=FALSE}
plot(cars)
```
And this is the folder-tree that's missing (relpath from a given Rstudio project):
Failed to load resource: net::ERR_FILE_NOT_FOUND
/TestRevealJs_files/reveal.js-3.3.0/css/print/paper.css
Where TestRevealJs is the name of the project.

Resources