4

I am writing a C++ program which has to automatically generate some data to be used by students in an integrated exercise. I have already exported this data to .tex files and would also like the C++ program to be able to compile these tex files automatically.

Usually I would compile tex files from the command line by doing the following:

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

So I tried doing the following in my C++ code (directory and filename are both strings):

//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());

Which then produces the following error message on the terminal output:

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

I have searched this online but I have failed to find a solution for this problem, though I believe it is likely to be something very simple.

I am working on OSX and have the following version of latex installed:

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

All help is greatly appreciated!

4

1 回答 1

2

首先,程序的路径latex需要dvipdf在您的PATH环境变量中。

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

chdir(directory.c_str())

你需要这个

#include <cunistd>
using namespace std;

在文件的开头。

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

于 2013-08-23T11:47:15.660 回答