[Tutor] Python Tool for converting TeX to GIF

Stephan Richter srichter@cbu.edu
Wed, 05 Sep 2001 00:21:41 -0500


>There's textogif:
>
>   http://www.fourmilab.ch/webtools/textogif/textogif.html
>
>but it has a lot of requirements. I couldn't find anything else for TeX ->
>GIF.

Yeah, I had seen this one before. But I figured out that the script can be 
written quickly (see code below) in Python. I really do not like it. The 
code is ugly and some features are not working, but it is a proof of 
concept which I will turn into a class now... ;-)

>You can also have a look at MathML. There are only a couple of browsers that
>support it, but there are some MathML -> GIF converters out there.

I am just not one of the XML fans and the professors using this tool know 
Latex already.

Regards,
Stephan

Code for tex2gif.py

import sys, getopt, string

usage = '''
Usage:
$COMNAME [options...] file.tex

Description:
$COMNAME converts a *.tex file into one GIF image file.
Current Version is ${VERSION_MAJOR}.${VERSION_MINOR}

Options :
-p --detail_ratio          [default = ${DETAIL_DEFAULT}]
-d --dpi pix_per_inch      [default = ${DPI_DEFAULT}]
-m --margin margin_width   [default = ${MARGIN_DEFAULT}]
-r --rotate angle          [default = ${ROTATE_DEFAULT} (counterclockwise 
in deg)]
-t --transparent colorname [default no transparent color set]

--usage | --help : show this message
'''

command = '''
latex %(file)s.tex;
dvips  -q -f %(file)s.dvi > %(file)s.ps
gs -sDEVICE=ppmraw \
    -sOutputFile=- \
    -g%(xSize)sx%(ySize)s \
    -r%(workDPI)s \
    -q -dNOPAUSE %(file)s.ps < /dev/null  | \
pnmscale %(reductionRatio)s | \
pnmcrop -white  | \
pnmmargin -white %(margin)s | \
ppmquant 256 | \
ppmtogif -interlace %(transparentOption)s - \
 > %(file)s.gif

rm -f %(file)s.log %(file)s.aux %(file)s.dvi %(file)s.ps
'''


detailRatio = 3.0
dpi         = 78
margin      = 1
rotate      = 0
transparent = 2
xSizeInch   = 12
ySizeInch   = 12
file        = ''

opts, args = getopt.getopt(sys.argv[1:],
                            'p:d:m:r:t:h:u',
                            ['detail_ratio=', 'dpi=', 'margin=', 'rotate=', 
'transparent=', 'usage', 'help'])

file = string.split(args[0], '.')[0]

for opt, arg in opts:
     if opt in ('-h', '--help', '-u', '--usage'):
         print usage
         sys.exit()
     if opt in ("-p", "--detail_ratio"):
         detailRatio = float(arg)
     if opt in ("-d", "--dpi"):
         dpi = float(arg)
     if opt in ("-m", "--margin"):
         margin = float(arg)
     if opt in ("-r", "--rotate"):
         rotate = int(arg)
     if opt in ("-t", "--transparent"):
         transparent = int(arg)


def tex2gif():

     fillIn = globals()
     fillIn['workDPI'] = detailRatio*dpi
     fillIn['reductionRatio'] = 1.0/detailRatio
     fillIn['xSize'] = int(fillIn['workDPI'] * xSizeInch)
     fillIn['ySize'] = int(fillIn['workDPI'] * ySizeInch)
     fillIn['transparentOption'] = ''

     import popen2
     print popen2.popen2(command %fillIn)[0].read()


tex2gif()



--
Stephan Richter
CBU - Physics and Chemistry Student
Web2k - Web Design/Development & Technical Project Management