Skip to content

Instantly share code, notes, and snippets.

@svenmuennich
Last active December 16, 2015 13:38
Show Gist options
  • Save svenmuennich/5442634 to your computer and use it in GitHub Desktop.
Save svenmuennich/5442634 to your computer and use it in GitHub Desktop.
This is a small plugin for Sublime Text 2. It automatically compiles a saved .tex to the same directory. You have to save 'compiletexonsave.py' to '~/Library/Application Support/Sublime Text 2/Packages/CompileTexOnSave/compiletexonsave.py' or similar based on your OS (mine is OSX). To enable auto compiling you have to edit your Sublime project s…
import sublime
import sublime_plugin
import subprocess
import os
from threading import Thread
#####
# The main compile method using the command line to navigate to the saved files directory
# and calling 'pdflatex' on the saved file.
#####
def compile(inputFile, outputFolder):
# Create the commands
cmd = "cd {0}; /usr/texbin/pdflatex {1}".format(outputFolder, inputFile)
# Exectute them
proc = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True)
# Print TeX output
print proc.communicate()[0]
class CompileTexOnSave(sublime_plugin.EventListener):
def on_post_save(self, view):
# Check if the saved file is a .tex-file and compiling is enabled in the project settings
fileType = (view.syntax_name(view.sel()[0].b)).split().pop()
should_compile = view.settings().get('compile_tex_on_save')
if fileType == 'text.tex.latex' and should_compile == 1:
# Determine file name and output directory
fileName = os.path.split(view.file_name())[1]
folder = view.window().folders()[0]
print 'Will compile', fileName, 'to', folder
# Start compiling in new thread
t = Thread(target=compile, args=(fileName, folder))
t.start()
{
"folders":
[
{
"path": "/The/Path/To/Your/Project"
}
],
"settings":
{
"compile_tex_on_save": 1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment