pdflatex.py 3.13 KB
Newer Older
Martin Owens's avatar
Martin Owens committed
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
#!/usr/bin/env python
# coding=utf-8
#
# Copyright (C) 2019 Marc Jeanmougin
#
# This program is free software; you can redistribute it and/or modify
# it 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.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
#
"""
Generate Latex via a PDF using pdflatex
"""

import os

26
27
import inkex
from inkex.base import TempDirMixin
Martin Owens's avatar
Martin Owens committed
28
from inkex.command import call, inkscape
29
from inkex import load_svg, ShapeElement, Defs
Martin Owens's avatar
Martin Owens committed
30

31
class PdfLatex(TempDirMixin, inkex.GenerateExtension):
Martin Owens's avatar
Martin Owens committed
32
33
34
35
36
37
38
39
40
41
    """
    Use pdflatex to generate LaTeX, this whole hack is required because
    we don't want to open a LaTeX document as a document, but as a
    generated fragment (like import, but done manually).
    """
    def add_arguments(self, pars):
        pars.add_argument('--formule', type=str, default='')
        pars.add_argument('--packages', type=str, default='')

    def generate(self):
42
43
44
        tex_file = os.path.join(self.tempdir, 'input.tex')
        pdf_file = os.path.join(self.tempdir, 'input.pdf') # Auto-generate by pdflatex
        svg_file = os.path.join(self.tempdir, 'output.svg')
Martin Owens's avatar
Martin Owens committed
45
46
47
48

        with open(tex_file, 'w') as fhl:
            self.write_latex(fhl)

49
50
51
        call('pdflatex', tex_file,\
            output_directory=self.tempdir,\
            halt_on_error=True, oldie=True)
Martin Owens's avatar
Martin Owens committed
52

53
        inkscape(pdf_file, export_filename=svg_file, pdf_page=1,
Marc Jeanmougin's avatar
Marc Jeanmougin committed
54
                 pdf_poppler=True, export_type="svg")
Martin Owens's avatar
Martin Owens committed
55

56
57
58
59
60
61
        if not os.path.isfile(svg_file):
            fn = os.path.basename(svg_file)
            if os.path.isfile(fn):
                # Inkscape bug detected, file got saved wrong
                svg_file = fn

Martin Owens's avatar
Martin Owens committed
62
        with open(svg_file, 'r') as fhl:
63
64
65
            svg = load_svg(fhl).getroot()
            svg.set_random_ids(backlinks=True)
            for child in svg:
Martin Owens's avatar
Martin Owens committed
66
67
                if isinstance(child, ShapeElement):
                    yield child
Marc Jeanmougin's avatar
Marc Jeanmougin committed
68
69
                elif isinstance(child, Defs):
                    for def_child in child:
70
                        #def_child.set_random_id()
Marc Jeanmougin's avatar
Marc Jeanmougin committed
71
                        self.svg.defs.append(def_child)
Martin Owens's avatar
Martin Owens committed
72
73

    def write_latex(self, stream):
74
        """Takes a forumle and wraps it in latex"""
Martin Owens's avatar
Martin Owens committed
75
76
77
78
79
80
81
82
83
84
85
86
        stream.write(r"""%% processed with pdflatex.py
\documentclass{minimal}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsfonts}
""")
        for package in self.options.packages.split(','):
            if package:
                stream.write('\\usepackage{{{}}}\n'.format(package))
        stream.write("\n\\begin{document}\n")
        stream.write(self.options.formule)
        stream.write("\n\\end{document}\n")
87
        stream.flush()
Martin Owens's avatar
Martin Owens committed
88
89

if __name__ == '__main__':
90
    PdfLatex().run()