Skip to content

Instantly share code, notes, and snippets.

@metzenseifner
Last active December 15, 2015 13:49
Show Gist options
  • Save metzenseifner/5269856 to your computer and use it in GitHub Desktop.
Save metzenseifner/5269856 to your computer and use it in GitHub Desktop.
Python command to execute xelatex and allow spaces in filename. It can be used with TeXShop by pasting /usr/bin/pdflatex.py --file-line-error --shell-escape --synctex=1 in the "Latex" box below pdfTeX. You have to replace the path to pdflatex.py with the path of your choosing (whereever you saved pdflatex.py). converting xelatex.py to pdflatex.p…
#!/usr/bin/python
import os, sys, shutil, subprocess
OUTPUT_ARGUMENT = '--output-directory='
DEFAULT_OUTPUT_PATH = '/Users/Jonathan/tmp'
# Grab the output directory from the arguments
output_arg_specified = False
output_dir = None
for arg in sys.argv:
if arg.startswith(OUTPUT_ARGUMENT):
output_arg_specified = True
output_dir = arg.split('=')[1]
break
# If an output directory argument was not found, then set it to the default
if output_dir is None:
output_dir = DEFAULT_OUTPUT_PATH
# The output pdf will be named the same as the tex file
tex_file = sys.argv[-1] #variable includes spaces in file name no problem here
pdf_file_pathNoExt, extention = os.path.splitext(tex_file) #entire path of filename and unquoted for pdf_file_name
pdf_path,pdf_file_name = os.path.split(pdf_file_pathNoExt)
if pdf_path is "":
pdf_path,temp = os.path.split(os.path.abspath(tex_file))
# Not sure why this is necessary, if input does not have .tex extension then script fails.
assert extention == '.tex'
if not os.path.isdir(output_dir):
os.mkdir(output_dir)
# If command arguments did not contain the oput directory argument, then since
# we set it to have a new detault, add it to the arguments we will pass on
#args = sys.argv[1:]
#if not output_arg_specified:
# args.insert(0, OUTPUT_ARGUMENT + output_dir)
command = '/usr/texbin/xelatex ' + '--output-driver=/usr/texbin/xdvipdfmx ' + OUTPUT_ARGUMENT + '"' + output_dir + '"' + ' ' + '"' + pdf_path + os.path.sep + tex_file + '"'
# Run the original command which generates all the files
os.system(command)
# Source and Destination Variables for the move command below
#pdf_file_src = output_dir + '%s' % (os.path.sep) + pdf_file_name + '.pdf'
pdf_file_src = output_dir + '%s%s%s' % (os.path.sep,pdf_file_name,".pdf")
pdf_file_dest = '%s%s' % (pdf_path, os.path.sep) + pdf_file_name + '.pdf'
print " *** Ran Command: " + command
print " *** OUTPUT ARGUMENT " + OUTPUT_ARGUMENT
#print " *** args %s" % (args)
print " *** Debugging Variables: \n *** tex_file --> " + tex_file + "\n *** pdf_path --> " + pdf_path + "\n *** pdf_file_name --> " + pdf_file_name + "\n *** extention --> " + extention
print " *** pdf_file_src --> " + pdf_file_src + "\n *** pdf_file_dest --> " + pdf_file_dest
# If the destination pdf already exists, delete it so that the new one may be
# coppied.
if os.path.isfile(pdf_file_dest):
os.remove(pdf_file_dest)
# Move the pdf back to where it normally is
shutil.move(pdf_file_src, pdf_file_dest)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment