]> git.rmz.io Git - dotfiles.git/blob - bin/tex2im
vim: do not set pastetoggle in nvim
[dotfiles.git] / bin / tex2im
1 #!/bin/bash
2 #############################################################
3 # TEX2IM: Converts LaTeX formulas to pixel graphics which #
4 # can be easily included in Text-Processors like #
5 # M$ or Staroffice. #
6 # #
7 # Required software: latex, convert (image magic) #
8 # to get color, the latex color package is required #
9 #############################################################
10 # Version 1.8 (http://www.nought.de/tex2im.html) #
11 # published under the GNU public licence (GPL) #
12 # (c) 14. May 2004 by Andreas Reigber #
13 # Email: anderl@nought.de #
14 #############################################################
15
16 #
17 # Default values
18 #
19
20 resolution="150x150"
21 format="png"
22 color1="white"
23 color2="black"
24 trans=0
25 noformula=0
26 aa=1
27 extra_header="$HOME/.tex2im_header"
28
29 if [ -f ~/.tex2imrc ]; then
30 source ~/.tex2imrc
31 fi
32
33 OPTERR=0
34
35 if [ $# -lt 1 ]; then
36 echo "Usage: `basename $0` [options] file.tex, for help give option -h" 1>&2
37 exit 1
38 fi
39
40 while getopts hanzb:t:f:o:r:vx: Optionen; do
41 case $Optionen in
42 h) echo "tex2im [options] inputfile
43
44 The content of input file should be _plain_ latex mathmode code!
45 Alternatively, a string containing the latex code can be specified.
46
47 Options:
48 -v show version
49 -h show help
50 -a change status of antialiasing
51 default is on for normal mode and
52 off for transparent mode
53 -o file specifies output filename,
54 default is inputfile with new extension
55 -f expr specifies output format,
56 possible examples: gif, jpg, tif......
57 all formates supported by "convert" should work,
58 default: png
59 -r expr specifies desired resolution in dpi,
60 possible examples: 100x100, 300x300, 200x150,
61 default is 150x150
62 -b expr specifies the background color
63 default: white
64 -t expr specifies the text color
65 default: black
66 -n no-formula mode (do not wrap in eqnarray* environment)
67 default: off
68 -z transparent background
69 default: off
70 -x file file containing extra header lines.
71 default: ~/.tex2im_header"
72 exit 0 ;;
73 v) echo "TEX2IM Version 1.8"
74 exit 0 ;;
75 r) resolution=$OPTARG;;
76 o) outfile=$OPTARG;;
77 z) trans=1
78 aa=0;;
79 a) if [ $aa -eq 0 ]; then
80 aa=1
81 else
82 aa=0
83 fi;;
84 n) noformula=1;;
85 b) color1=$OPTARG;;
86 t) color2=$OPTARG;;
87 f) format=$OPTARG;;
88 x) extra_header=$OPTARG;;
89 esac
90 done
91
92 #
93 # Generate temporary directory
94 #
95
96 if test -n "`type -p mktemp`" ; then
97 tmpdir="`mktemp /tmp/tex2imXXXXXX`"
98 rm $tmpdir
99 mkdir $tmpdir
100 else
101 tmpdir=/tmp/tex2im$$
102 if [ -e $tmpdir ] ; then
103 echo "$0: Temporary directory $tmpdir already exists." 1>&2
104 exit 1
105 fi
106 mkdir $tmpdir
107 fi
108 homedir="`pwd`" || exit 1
109
110 #
111 # Names for input and output files
112 #
113
114 while [ $OPTIND -le $# ]
115 do
116
117 eval infile=\$${OPTIND}
118
119 if [ -z $outfile ]; then
120 if [ -e "$infile" ]; then
121 base=`basename ${infile} .tex` ;
122 outfile=${base}.$format
123 else
124 outfile=out.$format
125 fi
126 fi
127
128 #
129 # Here we go
130 #
131
132 (
133 cat << ENDHEADER1
134 \documentclass[12pt]{article}
135 \usepackage{color}
136 \usepackage[dvips]{graphicx}
137 \pagestyle{empty}
138 ENDHEADER1
139 ) > $tmpdir/out.tex
140
141 #
142 # Do we have a file containing extra files to include into the header?
143 #
144
145 if [ -f $extra_header ]; then
146 (
147 cat $extra_header
148 ) >> $tmpdir/out.tex
149 fi
150
151 if [ $noformula -eq 1 ]; then
152 (
153 cat << ENDHEADER2
154 \pagecolor{$color1}
155 \begin{document}
156 {\color{$color2}
157 ENDHEADER2
158 ) >> $tmpdir/out.tex
159 else
160 (
161 cat << ENDHEADER2
162 \pagecolor{$color1}
163 \begin{document}
164 {\color{$color2}
165 \begin{eqnarray*}
166 ENDHEADER2
167 ) >> $tmpdir/out.tex
168 fi
169
170 if [ -e "$infile" ]; then
171 cat $infile >> $tmpdir/out.tex
172 else
173 echo $infile >> $tmpdir/out.tex
174 fi
175
176 if [ $noformula -eq 1 ]; then
177 (
178 cat << ENDFOOTER
179 }\end{document}
180 ENDFOOTER
181 ) >> $tmpdir/out.tex
182 else
183 (
184 cat << ENDFOOTER
185 \end{eqnarray*}}
186 \end{document}
187 ENDFOOTER
188 ) >> $tmpdir/out.tex
189 fi
190
191 cd $tmpdir
192 for f in $homedir/*.eps; do
193 test -f ${f##*/} || ln -s $f . # multi-processing!
194 done
195 latex -interaction=batchmode out.tex > /dev/null
196 cd "$homedir"
197 dvips -o $tmpdir/out.eps -E $tmpdir/out.dvi 2> /dev/null
198
199 #
200 # Transparent background
201 #
202
203 if [ $trans -eq 1 ]; then
204 if [ $aa -eq 1 ]; then
205 convert +adjoin -antialias -transparent $color1 -density $resolution $tmpdir/out.eps $tmpdir/out.$format
206 else
207 convert +adjoin +antialias -transparent $color1 -density $resolution $tmpdir/out.eps $tmpdir/out.$format
208 fi
209 else
210 if [ $aa -eq 1 ]; then
211 convert +adjoin -antialias -density $resolution $tmpdir/out.eps $tmpdir/out.$format
212 else
213 convert +adjoin +antialias -density $resolution $tmpdir/out.eps $tmpdir/out.$format
214 fi
215 fi
216
217
218 if [ -e $tmpdir/out.$format ]; then
219 mv $tmpdir/out.$format $outfile
220 else
221 mv $tmpdir/out.$format.0 $outfile
222 fi
223
224 let OPTIND=$OPTIND+1
225 outfile=""
226 done
227
228 #
229 # Cleanup
230 #
231
232 rm -rf $tmpdir
233 exit 0