![获取要以 LaTeX 格式格式化的 Python 程序的输出?](https://linux22.com/image/420637/%E8%8E%B7%E5%8F%96%E8%A6%81%E4%BB%A5%20LaTeX%20%E6%A0%BC%E5%BC%8F%E6%A0%BC%E5%BC%8F%E5%8C%96%E7%9A%84%20Python%20%E7%A8%8B%E5%BA%8F%E7%9A%84%E8%BE%93%E5%87%BA%EF%BC%9F.png)
我的朋友正在为我编写一些 Python 代码来帮我做一些计算。有没有办法让我将这些计算的输出自动转换为 LaTeX 格式?
没什么太复杂的,输出只是一堆有理函数。我希望能够自动将它们放入 LaTeX 编码中。有没有简单的方法可以做到这一点?
答案1
在 Rstudio 中编译的文件示例test.Rnw
。可以评估 phyton 代码并显示结果,或者仅显示突出显示的代码,或者同时显示代码和结果。该文件在内部导出到 LaTeX(使用 R 和 knitr)以获取 PDF。
\documentclass[twocolumn]{article}
\begin{document}
\section{Phyton code and result}
<<test0, engine='python'>>=
import random
print(random.randint(0,9))
@
\section{Code without result}
<<test2, echo=T, eval=F, comment="", engine='python'>>=
import calendar
yy = 2020 # year
mm = 9 # month
print(calendar.month(yy, mm))
@
\section{Result without code}
<<test3, echo=F, comment="", engine='python'>>=
import matplotlib.pyplot as plt
plt.plot([1,2,3,4,10])
@
\end{document}