使用Swift编译Latex代码 [英] Compile Latex code using Swift
问题描述
我想使用Swift编译.tex
文件.我有以下代码:
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()
}
}
但是,在调用FileManager.compileLatex(file: "latex.tex")
时,出现错误无法访问启动路径".因此,显然,启动路径是错误的,但是我不知道如何找出它的真正含义?我如何才能找到答案?感谢您的帮助
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
更新代码并出现此错误:
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.
推荐答案
launchPath
必须设置为可执行文件的路径,
例如
The launchPath
must be set to the path of the executable,
for example
task.launchPath = "/usr/texbin/latexmk"
currentDirectoryPath
可以选择设置为执行
指定目录中的任务. 文档"目录
通常是这样确定的:
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
最后,arguments
是以下命令的命令行参数
可执行文件,例如
Finally, the arguments
are the command line arguments for
the executable, for example
task.arguments = ["-xelatex", file]
或者,您可以使用shell启动可执行文件, 像
Alternatively, you can start the executable using the shell, something like
task.launchPath = "/bin/sh"
task.currentDirectoryPath = ...
task.arguments = ["-c", "latexmk -xelatex \"\(file)\""]
优点是外壳程序使用PATH环境变量来 找到可执行文件.缺点之一是引用参数 正确比较困难.
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.
更新:似乎"/usr/texbin"必须在 LaTeX流程.可以按照以下步骤进行操作:
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
这篇关于使用Swift编译Latex代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!