6 # http://www.fourmilab.ch/
8 $version = '1.1 (2003-11-07)';
11 # Converts a LaTeX file containing equations(s) into a GIF file for
12 # embedding into an HTML document. The black and white image of the
13 # equation is created at high resolution and then resampled to the
14 # target resolution to antialias what would otherwise be jagged
17 # Online documentation with sample output is available on the Web
18 # at http://www.fourmilab.ch/webtools/textogif/
20 # Write your equation (or anything else you can typeset with LaTeX)
23 # \documentclass[12pt]{article}
28 # \bf % Compiled formulae often look better in boldface
29 # \int H(x,x')\psi(x')dx' = -\frac{\hbar2}{2m}\frac{d2}{dx2}
35 # The "\pagestyle{empty}" is required to avoid generating a huge
36 # image with a page number at the bottom.
38 # Then (assuming you have all the software described below installed
39 # properly), you can simply say:
41 # textogif [options] filename ...
43 # to compile filename.tex to filename.gif, an interlaced,
44 # transparent background GIF file ready to use an an inline image.
45 # You can specify the base name, for example, "schrod", rather than
46 # the full name of the TeX file ("schrod.tex"). TeX requires the
47 # input file to have an extension of ".tex". The command line
48 # options are described in the help text at the end of this program
49 # and in the "Default Configuration" section below.
51 # A sample IMG tag, including the image width and height is printed
52 # on standard error, for example:
54 # <img src="schrod.gif" width=508 height=56>
58 # This script requires the following software to be installed
59 # in the standard manner. Version numbers are those used in the
60 # development and testing of the script.
62 # Perl 5.8.0 (anything later than 4.036 should work)
63 # TeX 3.14159 (Web2C 7.3.1)
64 # LaTeX2e <2000/06/01>
66 # Ghostscript 6.52 (2001-10-20)
70 # Default Configuration
72 # The following settings are the defaults used if the -dpi and
73 # -res options are not specified on the command line.
75 # The parameter $dpi controls how large the equation will appear
76 # with respect to other inline images and the surrounding text.
77 # The parameter is expressed in "dots per inch" in the PostScript
78 # sense. Unfortunately, since there's no standard text size in
79 # Web browsers (and most allow the user to change fonts and
80 # point sizes), there's no "right" value for this setting. The
81 # default of 150 seems about right for most documents. A setting
82 # of 75 generates equations at half the normal size, while 300
83 # doubles the size of equations. The setting of $dpi can always be
84 # overridden by specifying the "-dpi" command line option.
88 # The parameter $res specifies the oversampling as the ratio
89 # of the final image size to the initial black and white image.
90 # Smaller values produce smoothing with more levels of grey but
91 # require (much) more memory and intermediate file space to create
92 # the image. If you run out of memory or disc space with the
93 # default value of 0.5, try changing it to 0.75. A $res setting of
94 # 1.0 disables antialiasing entirely. The setting of $res can
95 # always be overridden by specifying the "res" command line option.
99 # The $background parameter supplies a command, which may be
100 # void, to be inserted in the image processing pipeline to
101 # adjust the original black-on-white image so that its background
102 # agrees with that of the document in which it is to be inserted.
103 # For a document with the default grey background used by Mosaic
104 # and old versions of Netscape, use:
106 # $background = "ppmdim 0.7 |"; $transparent = "b2/b2/b2";
108 # If your document uses a white background, the void specification:
110 # $background = ""; $transparent = "ff/ff/ff";
112 # should be used. For colour or pattern backgrounds, you'll have
113 # to hack the code. The reason for adjusting the background is to
114 # ensure that when the image is resampled and then output with a
115 # transparent background the edges of the characters will fade
116 # smoothly into the page background. Otherwise you'll get a
117 # distracting "halo" around each character. You can override this
118 # default specification with the -grey command line option.
120 $background = ""; $transparent = "ff/ff/ff";
122 # Image generation and decoding commands for GIF and PNG output.
124 $cmdGIF = 'ppmtogif';
125 $cmdGIFdecode = 'giftopnm';
126 $cmdPNG = 'pnmtopng';
127 $cmdPNGdecode = 'pngtopnm';
129 # Default image creation modes
132 $imageCmdD = $cmdGIFdecode;
136 # Command line option processing
138 while ($ARGV[0] =~ m/^-/) {
140 s/^--/-/; # Allow GNU-style -- options
141 if (m/^-d/) { # -dpi nnn
143 } elsif (m/^-gi/) { # -gif
145 $imageCmdD = $cmdGIFdecode;
147 } elsif (m/^-gr/) { # -grey n
148 $grey = shift(@ARGV);
149 $background = "ppmdim $grey | ";
150 $greylev = int(255 * $grey);
151 $transparent = sprintf("%02x/%02x/%02x", $greylev, $greylev, $greylev);
152 } elsif (m/^-h/) { # -help
155 } elsif (m/^-p/) { # -png
157 $imageCmdD = $cmdPNGdecode;
159 } elsif (m/^-r/) { # -res nnn
161 } elsif (m/^-v/) { # -version
162 print("Version $version\n");
167 # Main file processing loop
170 $f =~ s/(.*)\.tex$/$1/;
171 &syscmd("echo x | latex $f \n");
172 &syscmd("dvips -f $f >_temp_$$.ps\n");
174 # Assemble and execute the command pipeline which generates the image.
176 # Start by invoking Ghostscript with the pbmraw output device and
177 # output file set to standard output ("-") and the requested resolution.
178 # The -q (Quiet) option is required; otherwise Ghostscript will send
179 # processing information to standard output and corrupt transmission
180 # of the bitmap to the next component in the pipeline.
181 $cmd = "echo quit | gs -q -dNOPAUSE -r" . int($dpi / $res). "x". int($dpi / $res) .
182 " -sOutputFile=- -sDEVICE=ppmraw _temp_$$.ps | " .
184 # Next we crop white space surrounding the generated text, promote
185 # the monochrome bitmap to a grey scale image with 8 bits per pixel,
186 # apply whatever background adjustment transform is requested, and
187 # scale the image to the desired size.
188 "pnmcrop -white | pnmpad -white -left " . int(10/$res) .
189 " -right " . int(10/$res) . " -top " . int(10/$res) .
190 " -bottom " . int(10/$res) . " | " .
191 " $background pnmscale " . $res . " | " .
193 # Finally, convert the image to the desired output format and write
195 "$imageCmd -interlace -transparent rgb:$transparent >$f.$imageExt";
198 # Sweep up debris left around by the various intermediate steps
199 &syscmd("rm $f.dvi $f.aux $f.log _temp_$$.ps");
201 # Print the reference to include this figure, including width and height,
203 $r = `$imageCmdD $f.$imageExt | pnmfile`;
204 $r =~ m/(\d+) by (\d+)/;
205 print(STDERR "<img src=\"$f.$imageExt\" width=\"$1\" height=\"$2\">\n");
208 # Echo and execute a system command
213 print(STDERR "$cmd\n");
214 system($cmd) == 0 || die("Error processing command:\n\t$cmd\n\t");
221 usage: textogif [ options ] texfile...
223 -dpi n Set rendering dots per inch to n (default 150)
224 -gif Generate GIF image (default)
225 -grey Grey scale background level: 0 = black, 1 = white (default)
226 -help Print this message
227 -png Generate PNG image
228 -res n Set oversampling ratio, smaller = finer (default 0.5)
229 -version Print version number
230 For documentation and the latest version of this program
231 please visit the Web page:
232 http://www.fourmilab.ch/webtools/textogif/