[英]Converting TeX file to image in Python
使用這個公認的答案作為參考 ,我一直在嘗試將DF熊貓復制到 TeX, 將 PDF復制到 png食譜。 但是,我的Python 3(使用Jupyter Notebook通過Anaconda Navigator)給我帶來了一些麻煩。
我已經在本地磁盤上的以下默認位置安裝了MiKTeX:
C:\Users\USERNAME\AppData\Local\Programs\MiKTeX\2.9
我還在以下位置安裝了LaTex:
C:\Users\USERNAME\AppData\Local\Programs\Python\Python36-32\Lib\site-packages\notebook\static\components\MathJax\extensions\TeX
但是,當我使用Pandas df對象運行代碼塊時:
filename = 'out.tex'
pdffile = 'out.pdf'
outname = 'out.png'
template = r'''\documentclass[preview]{{standalone}}
\usepackage{{booktabs}}
\begin{{document}}
{}
\end{{document}}
'''
with open(filename, 'wb') as f:
f.write(bytes(template.format(df.to_latex()),'UTF-8'))
subprocess.call(['pdflatex', filename], shell=True)
subprocess.call(['convert', '-density', '300', pdffile, '-quality', '90', outname])
它只是返回“ 4”,而從原始df創建的唯一文件是“ out.tex ”。 我希望將DF轉換為png圖像,以便將整個過程保留在Notebook腳本中。
任何建議將不勝感激。 謝謝。
我使用以下兩個流程調用解決了此問題:
subprocess.call('latex -interaction=batchmode -output-directory='+ outpath + ' ' + input_tex_file, shell=True, stdout=open(os.devnull, 'wb'))
subprocess.call('dvipng -q* -T tight -o ' + output_png_file + ' ' + input_dvi_file, shell=True, stdout=open(os.devnull, 'wb'))
您必須首先從原始的乳膠數據創建.tex
文件。 然后這些命令首先創建.dvi
從文件(和一些額外的輔助文件) .tex
文件。 然后,您可以使用dvipng( https://ctan.org/pkg/dvipng?lang=en )創建一個.png
文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.