Line breaks in quarto document with typst

I am trying to get on the Quarto bandwagon, but have had trouble converting my R Markdown files to Quarto, so would always give up. Now, however, I see that Quarto is supporting typst, and since most of my documents are made into PDFs, I have decided to finally take the plunge and figure out how to make Quarto work. My first problem: I want to format a string from my YAML params to correctly print in the typst-generated PDF. Here is an example of my qmd:

---
title: "New line - typst"
format:
  typst:
    papersize: us-letter
    margin:
      x: 1in
      y: 0.5in
    numbering: "1"
    mainfont: "Calibri"
    fontsize: 11pt
    

params:
  UAs:    "A:66
B:8,18,35,37,38,39,41,42,54,55,59,64,65,67,73,78
Cw:2,4,5,6,7,8,12,15,17,18
DR:8,11,12,13,14,17,18
DQ:7
DQA:04,05,06"
---

```{r}
#| include: false

library(tidyverse)

# Format the UA string for printing.
(UA_string <- params$UAs %>% str_replace_all(" ", " \\\\ "))
```

## Current NKR UAs (avoids):  
`{r} UA_string`

I want my last line to print my string on six lines. Typst documentation here (Line Break Function – Typst Documentation) says placing a backslash and a space will create a new line, which is what I have attempted to do with UA_string <- params$UAs %>% str_replace_all(" ", " \\\\ "), but it does not print out with line breaks in the PDF.

Debugging advice: Look at intermediate files:

keep-typ: true
keep-md: true

This is valid if you produced Raw typst. Currently, you produce Markdown content that will be parsed. In Markdown linebreak is done using two spaces and a new line (Pandoc - Pandoc User’s Guide)

If you need a hard line break, put two or more spaces at the end of a line.

In the intermediate, you can see that you would produce Markdown content, that will be then converted to typst.

So you could do two spaces and a newline

(UA_string <- params$UAs |> stringr::str_replace_all(" ", "  \n"))

image

Otherwise, if you produce raw content, you need to use specific block or at least use correct typst. From the doc you linked, I do think you need to had newlines after the \. So I would try

```{r}
#| include: false

# Format the UA string for printing.
(UA_string <- params$UAs |> stringr::str_replace_all(" ", " \\\\\n") |> knitr::raw_block(type = "typst"))
```


## Current NKR UAs (avoids):  
`r UA_string`
  • knitr::raw_block() will ensure you are setting the content to be Raw Typst
  • using `r and not `{r} will make sure the content is not escaped. If you are already producing Markdown output, you need to either using `{r} I(<markdown_string>)` or just usual knitr (See Quarto – Inline Code)

This will produce this Markdown

```{=typst}
A:66 \
B:8,18,35,37,38,39,41,42,54,55,59,64,65,67,73,78 \
Cw:2,4,5,6,7,8,12,15,17,18 \
DR:8,11,12,13,14,17,18 \
DQ:7 \
DQA:04,05,06
```

And Same output as above.

Another variant as example

```{r}
#| include: false

# Format the UA string for printing.
(UA_string <- params$UAs |> stringr::str_replace_all(" ", " \\\\\n"))
```


## Current NKR UAs (avoids):  

```{=typst}
`{r} I(UA_string)`
```

Hope it helps understand

Note: I edited your post. Please format correctly next time (FAQ: How to Format R Markdown Source). Thank you !

This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.