Search code examples
rpdfyamlknitrquarto

How to Move R Code and Output to the Right in a Quarto PDF Document?


I want to move the R code and its output to the right in a Quarto-rendered PDF document. Could you help me achieve this? Below is the YAML header I am using:

---
title: |
  \huge \textbf{Title}
format: 
  pdf:
    fig-width: 4
    fig-height: 3
    fig-align: center
    out.width: '100%'
    fig-format: pdf
    include-in-header: 
      text: |
        \usepackage[none]{hyphenat}
        \usepackage{enumitem}
        \setlist[enumerate]{font=\bfseries}
        \usepackage{float}
        \usepackage{scrlayer-scrpage}
        \usepackage[a4paper,
            bindingoffset=0.2in,
            left=0.5in,
            right=0.5in,
            top=0.85in,
            bottom=0.85in,
            footskip=0.5in]{geometry} 
        \usepackage{titling}
        \setlength{\droptitle}{-2cm}
        \clearpairofpagestyles
        \lohead*{\textbf{L}}
        \cohead*{\textbf{C}}
        \rohead*{\textbf{R}}
        \cofoot*{\pagemark}
        
        \usepackage{etoolbox}
        \AtBeginEnvironment{verbatim}{\setlength{\leftskip}{5cm}} % Adjust left margin for verbatim
        \AtBeginEnvironment{knitrout}{\setlength{\leftskip}{2cm}} % Adjust left margin for knitrout
execute:
  warning: false
  message: false
  error: false
  echo: true
---

# Intro
1.  This is a simple use of quarto.

```{r}
1 + 1
```

Edited

I am using the first option from this answer. However, I am looking for a method that eliminates the need to manually indent the verbatim.


