Das deutsche Python-Forum

10 Jahre Diskussionen rund um die Programmiersprache Python
Aktuelle Zeit: Dienstag 21. Mai 2013, 20:12

Alle Zeiten sind UTC + 2 Stunden




Ein neues Thema erstellen Auf das Thema antworten  [ 1 Beitrag ] 
Autor Nachricht
 Betreff des Beitrags: Nutzerdefinierte LaTeX Symbole rendern für Kile
BeitragVerfasst: Dienstag 2. Dezember 2008, 11:34 
User

Registriert: Dienstag 5. August 2008, 00:30
Beiträge: 15
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:

#! /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)

 


Nach oben
 Profil  
 
Beiträge der letzten Zeit anzeigen:  Sortiere nach  
Ein neues Thema erstellen Auf das Thema antworten  [ 1 Beitrag ] 

Alle Zeiten sind UTC + 2 Stunden


Wer ist online?

Mitglieder in diesem Forum: 0 Mitglieder


Du darfst keine neuen Themen in diesem Forum erstellen.
Du darfst keine Antworten zu Themen in diesem Forum erstellen.
Du darfst deine Beiträge in diesem Forum nicht ändern.
Du darfst deine Beiträge in diesem Forum nicht löschen.

Suche nach:
Gehe zu:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Deutsche Übersetzung durch phpBB.de