在Latex中插入Python代码

这里指的插入是指最终能在生成的pdf中显示高亮的Python代码。

在Latex中插入Python代码,需要一个第三发的宏包pythonhighlight:

https://github.com/olivierverdier/python-latex-highlighting

下载pythonhighlight.sty后,将它放到你的.tex文件所在目录下。

然后声明要使用pythonhighlight,在tex文件内的导言区

usepackage{graphicx}
usepackage{pythonhighlight}

之后既可以在正文

添加代码了

egin{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}
egin{document}

egin{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}

原文地址:https://www.cnblogs.com/instant7/p/4190980.html