Search code examples
rlatexr-markdownquarto

top align columns in quarto (r) and add column-lines between columns


I am trying to create a quarto document with a 3 column layout that is rendered to PDF in Rstudio. (see example below).

  1. I would like all outputs to align at the top. how can I do that layout-valign="top", seems to have no impact
  2. is it possible to add a vertical line between the different columns?

(apologies for the fact that these are two questions)

---
title: "Untitled"
format: pdf
---


## QUARTO



::: {layout="[1,-0.1, 1,-0.1, 1]" layout-valign="top"}
::: {#leftcolumn}

```{=latex}
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit ...
```

:::

::: {#middlecolumn}
```{r code, eval=TRUE}
x <- 1:10
y <- rnorm(10)
z  <- x/y
```
:::

::: {#text2}
```{r plot, echo=FALSE, fig.pos="H", fig.height=5}
plot(x, y)
```
::: 
:::

enter image description here


Solution

  • For aligning, you can use \vtop or \vbox cmd's in combination with Quarto's layout options. Inside Quarto, it has a built-in layout-valign="top" which may not work as expected for PDF outputs (especially with complex LaTeX renderings).

    You can also add vertical lines between columns, you can do this by overriding the layout system with LaTeX. You can see this in the code below:

    ---
    title: "Untitled"
    format: pdf
    header-includes:
       - \usepackage{multicol}  # For multi-column layout
       - \usepackage{fancyvrb}  # For better code styling
       - \setlength{\columnsep}{20pt}  # Adjust column separation
       - \setlength{\columnseprule}{0.4pt}  # Adds a vertical line between columns
    ---
    
    ## QUARTO
    
    \begin{multicols}{3}
    
    \vtop{
    \textbf{Column 1: Text Content}
    \noindent
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit ...
    }
    
    \columnbreak
    
    \vtop{
    \textbf{Column 2: R Code}
    \begin{Verbatim}[frame=single]
    x <- 1:10
    y <- rnorm(10)
    z  <- x/y
    \end{Verbatim}
    }
    
    \columnbreak
    
    \vtop{
    \textbf{Column 3: Plot Output}
    \includegraphics[width=\linewidth]{path_to_image}
    }
    
    \end{multicols}
    

    Variables:

    The multicols package is for managing columns.

    The \columnsep controls the space between columns.

    The \columnseprule creates thin vertical lines between columns.

    The \vtop command ensures that all contents are top-aligned.

    The Verbatim provides better control for rendering 'R' code in LaTeX.

    You can also use \includegraphics to insert plots generated in your code.

    You just need to make sure that you replace path_to_image with whatever path the plot is generated from.