Nutzerdefinierte LaTeX Symbole rendern für Kile

Stellt hier eure Projekte vor.
Internetseiten, Skripte, und alles andere bzgl. Python.
Antworten
bastian.weber
User
Beiträge: 15
Registriert: Montag 4. August 2008, 23:30
Wohnort: Dresden

Hatte gestern das Bedürfnis, häufig genutzte eigene Symbole im KDE-LaTeX-Editor Kile bequem einfügen zu können. Der offizielle Weg geht wohl über ein Programm namens gesymb, aber da ich gerade wenig Platz auf der Platte habe wollte ich nicht ewig viel Zeug installieren, nur um gesymb zu kompilieren.

Kile braucht eigentlich nur für jedes Symbol eine png-Datei. Den einzufügenden LaTeX-Code ließt es aus den Meta-Daten dieser png-Datei.

Da ich vor kurzen ein bisschen mit Inkscape und seinen Python-Erweiterungen rumgespielt hatte, schien mir eine Python-Lösung recht machbar. Ich habe eine Version, die meinen Bedarf befriedigt, auch innerhalb von etwas über einer Stunde hinbekommen.

Falls noch andere ähnliche Bedürfnisse haben... vielleicht nützt es jemandem:


Code: Alles auswählen

#! /usr/bin/env python
# -*- coding: utf8 -*-
import os
import sys

#from ipHelp import IPS, ip_syshook
#ip_syshook(0) # personal IPython debug stuff

# inspired by the forum thread:
#http://www.matheplanet.com/matheplanet/nuke/html/viewtopic.php?post_id=729017&topic=94327

# pretty much taken from eqtexsvg.py by Julien Vitard (as part of inkscape)
# therefore the whole script is licenced under the terms of the
# GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# needs: latex, dvipng, pngcrush

def createPNG(symbolText, finalpng_filename):

    base_dir="./"
    latex_file = os.path.join(base_dir, "eq.tex")
    aux_file = os.path.join(base_dir, "eq.aux")
    log_file = os.path.join(base_dir, "eq.log")
    dvi_file = os.path.join(base_dir, "eq.dvi")
    out_file = os.path.join(base_dir, "eq.out")
    png_file = os.path.join(base_dir, "eq.png")

    def clean():
        os.remove(latex_file)
        os.remove(aux_file)
        os.remove(log_file)
        os.remove(dvi_file)
        os.remove(out_file)

    def create_equation_tex(filename, equation):
        tex = open(filename, 'w')
        tex.write("""%% processed with kilehilfe.py
\\documentclass{article}
\\usepackage{amsmath}
\\usepackage{amssymb}
\\usepackage{amsfonts}

\\thispagestyle{empty}
\\begin{document}
    """)

        # maybe you will have to add some packages above
        tex.write(equation)
        tex.write("\n\\end{document}\n")
        tex.close()

    create_equation_tex(latex_file, symbolText)
    #os.system('cd ' + base_dir)
    os.system('latex -output-directory=' + base_dir + ' -halt-on-error ' +
                                                latex_file + ' > ' + out_file)
    try:
        os.stat(dvi_file)
    except OSError:
        print >>sys.stderr, "invalid LaTeX input:"
        print >>sys.stderr, symbolText
        print >>sys.stderr, "temporary files were left in:", base_dir
        sys.exit(1)

    os.system('dvipng -T tight -o ' +png_file + ' ' + dvi_file)
    # -T tight cuts off all the empty blank space arround the symbol

    if symbolText[0]+symbolText[-1]=="$$":
       symbolText=symbolText[1:-1] # "$\aplha$" -> "\alpha"

    ## maybe pngcrush could be made obsolate by using this:
    ## (manipultating png-metadata with python)
    ##http://blog.modp.com/2007/08/python-pil-and-png-metadata-take-2.html
    cmd = 'pngcrush -text b "Command" "%s" %s %s ' %(symbolText, png_file,
                                                    finalpng_filename)
    os.system(cmd)
    clean()

###
# main program:

#render a symbol for every element of the list
#using raw strings (r'my_raw_sting') to prevent python from
#interpreting the backslash-character by itself
symbolList=[r'$\dot{q_1}$', r'$\ddot{q_1}$', r'$\dot{q_2}$', r'$\ddot{q_2}$',
            r'$\tilde{q_2}$', r'$\dot{\tilde{q_2}}$', r'$\stackrel{!}{=}$',
            r'$\mathbf{M}$', r'$\text{abc}$', ]


# now iterate over the list
for i, symb in enumerate(symbolList):
    createPNG(symb, "symb%03d.png" % i)

Antworten