pdflatex.py 3.13 KB
 Martin Owens committed Apr 26, 2019 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 committed Apr 26, 2019 28 ``````from inkex.command import call, inkscape `````` Sergei Izmailov committed Mar 29, 2020 29 ``````from inkex import load_svg, ShapeElement, Defs `````` Martin Owens committed Apr 26, 2019 30 `````` `````` Martin Owens committed Dec 02, 2019 31 ``````class PdfLatex(TempDirMixin, inkex.GenerateExtension): `````` Martin Owens committed Apr 26, 2019 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): `````` Martin Owens committed Sep 25, 2019 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 committed Apr 26, 2019 45 46 47 48 `````` with open(tex_file, 'w') as fhl: self.write_latex(fhl) `````` Martin Owens committed Sep 25, 2019 49 50 51 `````` call('pdflatex', tex_file,\ output_directory=self.tempdir,\ halt_on_error=True, oldie=True) `````` Martin Owens committed Apr 26, 2019 52 `````` `````` Martin Owens committed Apr 03, 2020 53 `````` inkscape(pdf_file, export_filename=svg_file, pdf_page=1, `````` Marc Jeanmougin committed Sep 25, 2019 54 `````` pdf_poppler=True, export_type="svg") `````` Martin Owens committed Apr 26, 2019 55 `````` `````` Martin Owens committed Oct 13, 2020 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 committed Apr 26, 2019 62 `````` with open(svg_file, 'r') as fhl: `````` Martin Owens committed Dec 05, 2019 63 64 65 `````` svg = load_svg(fhl).getroot() svg.set_random_ids(backlinks=True) for child in svg: `````` Martin Owens committed Apr 26, 2019 66 67 `````` if isinstance(child, ShapeElement): yield child `````` Marc Jeanmougin committed Sep 25, 2019 68 69 `````` elif isinstance(child, Defs): for def_child in child: `````` Martin Owens committed Dec 05, 2019 70 `````` #def_child.set_random_id() `````` Marc Jeanmougin committed Sep 25, 2019 71 `````` self.svg.defs.append(def_child) `````` Martin Owens committed Apr 26, 2019 72 73 `````` def write_latex(self, stream): `````` Martin Owens committed Sep 25, 2019 74 `````` """Takes a forumle and wraps it in latex""" `````` Martin Owens committed Apr 26, 2019 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 committed Apr 26, 2019 88 89 `````` if __name__ == '__main__': `````` Martin Owens committed Dec 02, 2019 90 `` PdfLatex().run()``