R Markdown code chunk does not show math equations - r

Basically i want to build a random multiple choice question generator with R Markdown. For this task there need to be equations in the code chunks of the markdown.
The following works like a charm and gives the equation "greekbeta = 1"
---
title: "test"
author: "me"
output:
word_document: default
---
```{r eval=TRUE, echo=FALSE,results = "asis"}
"$\beta = 1$"
```
In contrast, this will not work when some other math symbol is used, for example:
---
title: "test"
author: "me"
output:
word_document: default
---
```{r eval=TRUE, echo=FALSE,results = "asis"}
"$\sum_{n=1}^{\infty}$"
```
After pressing knit, an error occurs (unfortunately the error message is in german, basically this: "'\s' is an unknown escape-sequence within the string starting with "$/s"").
I am very puzzled by this, especially because for example \frac{1}{2} works, but \hat{x} does not. Equations in the "normal" markdown text are no problem at all. But for my task, the equations have to be in the code chunk sections.
Does someone has a workaround for this problem? I tried using "$\hat{x}$" or even "$$\hat{x}$", but the error message is still the same.
I am using pandoc 2.11.4, R 4.1.0 and knitr 1.33

Use cat() and escape the escapes.
---
title: "test"
author: "me"
output:
word_document: default
---
```{r eval=TRUE, echo=FALSE,results = "asis"}
cat("$\\beta = 1$", '\n\n')
cat("$a^2+b^2 = c^2$", '\n\n')
cat("$\\sum_{n=1}^{\\infty}x_i$", '\n\n')
```

Related

Referencing to figures in RMarkdown does not work

First, I make two Waffleplots (I think I did it with ggplot)
(...)
waffle_school <- make_waffle(schoolsoort, StudentId)
(...)
waffle_bench <- make_waffle(schoolsoort, benchStudentId)
(...)
using some function:
make_waffle <- function(variableCol, uniqueIdentifiers, numberRowsWaffle = 5, deleteZeros=1) {
(...)
waffle <- waffle(categ_table, colors = Kleuren_schoolsoort[names(Kleuren_schoolsoort) %in% names(categ_table)], rows = numberRowsWaffle) +
theme(legend.position = "bottom") + colScale #+ guides(fill=guide_legend(ncol=4, byrow=TRUE))
return(waffle)
}
Then I actually show the plots at some point in the text
{r Waffleplot-school, fig.height = 2.5, fig.margin=TRUE, fig.cap="\\textcolor{TIGcaptioncolor}{Leerlingen per schoolsoort 21/22 op jullie school}"}
waffle_school
and
{r Waffleplot-landelijk, fig.height = 2.5, fig.margin=TRUE, fig.cap=cap1}
waffle_bench
}
which I later want to refer to in my text, by saying:
"In Figure \ref{fig:Waffleplot-school} you can see (..) and in Figure \ref{fig:Waffleplot-landelijk} you see (...)."
This leaves me with question marks in the actual reference in de pdf. By the way, I use pdflatex as a renderer.
Also, this is a part of the yaml header that my main Rmd file has, which I run from another R file.
---
output:
pdf_document:
number_sections: true
keep_tex: true
fig_caption: yes
latex_engine: pdflatex
extra_dependencies: ["flafter"]
then, I call the Rmd file with the pictures and text (incl. references) in it, which has the following header:
---
title: "SchoolInBeeld"
author: "Marja"
date: "`r Sys.Date()`"
bookdown::pdf_document2
---
I have seen the notation \#ref{}, which yielded the exact reference latex call instead of the reference itself: "\ref{fig:Waffleplot-school}". Also, I have visited many questions on this, like
Cross reference not working here, outputting in bookdown::pdf_document2 as suggested here, did not help. I changed it back to pdf_document.
cross reference in Rmd
I feel like the output format is the problem though. Something like me trying to use bookdown things but cannot figure out how to call that package.
FYI, I am fluent in Latex, but relatively new to R.
can you try this:
---
output:
bookdown::pdf_document2:
number_sections: true
keep_tex: true
fig_caption: yes
latex_engine: pdflatex
extra_dependencies: ["flafter"]

