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:

 1#!/bin/bash
 2TEMPLATE="$1"
 3NEWFILE="$2"
 4
 5# read through the file
 6while IFS='' read -r line || [[ -n "$line" ]]; do
 7  if [[ "$line" == "#include"* ]]; then
 8    # if we find a string with "#include" then inline the file
 9    cat "$(echo "$line" | sed 's/#include//g; s/\"//g; s/\ //g;')"
10    echo
11  else
12    # otherwise just repeat the line
13    echo "$line"
14  fi
15done < $TEMPLATE | tee $NEWFILE

This script can be used like so:

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

Updated by Elliott Weix.