rquartotypst

Typst Quarto Template in R


I'm using typst to create a template for Quarto pdf's in R. I'm formatting the typst-template.typ document I have in my files. Below is the code that works well with the typst software:

#place(
  top + left,
  dx: 0pt,
  dy: -20pt,
  rect(fill: rgb("FA8975FF"),
  width: 0.15cm, 
  height: 1cm),
)
#place(
  top + left,
  dx: -5pt,
  dy: -10pt,
  rect(fill: rgb("000000"),
  width: 100%, 
  height: 0.05cm),
)

And here is the R typ file that is not rendering right at all. How do I put two rectangles with each other in typst format in R? Are there any guides to transcribing typst format into R templates?

#let psc-report(
  title: "title",
  body,
) = {

// This sets the text for the entire document
set text(
  font: "Calibri",
  size: 10pt,
)

// This sets the paging formats
set page(
  "us-letter",
  margin: (
  left: 1in, 
  right: 1in, 
  top: 1in, 
  bottom: 1in
  ),
  background: [
  place(
    top + left,
    dx: 5pt,
    rect(
    fill: rgb("FA8975FF"), 
    width: 0.25cm, 
    height: 1cm
    )
    ),
  place(
    top + left,
    dx: 10pt,
    rect(
    fill: rgb("15397F"), 
    width: 100%, 
    height: 0.1cm
    )    
    )
    ],
    
    // not entirely sure how the footer page number works yet
  footer: align(
    grid(
      columns: (40%, 60%),
      align(
      horizon, 
      text(fill: rgb("15397F"), 
      size: 10pt, 
      counter(page).display("1")
      )
      ),
    )
  )
)

  body
}

Solution

  • I think I have the code you need below. You didn't include an image so I'm not sure if this is your desired output. If you can include an image I can adjust the code as needed. The trick is to pull the place functions out of the set page() function.

    I used the quarto create extension format command referenced here which set up the _extensions directory for typst. That created two .typ files: typst-show.typ and typst-template.typ.

    Here is the typst-template.typ file with the rectangles and footer:

    #let psc-report(
      title: "title",
      body,
    ) = {
    set page(
      "us-letter",
      margin: (
      left: 1in, 
      right: 1in, 
      top: 1in, 
      bottom: 1in
      ),
      footer: align(
        grid(
          columns: (40%, 60%),
          align(
          horizon, 
          text(fill: rgb("15397F"), 
          size: 10pt, 
          counter(page).display("1")
          )),
        ))
    )
      place(
        top + left,
        dx: 5pt,
        rect(
          fill: rgb("FA8975FF"), 
          width: 0.25cm, 
          height: 1cm))
              
      place(
        top + left,
        dx: 10pt,
        rect(
          fill: rgb("15397F"), 
          width: 100%, 
          height: 0.1cm))
      
    }
    

    Here is the typst-show.typ file:

    #show: body => psc-report(
    $if(title)$
      title: [$title$],
    $endif$
    
    $if(by-author)$
      authors: (
    $for(by-author)$
    $if(it.name.literal)$
        ( name: [$it.name.literal$]),
    $endif$
    $endfor$),
    $endif$
    
    $if(date)$
      date: [$date$],
    $endif$
      body,
    )