在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}

posted @ 2014-12-29 08:55  KevinHwang  阅读(21503)  评论(0编辑  收藏  举报