Search code examples
swiftterminalxelatex

Compile Latex code using Swift


I wanted to compile a .tex file using Swift. I have the following code:

class FileManager {
    class func compileLatex(#file: String) {
        let task = NSTask()
        task.launchPath = "/usr/texbin/latexmk"
        task.currentDirectoryPath = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! String).stringByAppendingString("/roster")
        task.arguments = ["-xelatex", file]
        task.launch()
    }
}

However, when calling FileManager.compileLatex(file: "latex.tex") I get the error 'launch path not accessible'. So apparently, the launch path is wrong, yet I don't know how to find out which it really is? How can I find out or is there a general path? Thanks for any help

EDIT:

updated code and getting this error:

Latexmk: This is Latexmk, John Collins, 10 January 2015, version: 4.42.
Latexmk: applying rule 'pdflatex'...
Rule 'pdflatex': Rules & subrules not known to be previously run:
   pdflatex
Rule 'pdflatex': The following rules & subrules became out-of-date:
      'pdflatex'
------------
Run number 1 of rule 'pdflatex'
------------
------------
Running 'xelatex  -recorder  "Praktikumsbericht.tex"'
------------
sh: xelatex: command not found
Latexmk: Errors, so I did not complete making targets
Collected error summary (may duplicate other messages):
  pdflatex: (Pdf)LaTeX failed to generate the expected log file 'Praktikumsbericht.log'
Latexmk: Did not finish processing file 'Praktikumsbericht.tex':
   (Pdf)LaTeX failed to generate the expected log file 'Praktikumsbericht.log'
Latexmk: Use the -f option to force complete processing,
 unless error was exceeding maximum runs of latex/pdflatex.

Solution

  • The launchPath must be set to the path of the executable, for example

    task.launchPath = "/usr/texbin/latexmk"
    

    The currentDirectoryPath can optionally be set to execute the task in a specified directory. The "Documents" directory is usually determined like this:

    task.currentDirectoryPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
    

    Finally, the arguments are the command line arguments for the executable, for example

    task.arguments = ["-xelatex", file]
    

    Alternatively, you can start the executable using the shell, something like

    task.launchPath = "/bin/sh"
    task.currentDirectoryPath = ...
    task.arguments = ["-c", "latexmk -xelatex \"\(file)\""]
    

    The advantage is that the shell uses the PATH environment variable to locate the executable. One disadvantage is that quoting the arguments correctly is more difficult.

    Update: It seems that "/usr/texbin" must be in the PATH for the LaTeX process. This can be done as follows:

    // Get current environment:
    var env = NSProcessInfo.processInfo().environment
    // Get PATH:
    var path = env["PATH"] as String
    // Prepend "/usr/texbin":
    path = "/usr/texbin:" + path
    // Put back to environment:
    env["PATH"] = path
    // And use this as environment for the task:
    task.environment = env