How Make 2 Column Layout in R Markdown When Rendering PDF

How make 2 column layout in R markdown when rendering pdf?

New pandoc version have made this easier since my original answer. According to pandoc's manual, you can now specify classoptions directly in the YAML front matter:

---
classoption:
- twocolumn
---

The new div notation also allows for inserting two column sections anywhere in the document, working for most formats

:::::::::::::: {.columns}
::: {.column width="40%"}

contents...

:::
::: {.column width="60%"}

contents...

:::
::::::::::::::

Original answer

You can use the article option twocolumn to format the whole document in two columns. Add this to your yaml front matter:

---
output:
pdf_document:
pandoc_args: [
"-V", "classoption=twocolumn"
]
---

How can I change the number of columns several times in an R Markdown pdf document?

What you can do is first, to add -- as you stated -- pandoc_args: ... into your YAML header. Second, there are a few LaTeX solutions around (like this or this one) which won't work for RMarkdown. The only way I found so far is to use \onyecolumn / \twocolumn -- just with the drawback of the page breaks. But perhaps you can live with it until there's a better solution.

---
title: "Test"
output:
pdf_document:
pandoc_args: [
"-V", "classoption=twocolumn"
]
html_document: default
header-includes:
- \usepackage{lipsum} # just used for producing example text in document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Two columns of text
\lipsum[1-7]
\onecolumn

## Once column section.
This part should be the whole page width
```{r plot}
plot(rnorm(20),rnorm(20))
```
\lipsum[1]
\twocolumn

## Now 2 columns again
This section should go back to two columns
\lipsum

\begin{table*}
This is nice, but won't work with R chunks or headers. And you'll have to format with LaTeX code (e.g. \textbf{Foo blaah}).
\lipsum[1]
\end{table*}
\lipsum

Put 2 chunks of code side by side in RMarkdown or Quarto?

---
title: "Untitled"
output: html_document
---

:::::::::::::: {.columns}
::: {.column width="50%"}

```{r warning=FALSE,message=FALSE}
library(dplyr)
mtcars %>% select(gear)
```

:::
::: {.column width="50%"}

```{r warning=FALSE,message=FALSE}
library(dplyr)
select(mtcars, gear)
```

:::
::::::::::::::

Sample Image

used This SO question as a resource. This is using pandoc to format the document in Rmarkdown HTML output

Have two columns in Markdown

You can't, at least not with pure Markdown as it doesn't have any concept of columns. As explained in the rules:

The idea for Markdown is to make it easy to read, write, and edit prose. HTML is a publishing format; Markdown is a writing format. Thus, Markdown’s formatting syntax only addresses issues that can be conveyed in plain text.

For any markup that is not covered by Markdown’s syntax, you simply use HTML itself.

In fact, the best way would be to have each code block wrapped in a <div> with the appropriate class assigned to each div. However, most Markdown parsers do not parse Markdown inside a raw HTML block. Therefore, you may need to also define the code block in raw HTML as well. Check your parser's list of features to find out. In the event you are not able to define your own CSS to use separately from the Markdown (to style the HTML), you will also need to define the styles inline in the HTML. This question includes a nice sample of what that might look like. Just replace the comments with the appropriate code blocks. If you have to define your code blocks in raw HTML, they would look like this:

<pre><code class="language-c">int foo (void) 
{
int i;
}
</code></pre>

So, the final document that is sure to work in all (most?) Markdown parsers would look like this:

# Rule 1
Description for rule 1.

<div style="-webkit-column-count: 2; -moz-column-count: 2; column-count: 2; -webkit-column-rule: 1px dotted #e0e0e0; -moz-column-rule: 1px dotted #e0e0e0; column-rule: 1px dotted #e0e0e0;">
<div style="display: inline-block;">
<h2>Good</h2>
<pre><code class="language-c">int foo (void)
{
int i;
}
</code></pre>
</div>
<div style="display: inline-block;">
<h2>Bad</h2>
<pre><code class="language-c">int foo (void) {
int i;
}
</code></pre>
</div>
</div>

Note that that uses one of many different ways of defining columns in CSS. Different methods may or may not work in different browsers. YMMV.



Related Topics



Leave a reply



Submit