Search code examples
rr-markdownquarto

How do you align text on different lines in Quarto?


I'm not having success aligning text on multiple lines in Quarto. This is easy in Microsoft Word. Below is an image of what I want to produce with a Quarto doc rendered to a PDF:

enter image description here

Here's what I've tried so far in Quarto that has not been successful:

  1. Just regular spacing with the space bar to align text.
  2. Using the visual mode option: Insert - Special Characters - Non-Breaking Line Space. Aligned text in visual mode, but PDF output is not aligned.
  3. Pipe tables attempting to use a variation of the format below from the Quarto help section https://quarto.org/docs/authoring/markdown-basics.html. The problem in the table format, different lines/cells will have different lengths. When attempting this in table form, Quarto keeps attempting to reformat the table.
| Right | Left | Default | Center |
|------:|:-----|---------|:------:|
|   12  |  12  |    12   |    12  |
|  123  |  123 |   123   |   123  |
|    1  |    1 |     1   |     1  |

I need to be able to align text similar to the image provided for company formatting, but I don't want to use Word because I need to be able to supply results of data analysis and calculations and want to avoid copy/paste.

I'm not familiar with CSS or other potential options, but open to any suggestions on how to align text in Quarto similar to the image. Thank you.


Solution

  • For pdf output flextable works well.

    1. Create a data.frame with the cells
    2. Feed into flextable()
    3. Remove header and borders
    4. Set column width()s
    ---
    title: "text align"
    format: pdf
    editor: source
    execute:
      echo: false
    ---
    
    ```{r}
    library(flextable)
    library(tibble)
    ```
    
    ```{r}
    df1 <- 
      tribble(~col1, ~col2,
            "Client:", "Multi \n Line \n Text \n Here",
            "Intended Users of The Report:", "One Line Text Here",
            "Third Item:", "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 in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.")
    ```
    
    ```{r}
    flextable(df1) |> 
      delete_part("header") |>
      border_remove() |> 
      width(1, 2.1) |> 
      width(2, 4) |> 
      valign(valign = "top")
    ```