我目前正在使用python来自动化乳胶。
代码应允许用户将内容写入.txt文件,并且python可以从.txt中读取内容,然后将其写入.tex中,最后,乳胶可以将.tex编译为PDF
一切都很好,直到需要放入一个数字为止。因为将某些数字放入乳胶中需要某些代码。
例如:
用户在.txt中编写一个段落,如下所示:
Lorem Lorem Lorem
然后,他通过键入名称来指定要使用的图形。
somefigure.png
如何使用python代码将其转换为.tex:
Lorem Lorem Lorem
\begin{figure}[H]
\includegraphics[scale=0.5]{somefigure}
\caption{somefigure}
\end{figure}
困难在于以乳胶格式检测名称和范围。
我尝试过:
with open("INTRODUCTION.txt") as INTRODUCTIONTXT:
with open("INTRODUCTION.tex", "w") as INTRODUCTIONTEX:
for lines in INTRODUCTIONTXT:
INTRODUCTIONTEX.write(lines)
INTROFIG = open("INTRODUCTION.txt")
if "PNP.name.PNP" in INTROFIG:
print("\begin{figure}[H]\n\centering")
我将感谢您的答复!
答案 0 :(得分:1)
希望这段未经测试的代码可以完成您想要的
filename = 'INTRODUCTION'
# This is a raw (note the r - ignores \ escapes) multiline string.
# The {{ and }} become { and } and {0} will be replaced by the first argument
# of the format method.
figure_block = r'''\begin{{figure}}[H]
\includegraphics[scale=0.5]{{{0}}}
\caption{{{0}}}
\end{{figure}}
'''
with open(filename + '.txt') as txtfile:
with open(filename + '.tex', 'w') as texfile:
for line in txtfile:
stripped_line = line.strip() # remove white-space either side
if stripped_line.endswith('.png'): # self-explanatory -- yay python
texfile.write(figure_block.format(stripped_line))
else:
texfile.write(line)