Typst

Misc

  • Alternative to LaTeX for formatting pdfs

  • Packages

    • {r2typ} - An R package for generating Typst markup
  • Resources

  • Notes from

  • Comparison with LaTeX

    • LaTeX

      \usepackage{geometry}
      \usepackage{eso-pic}
      \usepackage{xcolor}
      \geometry{a4paper, total={170mm,257mm}, left=20mm, top=20mm, bottom=20mm, right=50mm}
      \definecolor{light}{HTML}{E6E6FA}
      \AddToShipoutPicture{% 
        \AtPageLowerLeft{% 
            \put(\LenToUnit{\dimexpr\paperwidth-3cm},0){% 
                \color{light}\rule{3cm}{\LenToUnit\paperheight}}%
         }%
      }%
      • Advantages
        • You want to include equations (currently easier in LaTeX)
        • (Currently) more flexibility with images and tables
    • Typst

      set page(
        margin: (left: 2cm, right: 1.5cm, top: 2cm, bottom: 2cm),
        background: place(right + top, rect(
          fill: rgb("#E6E6FA"),
          height: 100%,
          width: 3cm,
        ))
      )
      • Advantages

        • Easier to learn
        • More informative error messages
        • No extra installations
        • Speed
  • Font

    ---
    title: "My document"
    format: 
      typst:
        mainfont: "Cascadia Code"
    ---

Components

  • Bindings
  • Rules
    • set (docs) - Allows you to customize the appearance of elements. They are written as a function call to an element function preceded by the set keyword (or #set in markup).
  • Functions
    • place (docs) - Places content relatively to its parent container
    • grid (docs) - Allows you to arrange content in a grid. You can define the number of rows and columns, as well as the size of the gutters between them. There are multiple sizing modes for columns and rows that can be used to create complex layouts.
    • align (docs) - Aligns content horizontally and vertically

Templates

  • Example 1
    • YAML

      ---
      title: "My document"
      format:
        typst:
            template-partials:
              - typst-template.typ
              - typst-show.typ
      ---
      • typst-show.typ takes the variables from the quarto document and passes them to typst

      • typst-template.typ - Once typst has the quarto variables, it creates this template which is used to create the pdf

    • Show

      #show: article.with(
      $if(title)$
        title: "$title$",
      $endif$
      )
      
      # another example with a param town in quarto yaml
      #show: psc-report.with(
        $if(title)$
          title: "$title$",
        $endif$
        $if(params.town)$
          town: "$params.town$",
        $endif$
      )
      • Says if there’s a title in the qmd, then create a variable, title, with its value
    • Template

      #let article(
        // The document title.
        title: "Some title",
        // The document content.
        body
      ) = {
      
        // Set document metadata.
        set document(title: title)
      
        // Links should be purple.
        show link: set text(rgb("#800080"))
      
        // Title.
        text(weight: 800, upper(title))
  • Example 2 (source)
    • Show

      #show: psc-report.with(
        $if(title)$
          title: "$title$",
        $endif$
        $if(params.town)$
          town: "$params.town$",
        $endif$
      )
      • Sets variables for title metadata and town param from quarto YAML
    • Template

      Code

      #let psc-report(
        title: "title",
        town: "town",
        body,
      ) = {
      
        // text style for whole report
        set text(
         font: "Open Sans",
         size: 12pt,
        )
      
      
        set page(
      
          // dimensions of document
          "us-letter", // or "a4"
          margin: (left: 1in, 
                   right: 1in, 
                   top: 0.7in, 
                   bottom: 1in),
      
          // places blue rectangle in header
          background: 
            place(top, 
                  rect(fill: rgb("15397F"), // hex-code for color
                       width: 100%, 
                       height: 0.5in
                  )
            ),
      
          // creates layout for text in header rectangle
          header: align(
            horizon,
            grid(
              columns: (80%, 20%), // div sizes for title, town text
              // title text properties
              align(left, 
                    text(size: 20pt, 
                         fill: white, 
                         weight: "bold", 
                         title)),
              // town text properties
              align(right, 
                    text(size: 12pt, 
                         fill: white, 
                         weight: "bold", 
                         town)
              )
            )
          ),
      
          // page number, image
          footer: align(
            grid(
              columns: (40%, 60%), // page number, graphic
              align(horizon, 
                    text(fill: rgb("15397F"), 
                    size: 12pt, 
                    counter(page).display("1"))), // sets page number
              align(right, 
                    image("assets/psclogo.svg", 
                          height: 300%)
              )
            )
          )
      
       body
      }
      • The function , psc-report, arguments set the variable values (from the show file) for title and town
        • Variables seen in the header section
      • Sections
        • set text
        • set page
          • margin, background, header, footer
        • body
      • Functions: text, page, place, grid, align

Extensions

  • Installing the extension downloads a .typ file with some styling
  • Usage
    • Example: dept-news extension

      ---
      title: "Chemistry Department"
      format:
        dept-news-typst: default
      ---