I'm looking for a library (or a cleverer solution) in C or C++ that would make an image file (PNG|jpeg) from LaTeX code. The use of packages is a prerequisite.
我正在寻找一个C或c++的库(或者更聪明的解决方案),可以从乳胶代码中生成一个图像文件(PNG|jpeg)。使用包是一个先决条件。
For now I'm thinking of compiling a .tex file into a .dvi and using dvipng to get a .PNG.
目前,我正在考虑将.tex文件编译为.dvi,并使用dvipng获得. png。
There's also the possibility of compiling a .tex file into a .ps file and then converting it into a .PNG by the mean of extern utilities like pstopng or pstoedit.
也有可能将.tex文件编译成.ps文件,然后将其转换成一个. png,由外部实用工具(如pstopng或pstoedit)进行转换。
But these solutions are cumbersome and not always portable. I would like to integrate this conversion in my program transparently.
但是这些解决方案很麻烦,而且并不总是可以移植的。我想在我的程序中透明地集成这个转换。
2 个解决方案
#1
2
I've used the dvipng route several times before, but in python. It's a common path, that lots of people have taken. Here's the code, to give you something to get started, and in case anyone wants Python code. I do realise you asked for C/C++; this is for a starter, or for others. This is for generating equations, but it would be trivial to adapt it for more general structures. It does support packages.
我以前多次使用dvipng路径,但是是在python中。这是一条很常见的路,很多人都走了。这里有一些代码,可以让您开始,如果有人想要Python代码的话。我知道你要的是C/ c++;这是给初学者用的,或者给别人用的。这是用来生成方程的,但是把它应用于更一般的结构是很简单的。它支持包。
In terms of integrating it transparently, I feel your pain. Not everyone has tex / latex of course, and if they don't, it's often a pain to get. The best way to do it, I think, is to provide that functionality as a web service - but of course that's not always an option.
就整合而言,我感受到了你的痛苦。当然,并不是每个人都有tex / latex,如果他们没有,那就会很痛苦。我认为,最好的方法是将该功能作为web服务提供——当然,这并不总是一种选择。
Finally, note all the options for dvipng. They control the appearance, via various anti-alisaing options etc. I tuned them extensively to get what I thought looked good.
最后,注意dvipng的所有选项。它们通过各种反alisaing选项等来控制外观。我对它们进行了广泛的调整,以获得我认为看起来不错的效果。
def geneq(f, eq, dpi, wl, outname, packages):
# First check if there is an existing file.
eqname = os.path.join(f.eqdir, outname + '.png')
# Open tex file.
tempdir = tempfile.gettempdir()
fd, texfile = tempfile.mkstemp('.tex', '', tempdir, True)
basefile = texfile[:-4]
g = os.fdopen(fd, 'w')
preamble = '\documentclass{article}\n'
for p in packages:
preamble += '\usepackage{%s}\n' % p
preamble += '\pagestyle{empty}\n\\begin{document}\n'
g.write(preamble)
# Write the equation itself.
if wl:
g.write('\\[%s\\]' % eq)
else:
g.write('$%s$' % eq)
# Finish off the tex file.
g.write('\n\\newpage\n\end{document}')
g.close()
exts = ['.tex', '.aux', '.dvi', '.log']
try:
# Generate the DVI file
latexcmd = 'latex -file-line-error-style -interaction=nonstopmode ' + \
'-output-directory %s %s' % (tempdir, texfile)
p = Popen(latexcmd, shell=True, stdout=PIPE)
rc = p.wait()
if rc != 0:
for l in p.stdout.readlines():
print ' ' + l.rstrip()
exts.remove('.tex')
raise Exception('latex error')
dvifile = basefile + '.dvi'
dvicmd = 'dvipng --freetype0 -Q 9 -z 3 --depth -q -T tight -D %i -bg Transparent -o %s %s' % (dpi, eqname, dvifile)
# discard warnings, as well.
p = Popen(dvicmd, shell=True, stdout=PIPE, stderr=PIPE)
rc = p.wait()
if rc != 0:
print p.stderr.readlines()
raise Exception('dvipng error')
depth = int(p.stdout.readlines()[-1].split('=')[-1])
finally:
# Clean up.
for ext in exts:
g = basefile + ext
if os.path.exists(g):
os.remove(g)
#2
1
If you want to convert parts of your code to png files, and not necessarily everything, have a look at the preview package. As per their README, it can extract parts of a Latex source file to separate dvi files, and those can be converted to png files. Another option instead of using dvipng to get PNGs would be to convert the generated PDF/ps files to PNG directly:
如果您希望将部分代码转换为png文件,而不是全部,请查看预览包。根据他们的自述文件,它可以提取乳胶源文件的一部分来分离dvi文件,这些文件可以转换为png文件。另一个选择不是使用dvipng来获取PNG,而是将生成的PDF/ps文件直接转换为PNG:
gs -sDEVICE=png16m -dTextAlphaBits=4 -r300 -dGraphicsAlphaBits=4 -dSAFER -dBATCH -dNOPAUSE -sOutputFile=file.png file.pdf
#1
2
I've used the dvipng route several times before, but in python. It's a common path, that lots of people have taken. Here's the code, to give you something to get started, and in case anyone wants Python code. I do realise you asked for C/C++; this is for a starter, or for others. This is for generating equations, but it would be trivial to adapt it for more general structures. It does support packages.
我以前多次使用dvipng路径,但是是在python中。这是一条很常见的路,很多人都走了。这里有一些代码,可以让您开始,如果有人想要Python代码的话。我知道你要的是C/ c++;这是给初学者用的,或者给别人用的。这是用来生成方程的,但是把它应用于更一般的结构是很简单的。它支持包。
In terms of integrating it transparently, I feel your pain. Not everyone has tex / latex of course, and if they don't, it's often a pain to get. The best way to do it, I think, is to provide that functionality as a web service - but of course that's not always an option.
就整合而言,我感受到了你的痛苦。当然,并不是每个人都有tex / latex,如果他们没有,那就会很痛苦。我认为,最好的方法是将该功能作为web服务提供——当然,这并不总是一种选择。
Finally, note all the options for dvipng. They control the appearance, via various anti-alisaing options etc. I tuned them extensively to get what I thought looked good.
最后,注意dvipng的所有选项。它们通过各种反alisaing选项等来控制外观。我对它们进行了广泛的调整,以获得我认为看起来不错的效果。
def geneq(f, eq, dpi, wl, outname, packages):
# First check if there is an existing file.
eqname = os.path.join(f.eqdir, outname + '.png')
# Open tex file.
tempdir = tempfile.gettempdir()
fd, texfile = tempfile.mkstemp('.tex', '', tempdir, True)
basefile = texfile[:-4]
g = os.fdopen(fd, 'w')
preamble = '\documentclass{article}\n'
for p in packages:
preamble += '\usepackage{%s}\n' % p
preamble += '\pagestyle{empty}\n\\begin{document}\n'
g.write(preamble)
# Write the equation itself.
if wl:
g.write('\\[%s\\]' % eq)
else:
g.write('$%s$' % eq)
# Finish off the tex file.
g.write('\n\\newpage\n\end{document}')
g.close()
exts = ['.tex', '.aux', '.dvi', '.log']
try:
# Generate the DVI file
latexcmd = 'latex -file-line-error-style -interaction=nonstopmode ' + \
'-output-directory %s %s' % (tempdir, texfile)
p = Popen(latexcmd, shell=True, stdout=PIPE)
rc = p.wait()
if rc != 0:
for l in p.stdout.readlines():
print ' ' + l.rstrip()
exts.remove('.tex')
raise Exception('latex error')
dvifile = basefile + '.dvi'
dvicmd = 'dvipng --freetype0 -Q 9 -z 3 --depth -q -T tight -D %i -bg Transparent -o %s %s' % (dpi, eqname, dvifile)
# discard warnings, as well.
p = Popen(dvicmd, shell=True, stdout=PIPE, stderr=PIPE)
rc = p.wait()
if rc != 0:
print p.stderr.readlines()
raise Exception('dvipng error')
depth = int(p.stdout.readlines()[-1].split('=')[-1])
finally:
# Clean up.
for ext in exts:
g = basefile + ext
if os.path.exists(g):
os.remove(g)
#2
1
If you want to convert parts of your code to png files, and not necessarily everything, have a look at the preview package. As per their README, it can extract parts of a Latex source file to separate dvi files, and those can be converted to png files. Another option instead of using dvipng to get PNGs would be to convert the generated PDF/ps files to PNG directly:
如果您希望将部分代码转换为png文件,而不是全部,请查看预览包。根据他们的自述文件,它可以提取乳胶源文件的一部分来分离dvi文件,这些文件可以转换为png文件。另一个选择不是使用dvipng来获取PNG,而是将生成的PDF/ps文件直接转换为PNG:
gs -sDEVICE=png16m -dTextAlphaBits=4 -r300 -dGraphicsAlphaBits=4 -dSAFER -dBATCH -dNOPAUSE -sOutputFile=file.png file.pdf