gpt4 book ai didi

swift - 使用 Swift 编译 Latex 代码

转载 作者:行者123 更新时间:2023-11-30 10:18:14 34 4
gpt4 key购买 nike

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

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.

最佳答案

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

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

可以选择设置currentDirectoryPath来执行指定目录中的任务。 “文档”目录通常是这样确定的:

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

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

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

或者,您可以使用 shell 启动可执行文件,类似的东西

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

好处是shell使用PATH环境变量来找到可执行文件。一个缺点是引用论点正确地比较困难。

更新:似乎“/usr/texbin”必须位于 latex 工艺。这可以按如下方式完成:

// 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

关于swift - 使用 Swift 编译 Latex 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28971335/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com