-
Notifications
You must be signed in to change notification settings - Fork 10
/
tikz2png
executable file
·111 lines (98 loc) · 4.06 KB
/
tikz2png
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/bin/bash
#
# This script converts TikZ images to PNG.
# It needs
# * pdflatex with packages tikz, standalone
# * imagemagick
#
# Copyright 2013 Raphael Reitzig
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
# Call with a list of filenames, e.g.
# tikz2png a.tikz b.tikz c.tikz
# Each file will be augmented so it can be translated into a PDF using
# pdflatex and the LaTeX package standalone. The PDF is then converted
# to PNG with ImageMagick; you can easily adapt the script to output other
# formats. In case something goes wrong, you find a log file in the tmp
# directory (see corresponding setting).
#
# Input files need to have the following format (aside from comment lines):
#
# \begin{tikzpicture}[<parameters>]
# <content>
# \end{tikzpicture}
#
# If you need additional packages for an image, you can add lines of the form
#
# %p% <preamblematter>
#
# whose content is added to the augmented document's preamble.
#
# A simple example is this:
#
# %p% \usetikzlibrary{arrows,automata,positioning}
# \begin{tikzpicture}[shorten >=1pt,node distance=2cm,auto]
# \node[state,initial] (q_0) {$q_0$};
# \node[state,accepting] (q_1) [right of=q_0] {$q_1$};
# \path[->] (q_0) edge [bend left] node {$a$} (q_1)
# (q_1) edge [bend left] node {$b$} (q_0);
# \end{tikzpicture}
#
# If standalone is not part of your LaTeX installation, install it using your distribution's tools.
# If that is not an option, get it on CTAN (http://ctan.org/pkg/standalone) and install manually.
# If you can not install it into LaTeX folders, install it somewhere accessible and set up variable
# `standalonepath` below accordingly; uncomment line 85, too.
# Some parameters
tmp="/tmp"; # Directory where intermediate files are put. Not all may be cleaned up.
#standalonepath="/path/to/standalone"; # Path to a directory
latex_preamble=""; # Code you want to be always included in the LaTeX preamble
tikz_border="2bp"; # standalone class
tikz_scale="0.1"; # tikzset style
png_density="1200"; # convert -density
png_quality="100"; # convert -quality
png_scale="12%"; # mogrify -resize
if [ ${#} == 0 ];
then
echo "Please provide a file name as parameter.";
fi
for f in ${*};
do
echo -n "Processing ${f} ...";
preamble="${latex_preamble}\n`cat ${f} | grep -E "^[[:space:]]*%p%" | sed 's/^[[:space:]]*...//'`";
echo -e "\documentclass[border=${tikz_border}]{standalone}\n\usepackage{tikz}${preamble}\\\\begin{document}" > ${tmp}/${f%.tikz}.tex;
#echo "\begingroup\tikzset{every picture/.style={scale=${tikz_scale}}}%" >> ${tmp}/${f%.tikz}.tex;
cat ${f} | grep -v "^[[:space:]]*%p%" >> ${tmp}/${f%.tikz}.tex;
echo "\endgroup\end{document}" >> ${tmp}/${f%.tikz}.tex;
# Uncomment if standalone is not available. Make sure to configure the path above.
# cp ${standalonepath}/* ${tmp}/;
if [ ! -e ${tmp}/${f%.tikz}_tmp ]
then
mkdir ${tmp}/${f%.tikz}_tmp;
fi
dir=`pwd`;
cd ${tmp};
pdflatex --shell-escape -file-line-error -interaction=nonstopmode -output-directory=${f%.tikz}_tmp ${f%.tikz}.tex > tikz2png_${f%.tikz}_log;
cd "${dir}";
if [ -e ${tmp}/${f%.tikz}_tmp/${f%.tikz}.pdf ];
then
convert -density ${png_density} ${tmp}/${f%.tikz}_tmp/${f%.tikz}.pdf -quality ${png_quality} ${tmp}/${f%.tikz}.png;
mogrify -resize ${png_scale} ${tmp}/${f%.tikz}.png;
mv ${tmp}/${f%.tikz}.png ./
echo " Done.";
else
echo " Error. See ${tmp}/tikz2png_${f%.tikz}_log";
fi
rm ${tmp}/${f%.tikz}.tex &> /dev/null;
rm -rf ${tmp}/${f%.tikz}_tmp &> /dev/null;
done;