LaTeX {itemize} in R Markdown - r

How do I include the following LaTeX {itemize} in my R Markdown? It simply doesn't work when I try and knitr the HTML.
---
title: "Untitled"
author: "March 2019"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
\[x = R + E\]
where:
\begin{itemize}
\item[R=] is Racoon
\item[E=] is Elephant
\end{itemize}

Just use either + or - at the beginning of sentences which you wish to itemize, it works for both html and pdf output of Rmarkdown
eg
use the statement in between '$' symbol
$+ this statement will be itemized in rmd$

R Markdown doesn't use LaTeX when producing HTML. The code you used would work if output was to pdf_document, but not to html_document.
If you really want the labelled list in HTML, you're going to have to insert HTML code, not LaTeX code. I don't know if there's anything visually equivalent to LaTeX's \item[R=], but you could do this, which is logically equivalent:
---
title: "Untitled"
author: "March 2019"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
\[x = R + E\]
where:
<dl>
<dt>R=</dt>
<dd>is Racoon</dd>
<dt>E=</dt>
<dd>is Elephant</dd>
</dl>
This displays as
Perhaps CSS could be crafted to make it visually equivalent.
Edited to add: And of course it is possible, and has been done. It's easy to follow https://stackoverflow.com/a/13371522/2554330, since R Markdown uses the Bootstrap framework. Just add a class to the first tag:
<dl class="dl-horizontal">
<dt>R=</dt>
<dd>is Racoon</dd>
<dt>E=</dt>
<dd>is Elephant</dd>
</dl>
and it produces
If the styling is still not satisfactory, look at some of the other discussion related to https://stackoverflow.com/a/13371522/2554330.

Related

How can I automate different spacing between text and code blocks in R Markdown?

Consider the following R Markdown document:
---
title: "Stack Overflow Question"
author: "duckmayr"
date: "6/21/2019"
output: pdf_document
header-includes:
- \usepackage{setspace}
- \doublespacing
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Here is some example text.
I want all the body text to be double-spaced,
but I want all echoed code from code chunks to be single spaced.
In other words, not this:
```{r}
## This code is double-spaced.
## I want it to be single spaced.
## How can I do that?
```
Is there a canned or relatively painless way to have all normal text double-spaced, but have all code echoed from code chunks single spaced? I tried consulting the guide to chunk options here, but couldn't quite find what I was looking for.
If you are outputting to pdf the most painless way might be adding some LaTeX commands to your Rmd document:
---
title: "Stack Overflow Question"
author: "duckmayr"
date: "6/21/2019"
output: pdf_document
header-includes:
- \usepackage{setspace}
- \doublespacing
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Here is some example text. I want all the body text to be double-spaced, but I
want all echoed code from code chunks to be single spaced. In other words, not
this:
\singlespacing
```{r}
## This code is double-spaced.
## I want it to be single spaced.
## How can I do that?
```
\doublespacing
Some additional body text. Nor hence hoped her after other known defer his.
For county now sister engage had season better had waited. Occasional mrs
interested far expression acceptance. Day either mrs talent pulled men
rather regret admire but. Life ye sake it shed. Five lady he cold in meet up.
Alternatively, you could define a new chunk option using knitr chunk hooks. For instance, you could include in the setup chunk:
```{r setup, include=FALSE}
hook_chunk = knitr::knit_hooks$get('chunk')
knitr::knit_hooks$set(chunk = function(x, options) {
regular_output = hook_chunk(x, options)
# add latex commands if chunk option singlespacing is TRUE
if (isTRUE(options$singlespacing))
sprintf("\\singlespacing\n %s \n\\doublespacing", regular_output)
else
regular_output
})
knitr::opts_chunk$set(echo = TRUE, singlespacing = TRUE)
```
Some useful references:
Hooks - Customizable functions to run before/after a code chunk, tweak the output, and manipulate chunk options
How to Create New Chunk Options in R Markdown

How do I use themes for R Markdown with github_document as output?

