wiki:LaTex

Version 1 (modified by chriz, 9 years ago) (diff)

Migrated from old trac wiki

This is a keyword plugin to render formulas with LaTex becaus mimetex isn't so nice (but mimetex doesn't need dependencies). Dependencies:

  • dvipng (usually included in latex distributions, like texlive)
  • latex

You have to set the latex and dvipng executables (in Linux usually installed in /usr/bin/latex and /usr/bin/dvipng ). the keyword is eq. [:eq:"H \Psi = E \Psi"]

Here's the code, save it as latex.py in the <path to wikidpad>/user_extension directory

# -*- coding: utf-8 -*-
import os, urllib, os.path
import subprocess

import wx

from pwiki.StringOps import mbcsEnc

WIKIDPAD_PLUGIN = (("InsertionByKey", 1), ("Options", 1))

def describeInsertionKeys(ver, app):
    """
    API function for "InsertionByKey" plugins
    Returns a sequence of tuples describing the supported
    insertion keys. Each tuple has the form (insKey, exportTypes, handlerFactory)
    where insKey is the insertion key handled, exportTypes is a sequence of
    strings describing the supported export types and handlerFactory is
    a factory function (normally a class) taking the wxApp object as
    parameter and returning a handler object fulfilling the protocol
    for "insertion by key" (see EqnHandler as example).

    ver -- API version (can only be 1 currently)
    app -- wxApp object
    """
    return ((u"eq", ("html_single", "html_previewWX", "html_preview",
            "html_multi"), EqnHandler),)


