weix.us

Inlining typst files

I used to maintain my CV in LaTeX, but recently switched over to using Typst. In both programs, I keep a separate file for each section: education, publications, experience, etc. While this is super useful for an archival CV, it makes it difficult to edit and tweak for specific applications.

To solve this problem, I wrote a script that searches a typst file for all instances of the #include directive and replaces it with the contents of the included file. This produces a source .typ that can then be freely edited:

#!/bin/bash
TEMPLATE="$1"
NEWFILE="$2"

# read through the file
while IFS='' read -r line || [[ -n "$line" ]]; do
  if [[ "$line" == "#include"* ]]; then
    # if we find a string with "#include" then inline the file
    cat "$(echo "$line" | sed 's/#include//g; s/\"//g; s/\ //g;')"
    echo
  else
    # otherwise just repeat the line
    echo "$line"
  fi
done < $TEMPLATE | tee $NEWFILE

This script can be used like so:

$ /bin/bash inline.sh input.typ output.typ

Posted by Elliott Weix.