Home Download Buy Blog Forum Support

launching an external process

launching an external process

Postby kib2 on Sat Oct 04, 2008 11:11 am

Hi,

I was just typing a LaTeX document inside Sublime and I wonder how to launch ie "pdflatex" on it.
I had a look at the docs, but "build", "exec" etc. are not documented.

Thanks for any suggestion.
kib2
 
Posts: 38
Joined: Sun Mar 23, 2008 8:30 am
Location: France

Re: launching an external process

Postby vim on Sat Oct 04, 2008 11:34 am

you can use python's os.system() in a python plugin.

for example:

Code: Select all
import sublime, sublimeplugin
import os
class MyPluginCommand(sublimeplugin.TextCommand):
   def run(self, view, args):
      f = view.fileName();
      result = os.system("parser.exe " + f)

   def isEnabled(self, view, args):
      return True
vim
 
Posts: 298
Joined: Tue May 27, 2008 6:51 pm

Re: launching an external process

Postby kib2 on Sun Oct 05, 2008 8:33 am

Thanks for the suggestion vim, I've tried but got problems.

Code: Select all
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sublime, sublimeplugin
import os

class PdfLatexCommand(sublimeplugin.TextCommand):
    def run(self, view, args):
        f = view.fileName()
        print "the file is %s"%f
        result = os.system("pdflatex " + f)
   
    def isEnabled(self, view, args):
        return True


(pdflatex is in my PATH so I don't need to put the ".exe" behind it).
I've binded the command to a key, but nothing seems to happen.

So, I've tried to do a 'LaTeX.sublime-build' file with this (I took it from the Haskell one):

Code: Select all
buildCommand exec "^(...*?):([0-9]*):?([0-9]*)" pdflatex.exe '"$File"'


It works, but in fact I really don't know what I'm doing. What's the regexp "^(...*?):([0-9]*):?([0-9]*)" for ?
And now that my file ie "myfile.tex" has been compiled to "myfile.pdf", how to call my reader on it ? $File returns the file name, I need it without its extension + "pdf" ($File[:-3]+"pdf" in Python). It must exist other variables like $File, but what are their names ?

I know it's pain to write documentation, but Sublime really needs better docs (the wiki has become unreadable). We can all help jps on this point, I think it's no problem.
kib2
 
Posts: 38
Joined: Sun Mar 23, 2008 8:30 am
Location: France

Re: launching an external process

Postby vim on Sun Oct 05, 2008 8:54 pm

regarding the first method i suggested maybe you need to add:

Code: Select all
class MyPluginCommand(sublimeplugin.TextCommand):
   def run(self, view, args):
      f = view.fileName();
      exe = sublime.packagesPath()
      exe = os.path.join(exe, "User")
      exe = os.path.join(exe, "MyParser.exe")
         
      # Spaces in path workaround:
      # add an extra quote (!) before the quoted command name:
      # result = os.system('""pythonbugtest.exe" "test"')
      # Explanation:
      # there was a time when the cmd prompt treated all spaces as delimiters, so
      # >cd My Documents
      # would fail. Nowadays you can do that successfully and even
      # >cd My Documents\My Pictures
      # works.
      # In the old days, if a directory had a space, you had to enclose it in quotes
      # >cd "My Documents"
      # But you didn't actually need to include the trailing quote, so you could get away with
      # >cd "My Documents
      cmd = '""%(exe)s" "%(args)s""' % {'exe' : exe, 'args' : f } # stderr > out.txt 2>&1
      result = os.system(cmd)


that what i have used once and it worked. regarding the other method, i must admit it seems cleaner, but i can join your feelings here, i also don't have a clue how it works (i stumbled on it once, but couldn't figure it out)
vim
 
Posts: 298
Joined: Tue May 27, 2008 6:51 pm

Re: launching an external process

Postby jps on Mon Oct 06, 2008 7:18 am

I recommend using a Makefile for these sort of things, when I'm working with pdfs, I use a Makefile along the lines of:

Code: Select all
foo.pdf: foo.tex
   pdfclose --all
   pdflatex -interaction=nonstopmode foo.tex
   pdfopen --file foo.pdf


The requires you to have make installed (e.g., from cygwin), and pdfopen/pdfclose (availble from a few places, http://magic.aladdin.cs.cmu.edu/2005/07 ... -pdfclose/ is one).

The syntax of sublime-build files is:

Code: Select all
buildCommand exec error-regex command [args].


The regex matches file names in the output of the command, typically compiler errors messages. Submatch 1 should be the filename, 2 the line number, and 3 the column (the latter two being optional). The regex must be quoted, and any escapes within it double-escaped.

I'm not happy with the syntax of *.sublime-build files, it's on the todo list to make them more reasonable.
jps
Site Admin
 
Posts: 3067
Joined: Wed Mar 19, 2008 12:33 pm

Re: launching an external process

Postby SteveCooperOrg on Mon Oct 06, 2008 12:38 pm

Here's some snippets from my pdflatex compile command. Should be almost everything you need.

Code: Select all
#
# COMPILE LATEX AND LAUNCH ACROBAT
#
def compilePdf(latexFile):
  miktexExe = miktexCommandPath("pdflatex")
  commandLine = " --interaction=nonstopmode --aux-directory c:\\temp\\ \"" + latexFile + "\""
  # two compiles, to make sure contents etc are up-to-date
  for i in range(2):     
    print runSysCommand(miktexExe, commandLine)
  pdf = os.path.splitext(latexFile)[0] + ".pdf"
  print "pdf created at '%s'" % pdf
  subprocess.Popen(pdf, shell=True)

#
# RUN EXE AND GET OUTPUT
#
def getOutputOfSysCommand(commandText, arguments=None):
  """Returns the output of a command, as a string"""
  print "getting output of %s %s" % (commandText, arguments)
  p = subprocess.Popen([commandText, arguments], shell=True, bufsize=1024, stdout=subprocess.PIPE)
  p.wait()
  stdout = p.stdout
  return stdout.read()

#
# WHERE DOES MIKTEX LIVE?
#
def miktexCommandPath(commandName):
  return os.path.join(programFiles(), "MiKTeX 2.7\\miktex\\bin", commandName + ".exe")

#
# WHERE ARE PROGRAM FILES?
#
def programFiles():
  prog32 = "c:\\program files"
  prog64 = "c:\\program files (x86)"
  if os.path.exists(prog64):
    return prog64
  elif os.path.exists(prog32):
    return prog32
  else:
    raise Exception("can't find a program files")

Maybe what you're looking for is at the http://www.sublimetextwiki.com
SteveCooperOrg
 
Posts: 193
Joined: Thu Mar 20, 2008 9:53 am

Re: launching an external process

Postby kib2 on Mon Oct 06, 2008 3:09 pm

Thanks for all your answers,

jps : thanks for clarifying these points, I know now what the regexps are for. I wasn't aware of pdfopen/pdfclose : nice tool !

SteveCooperOrg : I'll try to adapt it to my needs as I'm using TeXLive 2008 on Windows.
kib2
 
Posts: 38
Joined: Sun Mar 23, 2008 8:30 am
Location: France


Return to Technical Support

Who is online

Users browsing this forum: Google [Bot], michaelaye and 30 guests