# -*- coding:utf-8 -*- import os import optparse ######################################################################## ## Check options ######################################################################## def checkOption(opt): if opt == None: parser.print_help() exit() skeleton = """ \\documentclass[11pt,a4paper]{report} \\pagestyle{empty} \\begin{document} \\%s{$%s$} \\end{document} """ parser = optparse.OptionParser() parser.add_option("-t", "--tex", dest="tex", help="tex format", metavar="[TEX]") parser.add_option("-s", "--size", dest="size", help="Font size (0-9)", default="4", metavar="[SIZE]") parser.add_option("-o", "--output", dest="out", help="output file name prefix", metavar="[OUT]") (options, args) = parser.parse_args() checkOption(options.tex) checkOption(options.size) checkOption(options.out) print "tex : %s" % (options.tex) print "size : %s" % (options.size) print "output: %s" % (options.out) # フォントサイズを決める if options.size == "0": fontsize = "tiny" elif options.size == "1": fontsize = "scriptsize" elif options.size == "2": fontsize = "footnotesize" elif options.size == "3": fontsize = "small" elif options.size == "4": fontsize = "normalsize" elif options.size == "5": fontsize = "large" elif options.size == "6": fontsize = "Large" elif options.size == "7": fontsize = "LARGE" elif options.size == "8": fontsize = "huge" elif options.size == "9": fontsize = "Huge" else: fontsize = "normalsize" texdata = skeleton % (fontsize, options.tex) print texdata outputBase = options.out texFilename = outputBase + ".tex" dviFilename = outputBase + ".dvi" pngFilename = outputBase + ".png" fd = open(texFilename, "w") fd.write(texdata) fd.close() os.system("platex %s" % (texFilename)) os.system("dvipng %s -o %s -T tight" % (dviFilename, pngFilename)) print "Remove temporary files" print " %s, %s, %s, %s" % (texFilename, dviFilename, outputBase + ".log", outputBase + "aux") os.remove(texFilename) os.remove(dviFilename) os.remove(outputBase + ".log") os.remove(outputBase + ".aux")
実行結果
python tex2png.py -t "y=ax^{2}" -o y_ax2
python tex2png.py -t "y=ax^{2}" -s 1 -o y_ax2_1
python tex2png.py -t "y=ax^{2}" -s 9 -o y_ax2_9
0 件のコメント:
コメントを投稿