2 # -*- coding: utf-8 -*-
5 # This file is part of LyX, the document processor.
6 # Licence details can be found in the file COPYING.
9 # author Martin Vermeer
10 # author Jürgen Spitzmüller
12 # Full author contact details are available in file CREDITS
14 # This script converts an SVG image to two files that can be processed
15 # with latex into high quality DVI/PostScript. It requires Inkscape.
18 # python svg2pstex.py [inkscape_command] inputfile.svg outputfile.eps_tex
19 # This command generates
20 # 1. outputfile.eps -- the converted EPS file (text from SVG stripped)
21 # 2. outputfile.eps_tex -- a TeX file that can be included in your
22 # LaTeX document using '\input{outputfile.eps_text}'
25 # Do not use this command as
26 # python svg2pstex.py [inkscape_command] inputfile.svg outputfile.pdf
27 # the real EPS file would be overwritten by a TeX file named outputfile.eps.
30 # This script converts an SVG image to something that latex can process
31 # into high quality PostScript.
33 from __future__ import print_function
35 import os, sys, re, subprocess
39 run a command, quit if fails
41 res = subprocess.check_call(cmd)
43 print("Command '%s' fails (exit code: %i)." % (res.cmd, res.returncode))
46 InkscapeCmd = "inkscape"
50 # We expect two or three args: the names of the input and output files
51 # and optionally the inkscape command (with path if needed).
54 # Two args: input and output file only
55 InputFile, OutputFile = sys.argv[1:]
57 # Three args: first arg is inkscape command
58 InkscapeCmd, InputFile, OutputFile = sys.argv[1:]
60 # Invalid number of args. Exit with error.
63 # Fail silently if the file doesn't exist
64 if not os.path.isfile(InputFile):
67 # Strip the extension from ${OutputFile}
68 OutBase = os.path.splitext(OutputFile)[0]
70 # Inkscape (as of 0.48) can output SVG images as an EPS file without text, ${OutBase}.eps,
71 # while outsourcing the text to a LaTeX file ${OutBase}.eps_tex which includes and overlays
72 # the EPS image and can be \input to LaTeX files. We rename the latter file to ${OutputFile}
73 # (although this is probably the name it already has).
74 runCommand([r'%s' % InkscapeCmd, '--file=%s' % InputFile, '--export-eps=%s.eps' % OutBase, '--export-latex'])
76 os.rename('%s.eps_tex' % OutBase, OutputFile)