I was told to write a report in R Markdown with the following template:
---
title: "Data Dictionary"
output: github_document
date: "Last Updated: `r format(Sys.time(), '%d, %B, %Y at %H:%M')`"
---
I want to use readthedown theme from rmdformats package, however I could not find a tutorial on how to do so properly. My question is, is it possible to use a theme with this template? Any help would be appreciated!
Assuming you already have installed the package, remotes::install_github("juba/rmdformats"), just pass rmdformats::readthedown to the output YAML:
---
title: "Untitled"
date: "Last Updated: `r format(Sys.time(), '%d, %B, %Y at %H:%M')`"
output: rmdformats::readthedown
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. 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. You can embed an R code chunk like this:
```{r cars}
summary(cars)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

R Markdown, numbering not showing for figure captions in HTML and Word

I am putting together an R Markdown document in HTML, and I have the following YAML:
---
title: "R Markdown Example"
author: "Me"
date: "October 30, 2017"
output:
html_document:
fig_width: 7
fig_height: 6
fig_caption: true
code_folding: "show"
---
I am trying to insert a figure with auto-numbered caption with the following code:
```{r, fig.cap="Figure caption \\label{fig_1}"}
plot(mtcars$hp, mtcars$mpg)
```
But the caption number won't show up. When I try to reference the image in the text
(Figure \ref{fig_1})
I just get:
(Figure )
I have the same problem. This only happens for Word and HTML outputs, so I think I may be using LaTeX documentation instead of the proper input for these types. I've tried a lot of the different recommendations for figure captions, but I can't seem to get any of it to work.
I needed to output to Word to adhere to a journal that only accepted Word... So I ended up using the captioner package. This should also work with html and pdf.
library(captioner)
fig_nums <- captioner()
In my r chunk I put my plot code and then the caption comes directly after the chunk in ticks, using the fig_nums function ``:
```{r}
plot(pressure)
```
`r fig_nums("pressure-plot", "pressure against temperature")`
It looks like this in the Word output:
Then I can refer to it in text like this:
As you can see from the nice visualisation in r fig_nums("pressure-plot", display = "cite") the pressure rises as
temperature rises.
This will show as:
As you can see from the nice visualisation in Figure 1 the pressure rises as
temperature rises
Be sure to run the first time you use, and check-out for more info:
install.packages("captioner")
vignette("using_captioner")
You can use the bookdown::html_document2 format.
---
title: "Untitled"
output: bookdown::html_document2
---
```{r pressure, echo=FALSE, fig.cap='test plot'}
plot(pressure)
```
\#ref(fig:pressure)

Code in Columns in RMarkdown presentation

Often when I include r code in a rmarkdown pdf presentation, I want to use the space of the whole slide and therefore want to present the code and the output side-by-side.
In pure LaTex I would go for \begin{columns}...\end{columns} and then include the code/output manually using lstlistings or some other code-highlighting library. But that proves tedious if I exceed a couple of code examples.
Now I want to use RMarkdown for the presentations and would like to achieve a similar result.
However, the following code throws an error:
## This is slide 1
\begin{columns}[t]
\begin{column}{0.5\textwidth}
```{r, eval=F}
plot(1:10)
```
\end{column}
\begin{column}{0.5\textwidth}
```{r, echo=F}
plot(1:10)
```
\end{column}
\end{columns}
Leaving out the knitr code-chunks and replacing them with text works.
I am aware that it has something to do with the pandoc engine (see here), but wanted to ask if anybody has found a way around this issue.
Well, I may should have looked with a wider focus.
Here is a solution that works for Python, but can easily be adapted to Rmarkdown: https://stackoverflow.com/a/26069751/3048453
I ended up with this code:
in header.tex
\newcommand{\columnsbegin}{\begin{columns}}
\newcommand{\columnsend}{\end{columns}}
in presentation.Rmd
---
title: "Two-column codes in RMarkdown"
author: "My Name"
date: "February 4, 2017"
output:
beamer_presentation:
includes:
in_header: header.tex
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Testslide with Columns
\columnsbegin
\column{.5\textwidth}
```{r, eval=F}
plot(mtcars[, 1:3])
```
\column{.5\textwidth}
```{r, echo=F}
plot(mtcars[, 1:3])
```
\columnsend
Which results in this

Add a CSS class to single code chunks in RMarkdown

Is it possible to add a CSS class to a certain code chunk?
Assume the following file:
---
title: "Untitled"
output: html_document
---
```{r cars}
summary(cars)
```
I want to give the chunk labeled 'cars' a certain CSS class, e.g. .myClass.
Is there any possibility like
```{r cars} {.myClass}
summary(cars)
```
or so? I am aware of hacks like wrapping the whole chunk in another <div>. I am interested in a straight forward solution.
Edit: this feature was introduced in knitr v.1.16 (05/18/17)
class.source and class.output options apply additional HTML classes to source and output chunks (see knitr documentation).
To add myClass to source chunk:
```{r cars, class.source='myClass'}
summary(cars)
```
Previous answer that inspired the class.source options (see here)
You can add a class using the fenced_code_attributes pandoc's extension (which is intended to add attributes to the <pre> tag, see here) and a knitr output hook.
The following example works fine:
---
title: "Untitled"
output:
html_document:
md_extensions: +fenced_code_attributes
---
```{r, include=FALSE}
knitr::knit_hooks$set(source = function(x, options) {
return(paste0(
"```{.r",
ifelse(is.null(options$class),
"",
paste0(" .", gsub(" ", " .", options$class))
),
"}\n",
x,
"\n```"
))
})
```
```{r cars, class="myClass1 myClass2"}
summary(cars)
```
After knitting this .Rmd file, the HTML document looks like this:
<pre class="r myClass1 myClass2">
<code>
summary(cars)
</code>
</pre>
The fenced_code_attributes extension is enabled by default: in standard cases, you don't need to include the line md_extensions: +fenced_code_attributes in your YAML header.
I don't know if there's more straightforward solution using knitr.

Resources