]> git.lyx.org Git - lyx.git/blob - lib/scripts/svg2pstex.py
Initial commit of Inkscape SVG LaTeX + PDF support
[lyx.git] / lib / scripts / svg2pstex.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # file svg2pstex.py
5 #
6 # This script converts an SVG image to something that latex can process
7 # into high quality PostScript.
8
9 # Usage:
10 #   python svg2pstex.py ${base}.fig ${base}.pstex
11 # This command generates
12 #   ${base}.eps    the converted eps file
13 #   ${base}.pstex  a tex file that can be included in your latex document
14 #       using '\input{${output}}'.
15 #
16 # Note:
17 #   Do not use this command as
18 #     python svg2pstex.py file.fig file.eps
19 #   the real eps file will be overwritten by a tex file named file.eps.
20 #
21
22 import os, sys
23
24 def runCommand(cmd):
25     ''' Utility function:
26         run a command, quit if fails
27     '''
28     if os.system(cmd) != 0:
29         print "Command '%s' fails." % cmd
30         sys.exit(1)
31
32 # We expect two args, the names of the input and output files.
33 if len(sys.argv) != 3:
34     sys.exit(1)
35
36 input, output = sys.argv[1:]
37
38 # Fail silently if the file doesn't exist
39 if not os.path.isfile(input):
40     sys.exit(0)
41
42 # Strip the extension from ${output}
43 outbase = os.path.splitext(output)[0]
44
45 # Inkscape 0.48 can output the image as a EPS file ${base}.pdf and place the text 
46 # in a LaTeX file ${base}.eps_tex, which is renamed to ${output}, for typesetting 
47 # by latex itself. 
48 runCommand('inkscape --file=%s --export-eps=%s.eps --export-latex' % (input, outbase))
49 os.rename('%s.eps_tex' % outbase, output)