latex插入python代码

来源:互联网 发布:mac macintosh hd 编辑:程序博客网 时间:2024/04/28 15:33

原文链接:http://www.cnblogs.com/prettysmc/p/5497144.html

1.缘由:最近在用latex写本科生毕业论文,在附录部分需要插入python代码,准确来讲是ipython的代码,用的是python的科学计算库anaconda。百度了好多解决方法,发现总有一些错误,比如improper alphabetic constant, undefined control sequence等等。

2. 解决方法:

  • 下载地址https://github.com/olivierverdier/python-latex-highlighting
  • 下载文件pythonhighlight.sty, 放到自己的.tex文件目录下
  • 将程序代码源文件也放到.tex文件目录下
  • 在latex导言区输入\usepackage{graphicx}  \usepackage{pythonhighlight}
  • 在需要插入代码的地方
    \inputpython{python_file.py}{开始行数}{结束行数}

     

  • 这种方法比直接输\begin{python}\end{python}成功率高
  • 但是这种方法的缺点是不能有中文注释
0 0