class EqnHandler:
    """
    Class fulfilling the "insertion by key" protocol.
    """
    def __init__(self, app):
        self.app = app
        #self.extAppExe = "/usr/bin/latex"
        self.extAppExe = ""
        self.extConvExe=""
        
    def taskStart(self, exporter, exportType):
        """
        This is called before any call to createContent() during an
        export task.
        An export task can be a single HTML page for
        preview or a single page or a set of pages for export.
        exporter -- Exporter object calling the handler
        exportType -- string describing the export type
        
        Calls to createContent() will only happen after a 
        call to taskStart() and before the call to taskEnd()
        """
        self.extAppExe = self.app.getGlobalConfig().get("main",
        "plugin_laTex_exePath", "")
        self.extConvExe = self.app.getGlobalConfig().get("main",
        "plugin_dvipng_exePath", "")
        
        if self.extAppExe:
            self.extAppExe = os.path.join(self.app.getWikiAppDir(),
                    self.extAppExe)
        
        
    def taskEnd(self):
        """
        Called after export task ended and after the last call to
        createContent().
        """
        pass


    def createContent(self, exporter, exportType, insToken):
        """
        Handle an insertion and create the appropriate content.

        exporter -- Exporter object calling the handler
        exportType -- string describing the export type
        insToken -- insertion token to create content for

        An insertion token has the following member variables:
            key: insertion key (unistring)
            value: value of an insertion (unistring)
            appendices: sequence of strings with the appendices

        Meaning and type of return value is solely defined by the type
        of the calling exporter.
        
        For HtmlExporter a unistring is returned with the HTML code
        to insert instead of the insertion.        
        """
        bstr = urllib.quote(mbcsEnc(insToken.value, "replace")[0])
        formula=insToken.value

        if not bstr:
            # Nothing in, nothing out
            return u""
        
        if self.extAppExe == "":
            # No path to LaTeX executable -> show message
            return u"<pre>" + _(u"[Please set path to LaTeX executable]") + \
                    "</pre>"
        if self.extConvExe == "":
            # No path to dvipng executable -> show message
            return u"<pre>" + _(u"[Please set path to dvipng executable]") + \
                    "</pre>"

        # Temporary file generator
        tfs = exporter.getTempFileSet()
        pythonUrl = (exportType != "html_previewWX")
        # /qualcosa/qualcosaltro
        dstFullPath = tfs.createTempFile("", ".png", relativeTo="")
        # file:/qualcosa/qualcosaltro
        url = tfs.getRelativeUrl(None, dstFullPath, pythonUrl=pythonUrl)
        # Input for LateX process        
        template=ur'''\documentclass[12pt]{article}
 \pagestyle{empty}
 \begin{document}
 \begin{displaymath}
 %s
 \end{displaymath}
 \end{document}''' % formula
        urltex = dstFullPath[:-4]+".tex" # Temporary url for tex file
        
        texfile=open(urltex, "w")
        texfile.write(template)
        texfile.close()
        # Run LaTeX process
        options="-output-directory="+os.path.dirname(urltex)
        proc=subprocess.Popen((self.extAppExe, options, "-halt-on-error",  urltex), stdout=subprocess.PIPE)
        trash=proc.communicate() # Eating the stdout > /dev/null
        if  proc.returncode!=0:
            return u"<pre>" + u"[Latex error, check the sintax]" +\
                    u"</pre>"
        # Run conversion
        convstring="%s -T tight -x 1200 -z 9 -bg transparent -o %s %s"%(self.extConvExe, dstFullPath , dstFullPath[:-4]+".dvi")
        dvipng=subprocess.Popen(convstring, shell=True,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        trash=dvipng.communicate()
        if dvipng.returncode!=0:
            return u"<pre>" + u"[dvipng error, probably there's an error in the executable path]" +\
                    u"</pre>"
        # Cleaning unuseful files
        unuseful=[dstFullPath[:-4]+".dvi",dstFullPath[:-4]+".aux", dstFullPath[:-4]+".log"  ]
        for unuse in unuseful:
            os.remove(unuse)
        # Return appropriate HTML code for the image
        if exportType == "html_previewWX":
            # Workaround for internal HTML renderer
            return (u'<img src="%s" border="0" align="bottom" alt="formula" />'
                    u'&nbsp;') % url
        else:
            return u'<img src="%s" border="0" align="bottom" alt="formula" />' \
                    % url


    def getExtraFeatures(self):
        """
        Returns a list of bytestrings describing additional features supported
        by the plugin. Currently not specified further.
        """
        return ()




def registerOptions(ver, app):
    """
    API function for "Options" plugins
    Register configuration options and their GUI presentation
    ver -- API version (can only be 1 currently)
    app -- wxApp object
    """
    # Register option
    app.getDefaultGlobalConfigDict()[("main", "plugin_laTex_exePath")] = u""
    app.getDefaultGlobalConfigDict()[("main", "plugin_dvipng_exePath")] = u""
    # Register panel in options dialog
    app.addOptionsDlgPanel(LaTexOptionsPanel, u"  LaTeX")


class LaTexOptionsPanel(wx.Panel):
    def __init__(self, parent, optionsDlg, app):
        """
        Called when "Options" dialog is opened to show the panel.
        Transfer here all options from the configuration file into the
        text fields, check boxes, ...
        """
        wx.Panel.__init__(self, parent)
        self.app = app
        
        pt = self.app.getGlobalConfig().get("main", "plugin_laTex_exePath", "")
        dp= self.app.getGlobalConfig().get("main", "plugin_dvipng_exePath", "")
        self.tfPath = wx.TextCtrl(self, -1, pt)
        self.dpPath= wx.TextCtrl(self, -1, dp)
        mainsizer = wx.BoxSizer(wx.VERTICAL)

        inputsizer = wx.BoxSizer(wx.HORIZONTAL)
        inputsizer.Add(wx.StaticText(self, -1, _(u"Path to LaTeX:")), 0,
                wx.ALL | wx.EXPAND, 5)
        inputsizer.Add(self.tfPath, 1, wx.ALL | wx.EXPAND, 5)
        
        inputsizer1 = wx.BoxSizer(wx.HORIZONTAL)
        inputsizer1.Add(wx.StaticText(self, -1, _(u"Path to dvipng:")), 0,
                wx.ALL | wx.EXPAND, 5)
        inputsizer1.Add(self.dpPath, 1, wx.ALL | wx.EXPAND, 5)
        
        mainsizer.Add(inputsizer, 0, wx.EXPAND)
        mainsizer.Add(inputsizer1, 0, wx.EXPAND)
        self.SetSizer(mainsizer)
        self.Fit()

    def setVisible(self, vis):
        """
        Called when panel is shown or hidden. The actual wxWindow.Show()
        function is called automatically.
        
        If a panel is visible and becomes invisible because another panel is
        selected, the plugin can veto by returning False.
        When becoming visible, the return value is ignored.
        """
        return True

    def checkOk(self):
        """
        Called when "OK" is pressed in dialog. The plugin should check here if
        all input values are valid. If not, it should return False, then the
        Options dialog automatically shows this panel.
        
        There should be a visual indication about what is wrong (e.g. red
        background in text field). Be sure to reset the visual indication
        if field is valid again.
        """
        return True

    def handleOk(self):
        """
        This is called if checkOk() returned True for all panels. Transfer here
        all values from text fields, checkboxes, ... into the configuration
        file.
        """
        pt = self.tfPath.GetValue()
        dp = self.dpPath.GetValue()
        
        self.app.getGlobalConfig().set("main", "plugin_laTex_exePath", pt)
        self.app.getGlobalConfig().set("main", "plugin_dvipng_exePath", dp)

Attachments (1)

Download all attachments as: .zip