Solution

  • Assuming by "move to the right" you mean an indentation of the code and output. For this to work you can indent your verbatim code like this:

    ---
    title: |
      \huge \textbf{Title}
    format: 
      pdf:
        fig-width: 4
        fig-height: 3
        fig-align: center
        out.width: '100%'
        fig-format: pdf
        include-in-header: 
          text: |
            \usepackage[none]{hyphenat}
            \usepackage{enumitem}
            \setlist[enumerate]{font=\bfseries}
            \usepackage{float}
            \usepackage{scrlayer-scrpage}
            \usepackage[a4paper,
                bindingoffset=0.2in,
                left=0.5in,
                right=0.5in,
                top=0.85in,
                bottom=0.85in,
                footskip=0.5in]{geometry} 
            \usepackage{titling}
            \setlength{\droptitle}{-2cm}
            \clearpairofpagestyles
            \lohead*{\textbf{Stat-8010}}
            \cohead*{\textbf{Statistical Methods I}}
            \rohead*{\textbf{Spring 2025}}
            \cofoot*{\pagemark}
           
    execute:
      warning: false
      message: false 
      error: false
      echo: true
    ---
    
    # Intro
    1.  This is a simple use of quarto.
    
        ```{r}
        x <- 1 + 1
        x
        ```
    

    Output

    out


    Another approach would be to do it all from the yaml. Adjust the left margin xleftmargin=2em.

    ---
    title: |
      \huge \textbf{Title}
    format: 
      pdf:
        fig-width: 4
        fig-height: 3
        fig-align: center
        out.width: '100%'
        fig-format: pdf
        highlight-style: github
        include-in-header: 
          text: |
            \usepackage[none]{hyphenat}
            \usepackage{enumitem}
            \setlist[enumerate]{font=\bfseries}
            \usepackage{float}
            \usepackage{scrlayer-scrpage}
            \usepackage[a4paper,
                bindingoffset=0.2in,
                left=0.5in,
                right=0.5in,
                top=0.85in,
                bottom=0.85in,
                footskip=0.5in]{geometry} 
            \usepackage{titling}
            \setlength{\droptitle}{-2cm}
            \clearpairofpagestyles
            \lohead*{\textbf{Stat-8010}}
            \cohead*{\textbf{Statistical Methods I}}
            \rohead*{\textbf{Spring 2025}}
            \cofoot*{\pagemark}
            
            
            % For code chunks
            \DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\},baselinestretch=1.2,xleftmargin=2em}
            
             % For output
            \usepackage{fancyvrb}
            \RecustomVerbatimEnvironment{verbatim}{Verbatim}{xleftmargin=2em}
            
            
           
    execute:
      warning: false
      message: false 
      error: false
      echo: true
    ---
    
    # Intro
    1.  This is a simple use of quarto.
    
    ```{r}
    x <- 1 + 1
    x
    ```
    

    However, here I had to remove the background color, because I do not know, how to indent it.

    out2

    Instead, you could put a frame around the code though:

    ---
    title: |
      \huge \textbf{Title}
    format: 
      pdf:
        fig-width: 4
        fig-height: 3
        fig-align: center
        out.width: '100%'
        fig-format: pdf
        highlight-style: github
        include-in-header: 
          text: |
            \usepackage[none]{hyphenat}
            \usepackage{enumitem}
            \setlist[enumerate]{font=\bfseries}
            \usepackage{float}
            \usepackage{scrlayer-scrpage}
            \usepackage[a4paper,
                bindingoffset=0.2in,
                left=0.5in,
                right=0.5in,
                top=0.85in,
                bottom=0.85in,
                footskip=0.5in]{geometry} 
            \usepackage{titling}
            \setlength{\droptitle}{-2cm}
            \clearpairofpagestyles
            \lohead*{\textbf{Stat-8010}}
            \cohead*{\textbf{Statistical Methods I}}
            \rohead*{\textbf{Spring 2025}}
            \cofoot*{\pagemark}
            
            
            % For code chunks
            \DefineVerbatimEnvironment{Highlighting}{Verbatim}{%
              commandchars=\\\{\}, % Enable LaTeX commands
              baselinestretch=1.2, % Adjust line spacing
              xleftmargin=2em, % Indent by 2em
              frame=single % Add a border around the code block
            }
            
            
             % For output
            \usepackage{fancyvrb}
            \RecustomVerbatimEnvironment{verbatim}{Verbatim}{xleftmargin=2em}
            
            
           
    execute:
      warning: false
      message: false 
      error: false
      echo: true
    ---
    
    # Intro
    1.  This is a simple use of quarto.
    
    ```{r}
    x <- 1 + 1
    x
    ```
    

    out3


    Third Solution

    Finally, we can use \renewenvironment{Shaded} to customize the shaded code background. We use mdframed with a custom color and the margins are fully customizable!!

    ---
    title: |
      \huge \textbf{Title}
    format: 
      pdf:
        fig-width: 4
        fig-height: 3
        fig-align: center
        out.width: '100%'
        fig-format: pdf
        code-block-border-left: false
        code-block-bg: false
        include-in-header: 
          text: |
            \usepackage[none]{hyphenat}
            \usepackage{enumitem}
            \setlist[enumerate]{font=\bfseries}
            \usepackage{float}
            \usepackage{scrlayer-scrpage}
            \usepackage[a4paper,
                bindingoffset=0.2in,
                left=0.5in,
                right=0.5in,
                top=0.85in,
                bottom=0.85in,
                footskip=0.5in]{geometry} 
            \usepackage{titling}
            \setlength{\droptitle}{-2cm}
            \clearpairofpagestyles
            \lohead*{\textbf{Stat-8010}}
            \cohead*{\textbf{Statistical Methods I}}
            \rohead*{\textbf{Spring 2025}}
            \cofoot*{\pagemark}
            
            \usepackage{xcolor}     % For background color support
            \usepackage{mdframed}    % For framed boxes with background
            
            % Define the color you want 
            \definecolor{mycolor}{HTML}{F1F3F5} % use r classic (light blueish in this case)
            
            % Redefine the Shaded environment
            \renewenvironment{Shaded}                     % adjust leftmargin here
              {\begin{mdframed}[backgroundcolor=mycolor,leftmargin=2em,innerleftmargin=5pt,innerrightmargin=0pt,innertopmargin=5pt,innerbottommargin=5pt]}
              {\end{mdframed}}
            
            \usepackage{fancyvrb}    % For output
            \RecustomVerbatimEnvironment{verbatim}{Verbatim}{xleftmargin=2em}
           
    execute:
      warning: false
      message: false 
      error: false
      echo: true
    ---
    
    # Intro
    1.  This is a simple use of quarto.
    
    ```{r}
    x <- 1 + 1
    x
    ```
    

    final