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

文章来自:http://www.cnblogs.com/instant7/p/4190980.html
© 2021 jiaocheng.bubufx.com  联系我们
ICP备案:鲁ICP备09046678号-3