4 # This file is part of LyX, the document processor.
5 # Licence details can be found in the file COPYING.
8 # author Martin Vermeer
9 # author Jürgen Spitzmüller
11 # Full author contact details are available in file CREDITS
13 # This script converts an SVG image to two files that can be processed
14 # with latex into high quality DVI/PostScript. It requires Inkscape.
17 # python svg2pstex.py [--unstable] [inkscape_command] inputfile.svg outputfile.eps_tex
18 # This command generates
19 # 1. outputfile.eps -- the converted EPS file (text from SVG stripped)
20 # 2. outputfile.eps_tex -- a TeX file that can be included in your
21 # LaTeX document using '\input{outputfile.eps_text}'
22 # use --unstable for inkscape < 1.0
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.
34 import os, sys, re, subprocess
38 run a command, quit if fails
40 res = subprocess.check_call(cmd)
42 print("Command '%s' fails (exit code: %i)." % (res.cmd, res.returncode))
45 InkscapeCmd = "inkscape"
50 # We expect two to four args: the names of the input and output files
51 # and optionally the inkscape command (with path if needed) and --unstable.
54 # Two args: input and output file only
55 InputFile, OutputFile = sys.argv[1:]
57 # Three args: check whether we have --unstable as first arg
58 if sys.argv[1] == "--unstable":
60 InputFile, OutputFile = sys.argv[2:]
62 InkscapeCmd, InputFile, OutputFile = sys.argv[1:]
64 # Four args: check whether we have --unstable as first arg
65 if sys.argv[1] != "--unstable":
66 # Invalid number of args. Exit with error.
70 InkscapeCmd, InputFile, OutputFile = sys.argv[2:]
72 # Invalid number of args. Exit with error.
75 # Fail silently if the file doesn't exist
76 if not os.path.isfile(InputFile):
79 # Strip the extension from ${OutputFile}
80 OutBase = os.path.splitext(OutputFile)[0]
82 # Inkscape (as of 0.48) can output SVG images as an EPS file without text, ${OutBase}.eps,
83 # while outsourcing the text to a LaTeX file ${OutBase}.eps_tex which includes and overlays
84 # the EPS image and can be \input to LaTeX files. We rename the latter file to ${OutputFile}
85 # (although this is probably the name it already has).
87 runCommand([r'%s' % InkscapeCmd, '--file=%s' % InputFile, '--export-eps=%s.eps' % OutBase, '--export-latex'])
89 runCommand([r'%s' % InkscapeCmd, '%s' % InputFile, '--export-filename=%s.eps' % OutBase, '--export-latex'])
91 os.rename('%s.eps_tex' % OutBase, OutputFile)