/
tikz2svg
executable file
·25 lines (23 loc) · 949 Bytes
/
tikz2svg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/usr/bin/env bash
# tikz2svg converts a tikz document to SVG, via PDF.
#
# Uses:
# + xelatex (though it should be easy to sub in pdflatex) and
# + pdf2svg: https://formulae.brew.sh/formula/pdf2svg
#
# Usage: tikz2svg [someTikzFile.tex]
# Example input: https://gist.github.com/lukasschwab/6a3e2cdd78075e8923a9057f7609a898
#
# See also: https://pandoc.org/lua-filters.html#building-images-with-tikz
# Extract filename before final extension.
filename="${1%.*}"
# Generate intermediate PDF in temporary directory.
intermediate="tikz2svg$$.$filename"
xelatex -interaction=batchmode -output-directory="/tmp" -jobname="$intermediate" "$1"
# Generate SVG from intermediate PDF.
pdf2svg "/tmp/$intermediate.pdf" "$filename.svg"
# Clean up intermediates.
rm /tmp/$intermediate*
# Rewrite IDs to prevent collisions between generated SVGs.
sed -i '' "s/ id=\"/ id=\"$filename-/g; s/\"#/\"#$filename-/g" "$filename.svg"
blog info "Wrote $filename.svg"