5

I followed instructions provided here(How to create a shortcut for user's build system in Sublime Text?) to compile latex documents in xelatex, and on top of that I would also like it to automatically open pdf after compiling just like with latexmk, how can I achieve that? The document is built just fine, but I have to open it each time manually.

Community
  • 1
  • 1
cgnx
  • 113
  • 2
  • 14
  • 1
    Are you doing this in linux or windows? You could try invoking a scritp (.bat or .sh) from python to open the pdf after creating it. – jspurim May 15 '14 at 22:15
  • 1
    This post should help you implement this in Python for both Windows and OS X: https://stackoverflow.com/questions/434597/open-document-with-default-application-in-python – Sage Mitchell May 16 '14 at 16:11
  • Thank you, I will have a look at it later, now I am a bit busy. And as for the OS, I am on windows – cgnx May 16 '14 at 21:05

1 Answers1

5

Here's an extension to the CompileWithXelatexCommand implementation that successfully opens the PDF in my default PDF viewer.

import sublime, sublime_plugin
import os
import time

class CompileWithXelatexCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        if '/usr/texbin' not in os.environ['PATH']:
            os.environ['PATH'] += ':/usr/texbin'

        base_fname = self.view.file_name()[:-4]
        pdf_fname = base_fname + ".pdf"
        self.view.window().run_command('exec',{'cmd': ['xelatex','-synctex=1','-interaction=nonstopmode',base_fname]})

        tries = 5
        seconds_to_wait = 1
        while tries > 0:
            if os.path.isfile(pdf_fname):
                break
            time.sleep(seconds_to_wait)
            seconds_to_wait *= 2
            tries -= 1

        os.system("open " + pdf_fname)

The polling loop is required; otherwise, the open call may happen before the PDF has been generated. There may be a cleaner way to synchronously exec a sequence of commands via run_command.

I don't have access to Windows now, but from this post you'll probably just need to change "open " to "start ". The PATH initialization logic will either need to be eliminated or adjusted.

Community
  • 1
  • 1
Sage Mitchell
  • 1,563
  • 8
  • 31
  • great! It seems to work just fine(it compiles and open pdf), but I still get this warning in sublime "Output written on xe.pdf (1 page). Transcript written on xe.log. xelatex: unrecognized option `-syntex=1'" so for it doesn't affect anything, but what is wrong with that?I only changed open to start as you suggested. – cgnx May 17 '14 at 09:25
  • My mistake. It's supposed to be `-synctex=1` (with a 'c'). Editing the post now. – Sage Mitchell May 17 '14 at 15:57