apply multiple columns division in the same R markdown page - r

I want to divide my Rmarkdown document like in picture. One column for the first section then two columns in the other sections.
used \onecolumn and \twocolumn but each section is created in a single page. Is there a solution to fix it?

This article has been published in a journal of the AMS. Fortunately, the rticle package supports the AMS journals. I use the standard template as generated by RStudio, change layout in the YAML header and use fancyhdr to redefine the pagestyle:
layout: twocol
header-includes:
- \usepackage{fancyhdr}
- '`\fancypagestyle{plain}{\fancyhf{}\fancyfoot[C]{\thepage}\renewcommand{\headrulewidth}{0pt}}`{=latex}'
I then get the following result:
As an alternative, you could try to adapt the revtex LaTeX class as used by the APS.
Note: I am using a quite recent version of pandoc, which makes the quoting of LaTeX complicated. With older versions of pandoc you might get away without the quoting.

Related

Is there an easy way to use symbols for footnotes in rmarkdown/bookdown?

I would like to use numbered references and symbols for footnotes in my Rmarkdown document. The bookdown package is excellent for the referencing and cross-referencing, etc. However, I can't find any easy way to change the footnote numbering to symbols. Is there an easy way (ideally to export as both Word and PDF)?
Turns out, it's as simple as adding
header-includes:
- \renewcommand*{\thefootnote}{\fnsymbol{footnote}}
To the YAML.
Thanks to https://www.resurchify.com/latex_tutorial/latex_footnotes.php

Bookdown (proof) custom environment: how to nest code chunks or inline R inside custom (e.g. proof) environments and get proper parsing?

I guess my question is a potential if not probable duplicate of How to use inline R code in a bookdown theorem or example environment. It's been nearly 3 years since this question was asked, so a refresh might be welcome in any case, all the more given #YiHui's comment: "That is not possible with bookdown (at least for now)"?
Background: I am trying to use #YiHui bookdown package to produce a book with lecture slides (see creating accompanying slides for bookdown project). A key feature for this purpose is to be able to use conditional formatting, such as eval = (out_type=="beamer"). The issue I am facing is therefore to be able to add code chunks or inline R code inside bookdown custom environments (see https://bookdown.org/yihui/bookdown/markdown-extensions-by-bookdown.html#theorems and https://bookdown.org/yihui/bookdown/custom-blocks.html). I would love to get the following to compile properly. Is there a solution? Would a hook work (see https://yihui.org/knitr/hooks/)? There is a hack that works for forcing pandoc to parse inside a latex environment, but i do no think it applies here. Would a lua filter work (I have no idea how to use this)?
En passant, I am also surprised to see $\digamma$ showing up properly in the html output, but not in the pdf output, though it is parsed correctly in the tex file. The amssymb package is loaded and it's supposedly defined in there - what am I missing?
Much appreciated any help on this. Here is a MWE - I just edited chapter 4 from the default bookdown project. You can simply replace the contents of the file 03-method.Rmd with the following:
# Methods
We describe our methods in this chapter.
So here is a theorem to prove 1+1 = `r 1+1`.
```{theorem, echo=TRUE}
we want to show that in R, 1+1 = 2.
We also wonder why `$\digamma$` ($\digamma$) not showing.
Shows in the html output only, not pdf.
But when I recompile the tex file produced by bookdown - shows up just fine!!
Indeed, is defined correctly from `\usepackage{amssymb}`.
```
```{proof, echo=TRUE}
- If I am not mistaken, I cannot get an Rmarkdown style list to work either in the proof environment.
- Well, this is where i would like to be able to use inline code.
- we use r to compute 1+1: `r 1+1`.
- Does not work.
- Just shows verbatim inline code.
```
With the current development version of bookdown:
remotes::install_github('rstudio/bookdown')
you can write theorems in Pandoc's fenced Divs, e.g.,
# Methods
We describe our methods in this chapter.
So here is a theorem to prove 1+1 = `r 1+1`.
::: {.theorem}
we want to show that in R, 1+1 = 2.
We also wonder why `$\digamma$` ($\digamma$) not showing.
Shows in the html output only, not pdf.
But when I recompile the tex file produced by bookdown - shows up just fine!!
Indeed, is defined correctly from `\usepackage{amssymb}`.
:::
::: {.proof}
- If I am not mistaken, I cannot get an Rmarkdown style list to work either in the proof environment.
- Well, this is where i would like to be able to use inline code.
- we use r to compute 1+1: `r 1+1`.
- Does not work.
- Just shows verbatim inline code.
:::
However, please note that this new syntax is not supported for beamer output yet.

How to use a LaTeX package with R/exams?

I am using exams2moodle() to create exam quizzes. I would like to use some math symbols which require the LaTeX package amssymb. How should I proceed?
TL;DR: amssymb LaTeX symbols are supported by MathJax which is the default renderer in a standard Moodle installation. So these symbols should work out of the box when using exams2moodle(..., converter = "pandoc-mathjax") which is the default sinc R/exams 2.3.3 (released in July 2019).
Details: As already pointed out by the answer of #Hack-R, there are various ways of including mathematical notation written in LaTeX in HTML-based exercises. An overview is given in this blog post: http://www.R-exams.org/tutorials/math/
It is important to note that in this case the LaTeX code is not actually processed by LaTeX itself. It can either be converted to MathML (using pandoc or ttm) or rendered in the browser by the MathJax JavaScript library. In any case it depends on the converter/renderer which LaTeX commands can be successfully displayed. All of them support basic LaTeX commands plus some extra commands from certain packages. Which additional commands from which packages are supported depends on the converter/renderer. In any case, the capabilities can not be extended by style files!
In a standard Moodle installation MathJax is enabled which suports the AMS LaTeX package (because MathJax was developed by/for the AMS!). So the best strategy is to simply keep the LaTeX in the HTML for Moodle. This can be done with exams2moodle(..., converter = "pandoc-mathjax") which is actually the default converter since R/exams 2.3.3 released in July 2019. In previous versions of R/exams, the default was converter = "ttm" which did not support all the amssymb symbols.
In general, you input your symbols into either .Rmd or .Rnw and you can have it rendered with any available engine (e.g. pandoc, mathjax, knitr, etc).
install.packages("exams")
require(exams)
xWeave is called on each exercise file and creates LaTeX code
elearn_exam <- c("swisscapital.Rmd", "deriv.Rmd", "ttest.Rmd",
"boxplots.Rmd", "function.Rmd", "lm.Rmd", "fourfold2.Rmd")
set.seed(2020-04-16)
exams2moodle() produces an XML file that may be uploaded into Moodle
It goes from LaTeX to HTML then HTML to XML
exams2moodle(elearn_exam, n = 3, name = "R-exams")
To add custom LaTeX packages you can modify the preamble as per the answers in this post, i.e. \usepackage
As described in the linked post, that can either be done direct, i.e.
---
title: "Title"
author: "Me"
header-includes:
- \usepackage{mypackage}
output:
pdf_document
---
or via a mystyles.sty file in the same directory.

Add speaker notes to beamer presentations using RMarkdown

