Pandoc’s Markdown supports the multi-column layout for slides but not other types of documents. In this recipe, we show how to use the multi-column layout in normal HTML documents and LaTeX documents. This recipe was inspired by Atsushi Yasumoto’s solutions to the knitr issue https://github.com/yihui/knitr/issues/1743.

    The recipe will be much simpler if you only need to consider HTML output, because arranging HTML elements side by side is relatively simple via CSS. It will be even simpler if you only need to arrange the text output of a code chunk side by side. Below is the first example:

    1. ---
    2. output: html_document
    3. ---
    4. ```{r attr.source="style='display:inline-block;'", collapse=TRUE}
    5. 1:10 # a sequence from 1 to 10
    6. 10:1 # in the reverse order
    7. ```

    The CSS attribute display: inline-block; means the output code blocks (i.e., the <pre> tags in HTML) should be displayed as inline elements. By default, these blocks are displayed as block-level elements (i.e., display: block;) and will occupy whole rows. The chunk option collapse = TRUE means the text output will be merged into the R source code block, so both the source and its text output will be placed in the same <pre> block.

    If you want to arrange arbitrary content side by side in HTML output, you can use Pandoc’s fenced Div. The name “Div” comes from the HTML tag <div>, but you can interpret it as an arbitrary block or container. A Div starts and ends with three or more colons (e.g., :::). A Div with more colons can contain Divs with fewer colons. An important and useful feature of the fenced Div is that you can attach attributes to it. For example, you can apply the CSS attribute display: flex; to an outside container, so that the inside containers will be placed side by side:

    1. ---
    2. output: html_document
    3. ---
    4. :::: {style="display: flex;"}
    5. ::: {}
    6. Here is the **first** Div.
    7. ```{r}
    8. str(iris)
    9. ```
    10. :::
    11. ::: {}
    12. And this block will be put on the right:
    13. ```{r}
    14. plot(iris[, -5])
    15. ```
    16. :::
    17. ::::

    In the above example, the outside Div (::::) contains two Divs (:::). You can certainly add more Divs inside. To learn more about the very powerful CSS attribute display: flex; (CSS Flexbox), you may read the guide at https://css-tricks.com/snippets/css/a-guide-to-flexbox/. The CSS Grid (display: grid;) is also very powerful and can be used in the above example, too. If you want to try it, you may change display: flex; to display: grid; grid-template-columns: 1fr 1fr; grid-column-gap: 10px;. See the guide at https://css-tricks.com/snippets/css/complete-guide-grid/ if you want to learn more about the grid layout.

    It is trickier if you want the layout to work for both HTML and LaTeX output. We show a full example below that works for HTML documents, LaTeX documents, and Beamer presentations:

    1. ---
    2. output:
    3. html_document:
    4. css: columns.css
    5. pdf_document:
    6. keep_tex: true
    7. includes:
    8. in_header: columns.tex
    9. beamer_presentation:
    10. keep_tex: true
    11. includes:
    12. in_header: columns.tex
    13. ---
    14. # Two columns
    15. Below is a Div containing three child Divs side by side. The Div
    16. in the middle is empty, just to add more space between the left
    17. and right Divs.
    18. :::::: {.cols data-latex=""}
    19. ::: {.col data-latex="{0.55\textwidth}"}
    20. ```{r, echo=FALSE, fig.width=5, fig.height=4}
    21. par(mar = c(4, 4, .2, .1))
    22. plot(cars, pch = 19)
    23. ```
    24. :::
    25. ::: {.col data-latex="{0.05\textwidth}"}
    26. \
    27. <!-- an empty Div (with a white space), serving as
    28. a column separator -->
    29. :::
    30. ::: {.col data-latex="{0.4\textwidth}"}
    31. The figure on the left-hand side shows the `cars` data.
    32. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
    33. eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
    34. enim ad minim veniam, quis nostrud exercitation ullamco laboris
    35. nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
    36. in reprehenderit in voluptate velit esse cillum dolore eu fugiat
    37. nulla pariatur.
    38. :::
    39. ::::::

    Image 1: A two-column layout that works for HTML, LaTeX, and Beamer output.

    FIGURE 5.3: A two-column layout that works for HTML, LaTeX, and Beamer output.

    Figure 5.3 shows the output. In this example, we used an outside Div with the class .cols and three inside Divs with the class .col. For HTML output, we introduced an external CSS file columns.css, in which we applied the Flexbox layout to the outside Div so the inside Divs can be placed side by side:

    For LaTeX output (pdf_document), we have to introduce some dirty hacks stored in columns.tex to the LaTeX preamble to define the LaTeX environments cols and col:

    1. \newenvironment{cols}[1][]{}{}
    2. \newenvironment{col}[1]{\begin{minipage}{#1}\ignorespaces}{%
    3. \end{minipage}
    4. \ifhmode\unskip\fi
    5. \aftergroup\useignorespacesandallpars}
    6. \def\useignorespacesandallpars#1\ignorespaces\fi{%
    7. #1\fi\ignorespacesandallpars}
    8. \makeatletter
    9. \def\ignorespacesandallpars{%
    10. \@ifnextchar\par
    11. {\expandafter\ignorespacesandallpars\@gobble}%
    12. {}%
    13. }
    14. \makeatother

    The col environment is particularly complicated mainly because Pandoc starts a new paragraph for each Div in LaTeX output, and we have to remove these new paragraphs. Otherwise, the Divs cannot be placed side by side. The hacks were borrowed from https://tex.stackexchange.com/q/179016/9128.

    For Beamer output, we apply the same hacks in columns.tex. Please note that Pandoc has provided some special Div’s for slide shows, such as ::: {.columns}, ::: {.column}, and ::: {.incremental}. Because they already have their special meanings, you must be careful not to use these types of Div’s if you intend to convert a Div to a LaTeX environment in the way mentioned in this section. That is why we did not use the Div types columns or column, but chose to use cols and col instead.

    For more information about fenced Div’s, please refer to Section 9.6