Skip to content

Instantly share code, notes, and snippets.

@bmmalone
Last active April 30, 2017 21:25
Show Gist options
  • Save bmmalone/8ce1d659a42591bfc07eb6b8157959db to your computer and use it in GitHub Desktop.
Save bmmalone/8ce1d659a42591bfc07eb6b8157959db to your computer and use it in GitHub Desktop.
Convert an exported Xfig figure to an eps figure
#! /usr/bin/env python3
import argparse
import os.path
import shutil
import subprocess
user_home = os.path.expanduser('~')
default_template_file = os.path.join(
user_home,
"local",
"share",
"fig2eps.tex-template"
)
def main():
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description="This class creates the necessary intermediate files and makes the "
"required calls to convert a figure exported as \"Combined PS/LaTeX (both parts)\" "
"from Xfig to an eps image. This only works when called from the same directory "
"into which the files were exported.")
parser.add_argument('name', help="The base name of the exported files")
parser.add_argument('--template', help="The template tex file", default=default_template_file)
args = parser.parse_args()
# first, copy the template file to the correct location
location = os.path.dirname(os.path.abspath(args.name))
tex_filename = '{}.tex'.format(args.name)
tex_filename = os.path.join(location, tex_filename)
print("tex_filename: {}".format(tex_filename))
shutil.copy(args.template, tex_filename)
# now, replace the <name> template with the actual name
cmd = "sed -i 's:<name>:{}:' {}".format(args.name, tex_filename)
subprocess.call(cmd, shell=True)
# now the call to latex
cmd = "latex {}".format(tex_filename)
subprocess.call(cmd, shell=True)
# and the call to dvips
cmd = "dvips {}.dvi -E -o {}.eps".format(args.name, args.name)
subprocess.call(cmd, shell=True)
if __name__ == '__main__':
main()
\documentclass{article}
\usepackage{epsfig}
\usepackage{amssymb}
\usepackage{amsbsy}
\usepackage{epsfig}
\usepackage{amsmath}
\usepackage{color}% (note: you might not might not need to do this)
\usepackage{pifont}% http://ctan.org/pkg/pifont
\newcommand{\cmark}{\ding{51}}%
\newcommand{\xmark}{\ding{55}}%
%\setlength{\textwidth}{100cm}
%\setlength{\textheight}{100cm}
\begin{document}
\pagestyle{empty}
\input{<name>.pstex_t}
\end{document}
@bmmalone
Copy link
Author

The default_template_file should be updated to wherever the template file is actually located to make using the script easier.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment