LaTeX代码环境配置:使用LaTeX展

发布时间:2019-08-26 07:55:33编辑:auto阅读(2047)

    翻译 https://stackoverflow.com/questions/3175105/writing-code-in-latex-document 内的内容。
    其实就是使用Listings包,一个例子如下:
    在正文前(\begin{document}之前)使用如下代码设置参数:

    \usepackage{listings}
    \usepackage{color}
    
    \definecolor{dkgreen}{rgb}{0,0.6,0}
    \definecolor{gray}{rgb}{0.5,0.5,0.5}
    \definecolor{mauve}{rgb}{0.58,0,0.82}
    
    \lstset{frame=tb,
      language=Python,
      aboveskip=3mm,
      belowskip=3mm,
      showstringspaces=false,
      columns=flexible,
      basicstyle={\small\ttfamily},
      numbers=none,
      numberstyle=\tiny\color{gray},
      keywordstyle=\color{blue},
      commentstyle=\color{dkgreen},
      stringstyle=\color{mauve},
      breaklines=true,
      breakatwhitespace=true,
      tabsize=3
    }

    在正文里面的时候在lstlisting环境内放置代码即可,例子如下:

    \begin{lstlisting}
    import numpy as np
    if __name__ == '__main__':
    	print(np.arange(10))
    \end{lstlisting}

    结果如下:
    result

关键字