r - rmarkdown - yaml - Is it possible to include inline, internal document hyperlink in title?

is it possible to include an inline (internal document) hyperlink in the YAML title block of a RMarkdown document? Or is there a R package that allows this?
Below is what I am trying to do:
---
title: blah {#one1} and so on {#two2}
...
---
# Bibliography
1. Reference note to [explain](#one1) in title
2. Reference note to [explain](#two2) in title
Thank you.
The title is parsed as markdown, so you can use regular markdown link syntax in order to get the behavior you want.
---
title: 'There is a link in this title...[LINK](#anchor)'
author: "Jason Punyon"
date: "6/16/2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Bibliography
<p id='anchor'>Here's where I went!</p>
You could also just put the link in manually...
---
title: 'There is a link in this title...LINK'
author: "Jason Punyon"
date: "6/16/2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Bibliography
<p id='anchor'>Here's where I went!</p>

R markdown document display codes nicely

I am preparing R markdown document and I would like to display postgre sql codes nicely in R markdown document of course the codes should only be displayed not runned. How can I do that? . Below I put some code examples.
Thank you.
title: "POSTGRE sql "
output:
html_document: default
pdf_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r1, eval=F}
with data as (select * from (values
('03-05-2019'::date,'{"color": true,"view": [181] ,"school":
[805,812,852,856,857]}'::jsonb),
('06-08-2019'::date,'{"color": false,"view": [184,185],"school":
[805,855,859]}'::jsonb),
('04-07-2019'::date,'{"color": true,"view": [184,185,189],"school":
[855,859]}'::jsonb)
) as v(published_date,attributes))
```
In the header add this to your output
output:
html_document:
highlight: pygments
and in the body use this line
{r, engine = 'sql', eval = FALSE}
I am not sure the major differences in SQL and PostgreSQL, but the syntax highlighting does work.
Here is a link to show other languages you can use with pygments
http://pygments.org/languages/

R Studio Knitr PDF Markdown prints "asis"

I am generating a PDF document using Knitr. I am calling my r script from a separate file. Everything prints out fine in the PDF, but when I output a table using Stargazer, knitr generates a new page in the PDF document with "asis" printed on the page. I included a screen shot. Any ideas why this is happening?
My code chunk:
---
title: "PALS Biomarker Analysis"
author: "Daniel Parker"
date: "November 20, 2017"
output: pdf_document
header-includes:
- \usepackage{caption}
---
\captionsetup[table]{labelformat=empty}
```{r echo=FALSE}
library(knitr)
read_chunk('11-20-17 PALS Analysis V3.R')
```
```{r eighth, echo = FALSE, results="asis"}
<<linear_model_one>>
```
If I remove the results="asis" the table does not print correctly.
Try changing this code
```{r eighth, echo = FALSE, results="asis"}
<<linear_model_one>>
```
to this (remove the double quotes surrounding asis)
```{r eighth, echo = FALSE, results='asis'}
<<linear_model_one>>
```

Comment control from YAML in R Markdown

I'm trying to control a comment from my YAML header with a parameter, but I can't seem to make it work.
Here is an example of my code.
---
title: My report
date: "`r format(Sys.time(), '%d %B, %Y')`"
output:
html_document: default
pdf_document: default
word_document: default
params:
optional_text: "TRUE"
---
`r if(optional_text){"My text"}`
I have tried a few things (with/without quotation marks, etc.), but I always end up with the same error
Error in eval(parse_only(code[i]), envir = envir) :
object 'optional_text' not found
I have figured out a walkaround in the meanwhile, but it just doesn't seem efficient.
```{r label, include=FALSE}
optional_text<-TRUE
```
You just have to change one little thing:
`r if(params$optional_text){"My text"}`
In RMD the parameters are called with params$NAME_OF_PARAMETER, see here: http://rmarkdown.rstudio.com/developer_parameterized_reports.html

Resources