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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
   |  
"""A simple tool for embedding LaTeX in XHTML documents.
 
This script lets you embed LaTeX code between <div> and <span> tags. Example:
    <div class="eq>
      y = \int_0^\infty \gamma^2 \cos(x) dx 
    </div>
    <p> An inline equation <span class="eq">y^2=x^2+\alpha^2</span> here.</p>
 
The script extracts the equations, creates a temporary LaTeX document, 
compiles it, saves the equations as images and replaces the original markup 
with images.
 
Usage:
    python eqhtml.py source dest
    
Process source and save result in dest. Note that no error checking is 
performed. 
"""
 
import os, sys
 
# Include your favourite LaTeX packages and commands here
tex_preamble = r'''
\documentclass{article} 
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amssymb}
\usepackage{bm}
\newcommand{\mx}[1]{\mathbf{\bm{#1}}} % Matrix command
\newcommand{\vc}[1]{\mathbf{\bm{#1}}} % Vector command 
\newcommand{\T}{\text{T}}                % Transpose
\pagestyle{empty} 
\begin{document} 
'''
 
imgpath = 'equation/' # path to generated equations. e.q 'img/'
 
# get source and dest filenames from command line
sourcefn = sys.argv[1]
destfn = sys.argv[2]
sourcefn_base = os.path.splitext(os.path.basename(sourcefn))[0]
# change working directory to the same as source's
cwd = os.getcwd()
os.chdir(os.path.abspath(os.path.dirname(sourcefn)))
sourcefn = os.path.basename(sourcefn)
texfn = sourcefn_base+'.tex'
 
print "Processing %s" % sourcefn
# load source document
f = open(sourcefn)
docphp = f.readlines();
f.close()
 
# find all elements with attribute class='eq'
# eqs will contain all the equations
eqs = []
chaine_debut = '<div class="eq">'
chaine_fin = '</div>'
lecture_equation = 0
# new lines will be written in newdoc_php
newdoc_php = []
counter = 0
for ligne in docphp:
    if chaine_debut in ligne:
        # beginning of an equation
        lecture_equation = 1
        counter = counter+1
        # delete LaTeX code from the document, and replace
        # them by image urls.
        imgname = "%seq%s%i.png" % (imgpath, sourcefn_base, counter)
        ligne_image = '<div class="eq"><img alt="" src="%s" /></div>\n' % (imgname)
        newdoc_php.append(ligne_image)
        # initialization of eq (on the first line, no latex symbols)
        eq = ""   
    elif (lecture_equation == 1):
        if chaine_fin in ligne:
            # end of the equation
            index = ligne.find(chaine_fin);
            eq = eq + (ligne[0:index]);
            lecture_equation = 0
            eqs.append(eq)
        else:
            eq = eq + ligne
    else:
        newdoc_php.append(ligne)
 
 
# equations are now available in the eqs[..] variable
 
# create a LaTeX document and insert equations
f = open(texfn,'w')
f.write(tex_preamble)
counter = 1
for eq in eqs:
    f.write("\\[ %s \\] \n \\newpage \n" % eq)
    counter += 1
# end LaTeX document    
f.write('\end{document}')
f.close()
 
# compile LaTeX document. A DVI file is created
os.system('latex %s' % texfn)
 
# Run dvipng on the generated DVI file. Use tight bounding box. 
# Magnification is set to 1200
cmd = "dvipng -T tight -x 1200 -z 9 -bg transparent " \
+ "-o %seq%s%%d.png %s" % (imgpath , sourcefn_base, sourcefn_base)
os.system(cmd) 
 
 
 
# Remove temporary files
os.remove(sourcefn_base+'.tex')
os.remove(sourcefn_base+'.log')
os.remove(sourcefn_base+'.aux')
os.remove(sourcefn_base+'.dvi')
 
os.chdir(cwd)
 
# Write processed source document to dest 
f = open(destfn,'w')
for ligne in newdoc_php:
    f.write(ligne)
f.close()
 
 
print "Done." | 
Partager