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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM