0

我想.tex用 Swift 编译一个文件。我有以下代码:

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()
    }
}

但是,在调用时,FileManager.compileLatex(file: "latex.tex")我收到错误“无法访问启动路径”。显然,启动路径是错误的,但我不知道如何找出它的真正含义?我怎样才能找到或有一般的路径?谢谢你的帮助

编辑:

更新代码并收到此错误:

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.
4

1 回答 1

1

launchPath必须设置为可执行文件的路径,例如

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

可以选择将其currentDirectoryPath设置为在指定目录中执行任务。“Documents”目录通常是这样确定的:

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

最后,arguments是可执行文件的命令行参数,例如

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

或者,您可以使用 shell 启动可执行文件,例如

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

优点是 shell 使用 PATH 环境变量来定位可执行文件。一个缺点是正确引用论点更加困难。

更新:似乎“/usr/texbin”必须在 LaTeX 进程的 PATH 中。这可以按如下方式完成:

// 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
于 2015-03-10T18:34:30.337 回答