从C ++程序编译LaTeX文档

路易·菲利普(Louis Philippe)

我正在编写一个C ++程序,该程序必须自动生成一些数据,供学生在综合练习中使用。我已经将该数据导出到.tex文件,并且还希望C ++程序能够自动编译这些tex文件。

通常,我会通过执行以下操作从命令行编译tex文件:

$ latex file.tex
$ latex file.tex
$ dvipdf file.dvi

因此,我尝试在我的C ++代码中执行以下操作(目录和文件名均为字符串):

//Move to the location where I created the files
string mycommand = "cd ";
mycommand += directory;
system(mycommand.c_str());
//Compile latex
mycommand = "latex " + filename + "_main.tex";
system(mycommand.c_str());
system(mycommand.c_str());
//Create .pdf
mycommand = "dvipdf " + filename + "_main.dvi";
system(mycommand.c_str());

然后在终端输出上产生以下错误消息:

sh: latex: command not found
sh: latex: command not found
sh: dvipdf: command not found

我已经在网上搜索了此内容,但未能找到解决该问题的方法,尽管我认为这很可能很简单。

我正在OSX上工作,并安装了以下版本的乳胶:

pdfTeX 3.1415926-2.4-1.40.13 (TeX Live 2012)
kpathsea version 6.1.0

非常感谢所有帮助!

乔费

首先,路径的方案latex,并dvipdf需要在你的PATH环境变量。

其次,shell via的调用system是完全独立的(实际上,每次都会启动一个新的Shell实例)。因此,如果您在一个目录中切换目录,则不会影响其他目录。而是通过以下命令切换程序的当前目录:

chdir(directory.c_str())

你需要这个

#include <cunistd>
using namespace std;

在文件的开头。

请注意,system如果未仔细检查参数(在您的情况下为文件名),则可以轻松利用依赖于输入参数的命令行调用来运行任意命令。由于您没有引号,因此如果文件名中包含空格,则程序将失败。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章