I want to create a beamer pdf presentation from a RMarkdown file.
I would like to add speaker notes to some slides and set options for these notes (to be printed or not when you print the presentation).
These speaker notes typically show on the speaker's computer when he is presenting on a screen but never show on the slides.
Is that possible? How can I do that?
Possible workaround, but unfortunately not purely markdown
Adapted from DanielEWeeks's GitHub.
01. Add the following to the YAML section
header-includes:
- \setbeameroption{show notes}
02. and add notes as
## This is a slide
this is (markdown) text in slide
\note{
this is a note (does not understand markdown and
and wont work in other outputs formats (such
as ioslides or Slidy
}
## Next Slide
Notes will look like:
03. you would then have to knit the document twice, changing the YAML header-includes to
header-includes:
- \setbeameroption{hide notes}
in order to create the pdf without notes.
By default, Rmarkdown will overwrite the previously created document, so you might need to:
create notes pdf-document.
change name of notes pdf-document.
change YAML section.
create presentation pdf-document.
Hopefully there is a better way that I don't know about.
I was wondering how to do this when using a pptx output. This is how:
::: notes
This is a speaker note.
- Use basic Markdown
- like this list
- *and inline formatting*
:::
Reproduced from here.

two-column layouts in RStudio presentations/slidify/pandoc

I'm trying to come up with a good system for generating slides and accompanying handouts. The ideal system would have the following properties:
beautiful in both presentation (PDF/HTML) and handout (PDF) layouts (handouts should have room for taking notes)
embedded R chunks, figures, other JPG/PNG pictures, etc.
easy to compose
build using command-line tools
bibliography support
pandoc slide separator format (automatically generate a new slide after headers of a specified level) is preferred
I can live with a little bit of additional processing (e.g. via sed), but would prefer not to write a huge infrastructure
two-column layouts: there is a SO post on how to get multi-column slides from pandoc, but it is LaTeX- rather than HTML-oriented.
ability to adjust sizes of embedded images (other than R-generated figures) and column widths on the fly
Here's what I've discovered so far about the various options:
Slidify:
doesn't do pandoc slide separator format, although there is a workaround
the suggestion for creating handouts is to print to PDF; I'd like to leave room for notes etc. (I could probably figure out a way to do that using something like PDFtk or psnup ...)
RStudio presentations (.Rpres files):
does lots of things nicely, including multi-columns with specified widths
doesn't support pandoc slide separator format
I can't figure out what's going on under the hood. There is RStudio documentation that describes the translation process for regular HTML, but it doesn't seem to cover the R presentation format (which isn't quite the same). (I have previously invested some effort in figuring out how to get RStudio-like output via pandoc ...), which means I can't generate slides etc. from the command line.
RStudio's Development Version (as of March 2014) comes bundled with Pandoc and version 2 of rmarkdown. It addresses many of the above issues with the .Rpres format.
pandoc: may be the only markdown-translator that has features such as footnotes, bibliography support, etc.. I can also use pandoc to generate LaTeX using the tufte-handout class, which meets my criteria of beauty.
Unfortunately, it seems not to have built-in two-column format support. Yihui Xie's HTML5 example doesn't show any two-column examples, and it claims (on slide 5) that clicking the "Knit HTML" button in RStudio is equivalent to pandoc -s -S -i -t dzslides --mathjax knitr-slides.md -o knitr-slides.html, but it doesn't seem to be ...
LaTeX/beamer: I could simply compose in Rnw (knitr-dialect Sweave) rather than R markdown to begin with. This would give me ultimate flexibility ...
despite many years of LaTeX use I do find LaTeX composition more of a pain than markdown composition.
After all that, my specific question is: what's the best (easiest) way to generate a two-column layout for HTML output?
Any other advice will also be appreciated.
This is an old Q, but I was recently plagued by a similar question, here's what I found:
Using the RPres format, two columns can be specified like so (details). Note that RPres can only be converted to HTML by clicking a button in RStudio, there doesn't seem to be any command line method, which is a bit annoying. Despite, that I'd say it is currently the simplest and most flexible method for getting slide columns with markdown:
===
Two Column Layout
===
This slide has two columns
***
```{r, echo=FALSE}
plot(cars)
```
Some flexibility is afforded by adjusting the column proportions:
===
Two Column Layout
===
left: 30%
This slide has two columns
***
```{r, echo=FALSE}
plot(cars)
```
With rmarkdown we can get two columns, but with no control over where the break is, which is a bit of a problem:
---
output: ioslides_presentation
---
## Two Column Layout {.columns-2}
This slide has two columns
```{r, echo=FALSE}
plot(cars)
```
We can also mix markdown and LaTeX in an Rmd file using the beamer_presentation format in RStudio to get two columns like this, but can't run any code in either column, which is a limitation:
---
output: beamer_presentation
---
Two Column Layout
-------
\begin{columns}
\begin{column}{0.48\textwidth}
This slide has two columns
\end{column}
\begin{column}{0.48\textwidth}
If I put any code in here I get an error, see
https://support.rstudio.com/hc/communities/public/questions/202717656-Can-we-have-columns-in-rmarkdown-beamer-presentations-
\end{column}
\end{columns}
Seems like a regular Rnw LaTeX doc is the best way to get columns if you want to use LaTex, not this markdown hybrid (cf. two column beamer/sweave slide with grid graphic)
In all of the above an image can be placed in an column.
The slidify website has instructions on making two columns here: http://slidify.org/customize.html but it's not clear what has to go into the assets/layouts folder to make it work
You can use fenced_divs notation or ::: to create columns or `Two Content layout'. See also this page to know more about the notation.
## Slide With Image Left
::: columns
:::: column
left
::::
:::: column
right
```{r your-chunk-name, echo=FALSE, fig.cap="your-caption-name"}
knitr::include_graphics("your/figure/path/to/the-image.pdf")
#The figure will appear on the right side of the slide...
```
::::
:::
Since pandoc 2+, which supports the notation, was implemented in RStudio v1.2+, you may need to install RStudio v1.2+ first. The installation is easy enough (at least in my case); just download and install RStudio v1.2+. In the way of installation, the former version of RStudio on your computer will be replaced with the new one without uninstalling it manually.
The ::: notation can be used even when you knit .Rmd files with beamer_presentation option, as well as when you create HTML slides. So we don't have to neither mix markdown and LaTeX notation in one file, nor add additional codes any longer: just knit the file as you knit other .Rmd with other options.
I now have what I think is a reasonable solution that should apply at least to ioslides-based solutions, and maybe (?) to other HTML5-based formats. Starting here, I added
<style>
div#before-column p.forceBreak {
break-before: column;
}
div#after-column p.forceBreak {
break-after: column;
}
</style>
to the beginning of my document; then putting <p class="forceBreak"></p> within a slide with {.columns-2} breaks the column at that point, e.g.
## Latin hypercube sampling {.columns-2}
- sample evenly, randomly across (potentially many) uncertain parameters
<p class="forceBreak"></p>
![](LHScrop.png)
[User:Saittam, Wikipedia](https://commons.wikimedia.org/wiki/File:LHSsampling.png#/media/File:LHSsampling.png)
There may be an even better way, but this isn't too painful.
#ChrisMerkord points out in comments that
.forceBreak { -webkit-column-break-after: always; break-after: column; }
worked instead (I haven't tested ...)
I got an idea from HERE, the basic solutions was:
### Function *inner_join*
. . .
`<div style="float: left; width: 50%;">`
``` {r, echo = FALSE, results = 'markup', eval = TRUE}
kable(cbind(A,B))
```
`</div>`
`<div style="float: right; width: 50%;">`
```{r, echo = TRUE, results = 'markup', eval = TRUE}
inner_join(A,B, by="C")
```
`</div>`
There is a workaround for beamer error.
In short: Error is related to pandoc conversion engine, which treats everything between \begin{...} and \end{...} as TeX. It can be avoided by giving new definition for begin{column} and end{column} in yaml header.
Create mystyle.tex and write there:
\def\begincols{\begin{columns}}
\def\begincol{\begin{column}}
\def\endcol{\end{column}}
\def\endcols{\end{columns}}
In the Rmd file use these new definitions
---
output:
beamer_presentation:
includes:
in_header: mystyle.tex
---
Two Column Layout
-------
\begincols
\begincol{.48\textwidth}
This slide has two columns.
\endcol
\begincol{.48\textwidth}
```{r}
#No error here i can run any r code
plot(cars)
```
\endcol
\endcols
And you get:
So far I haven't been able to do better than hacking my own little bit of markup on top of the rmd format: I call my source file rmd0 and run a script including this sed tidbit to translate it to rmd before calling knit:
sed -e 's/BEGIN2COLS\(.*\)/<table><tr><td style="vertical-align:top; width=50%" \1>/' \
-e 's/SWITCH2COLS/<\/td><td style="vertical-align:top">/' \
-e 's/END2COLS/<\/td><\/tr><\/table>/' ...
There are a few reasons I don't like this. (1) It's ugly and special-purpose, and I don't have a particularly good way to allow optional arguments (e.g. relative widths of columns, alignment, etc.). (2) It has to be tweaked for each output format (e.g. if I wanted LaTeX/beamer output I would need to substitute \begin{columns}\begin{column}{5cm} ... \end{column}\begin{column}{5cm} ... \end{column}\end{columns} instead (as it turns out I want to ignore the two-column formatting when I make LaTeX-format handouts, so it's a little easier, but it's still ugly).
Slidify may yet be the answer.
Not a direct solution, but Yihui's Xaringan package https://github.com/yihui/xaringan/ works for me. It's based on remark.js. In the default template, you can use .pull-left[] and .pull-right[] . Example: https://slides.yihui.name/xaringan/#15. You only need a minimum tweak on the existing .rmd files.

Resources