wsxxz 发表于 2015-12-2 12:55:56

在Latex中插入Python代码

  这里指的插入是指最终能在生成的pdf中显示高亮的Python代码。
  在Latex中插入Python代码,需要一个第三发的宏包pythonhighlight:
  https://github.com/olivierverdier/python-latex-highlighting
  下载pythonhighlight.sty后,将它放到你的.tex文件所在目录下。
  然后声明要使用pythonhighlight,在tex文件内的导言区
  \usepackage{graphicx}
\usepackage{pythonhighlight}
  之后既可以在正文
  添加代码了
  \begin{python}
#
from pyx import *
  g = graph.graphxy(width=8)
g.plot(graph.data.function("y(x)=sin(x)/x", min=-15, max=15))
g.writePDFfile("function")
print r'\includegraphics{function}'
\end{python}
  \end{document}
  截图:

  完整示例:
  \documentclass{article}
\usepackage{graphicx}
\usepackage{pythonhighlight}
\begin{document}
  \begin{python}
#
from pyx import *
  g = graph.graphxy(width=8)
g.plot(graph.data.function("y(x)=sin(x)/x", min=-15, max=15))
g.writePDFfile("function")
print r'\includegraphics{function}'
\end{python}
  \end{document}
页: [1]
查看完整版本: 在Latex中插入Python代码