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

5

30

When rendering html documents with rmarkdown there are ways to make a two columns layout, e.g. here

Is there an easy way to render a pdf document with two column layout? Is there an example code somewhere?

Keane answered 15/1, 2016 at 10:4 Comment(0)
S
33

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"
    ]
---
Stentor answered 15/1, 2016 at 10:37 Comment(3)
ok, I see where the second column is supposed to be but all the content is in the first column. How to put content to second column?Lise
So to start a twocolumn section, you just use \twocolumn, but how do you set it back to onecolumn for subsequent sections (like figures, for instance)?Subway
you can use \onecolumn but fine tuning this is hard. You'll find more help on tex.SE: tex.stackexchange.comStentor
S
9

More succinctly:

---
output:
  pdf_document:
classoption: twocolumn
---
Samiel answered 11/9, 2018 at 16:47 Comment(0)
B
7

Regarding switching between one and two columns modes in pdf the following snippets work for me

To two column mode:

```{r, echo=FALSE, results='asis'}
cat("\\twocolumn")
```

To one column mode:

```{r, echo=FALSE, results='asis'}
cat("\\onecolumn")
```
Blowtube answered 19/2, 2019 at 19:43 Comment(1)
This is starting a new page for me each time I change. I can't switch on the same page.Wittenburg
P
6

Credit for this answer found here: https://timmurphy.org/2010/06/23/adding-a-two-column-section-to-a-latex-document/

\begin{minipage}[t]{0.5\textwidth}
First Column Goodies.\\
More First Column Goodies.\\
\end{minipage}
\begin{minipage}[t]{0.5\textwidth}
Second Column Goodies.\\
More Second Column Goodies.\\
\end{minipage}

Note: VERY important that there is no space between the /end{minipage} and the next \begin{minipage} (not counting comments). Otherwise LaTeX will not render the columns side by side.

Pam answered 14/8, 2019 at 17:26 Comment(2)
This one has the benefit of working with xelatex as well.Riffle
This is the answer that worked for me with pdf_documentAtomize
G
2

In addition to scoa's answer, in order to give the columns more space you can add a value to the header-includes:, for example:

---
output:
  pdf_document:
    ...
header-includes:
- \setlength{\columnsep}{18pt}
---
Gayla answered 13/10, 2017 at 11:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.