tikz2png

Create png image from TikZ code. Useful for quickly viewing TikZ graphics. The script is in large part a collection of code I found in the WWW.

#!/bin/bash

## usage: tikz2png file.tex

## Remove path and ".tex" from end of first argument.
FILE=`basename ${1%.tex}`

## We cd into the destination directory in order to avoid problems
## with pdflatex when the tmp.tex file includes a file with path.
cd `dirname $1`

## Create .tex input file.
echo "\documentclass{article}\usepackage[utf8]{inputenc}\usepackage{tikz,amsmath}\usetikzlibrary{arrows,backgrounds,patterns,matrix,shapes,fit,calc,shadows,plotmarks}\usepackage[graphics,tightpage,active]{preview}\PreviewEnvironment{tikzpicture}\PreviewEnvironment{equation}\PreviewEnvironment{equation*}\newlength{\imagewidth}\newlength{\imagescale}\pagestyle{empty}\thispagestyle{empty}\begin{document}\include{$FILE}\end{document}" > tmp.tex

## Create pdf file.
pdflatex tmp

## Use supersampling so that text is antialiased.
## density 600=2*300 where 300 is nominal density resized down by 50%.
## This takes longer: -density 1200 tmp.pdf -resize 25%.
convert -density 600 tmp.pdf -resize 50% $FILE.png

## Tidy up.
rm tmp.*
rm $FILE.aux

# Return to original directory
cd -

Changelog

  • 2011-04-28: Added the directory change and use of "basename". This is needed if called with path to some other directory, e.g. "tikz2png ../pics/mydiagram.tex".
  • 2011-03-05: First published version
Zuletzt geändert am 28.04.2011 17:26 Uhr