Compile Latex code using Swift

送分小仙女□ 提交于 2019-12-25 05:25:49

问题


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.

回答1:


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


来源:https://stackoverflow.com/questions/28971335/compile-latex-code-using-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!