Skip to content

Instantly share code, notes, and snippets.

@AnonymerNiklasistanonym
Last active September 28, 2024 03:30
Show Gist options
  • Save AnonymerNiklasistanonym/c722b298b6528c698366f7757bee635d to your computer and use it in GitHub Desktop.
Save AnonymerNiklasistanonym/c722b298b6528c698366f7757bee635d to your computer and use it in GitHub Desktop.
Render a code file to a with pdflatex and minted syntax highlighted and with inkscape from PDF converted SVG file
#!/usr/bin/env bash
# Usage: ./render_code_to_svg.sh hello_world.c
# -> Creates hello_world.svg
CODE_FILE="$1"
CODE_FILE_EXTENSION="${CODE_FILE##*.}"
CODE_FILE_OUT="${CODE_FILE%.${CODE_FILE_EXTENSION}}.svg"
BUILD_DIR=build
if [[ $# -eq 0 ]] ; then
echo "No code file argument was provided!"
exit 1
fi
if [ ! -f "$CODE_FILE" ]; then
echo "Code file not found!"
exit 1
fi
echo "Render '$CODE_FILE' to '$CODE_FILE_OUT' (pdflatex+minted{$CODE_FILE_EXTENSION}, inkscape)"
mkdir -p "$BUILD_DIR"
echo "\documentclass[
border={0.7cm 0.15cm 0 0.15cm}, %left bottom right top (exists so that the linenos show up)
]{standalone}
\usepackage[outputdir=$BUILD_DIR]{minted}
\usepackage{varwidth}
\begin{document}
\begin{varwidth}{\linewidth}
\begin{minted}[
fontsize=\footnotesize,
linenos=true
]{$CODE_FILE_EXTENSION}" > "$BUILD_DIR/code.tex"
cat "$CODE_FILE" >> "$BUILD_DIR/code.tex"
echo "\end{minted}
\end{varwidth}
\end{document}" >> "$BUILD_DIR/code.tex"
echo "------------------"
cat "$BUILD_DIR/code.tex"
echo "------------------"
pdflatex -shell-escape -output-directory="$BUILD_DIR" "$BUILD_DIR/code.tex"
inkscape -o "$CODE_FILE_OUT" --export-type=svg --export-text-to-path "$BUILD_DIR/code.pdf"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment