---
title: TikZ
pubDate: 2025-02-06T19:00:00Z
description: Converting TikZ graphics into svg graphics.
---
At some point a man has to realize that they went down a rabbit-hole without any way to escape it. TikZ is one such rabbit-hole and, yes, I am that man.

In broad terms [TikZ](https://tikz.dev/) is a programming language within [LaTeX](https://www.latex-project.org/), to generate graphics from code.

# Standalone TikZ
The [standalone package](https://ctan.org/pkg/standalone) for LaTeX is inteded as a way to put graphics such as those generated by TikZ into a standalone document to be included in other documents. We are going to use it to generate a pdf with the TikZ graphic.
```latex
% graphic.tex
\documentclass[border=0pt, tikz]{standalone}

\begin{document}
  \begin{tikzpicture}
  
  \end{tikzpicture}
\end{document}
```

To compile we simply run:
```shell
pdflatex graphic.tex
```

# Convert to SVG
To convert this pdf into a svg file we are going to use the [pdf2svg](https://cityinthesky.co.uk/opensource/pdf2svg/) command-line converter:
```shell
pdf2svg graphic.tex
```
Et voilà! We produced an svg file from TikZ Source Code.

# Quick and Easy
Consider the following script for quick compilation:
```bash
#!/bin/bash

# check for input file
input="$1"

if [ -z "$input" ]; then
  echo "Usage: $0 <input>"
  exit 1
fi

if [ ! -f "$1.tex" ]; then
  echo "File not found: $1.tex"
  exit 1
fi

# compile
echo "Compiling $1.tex..."
pdflatex -interaction=batchmode "$1.tex" > /dev/null

if [ $? -ne 0 ]; then
  echo "Error: pdflatex failed!"
  exit 1
fi

echo "Converting to $1.svg..."
pdf2svg "$1.pdf" "$1.svg"

echo "Done!"

# cleanup
rm "$1.aux" "$1.log"
```
