Python code in LaTeX

share on google plus share on facebook share on twitter share on linkedin share via email

Including Python code in LaTeX papers is very simple and convenient with the “listings” package. Documentation of the package is part of the (awesome) LaTeX wikibook

First, include the package in your document:

\documentclass{article}

\usepackage{listings}

\begin{document}
\end{document}

And then insert code directly in the document:

\lstset{language=Python}
\lstset{frame=lines}
\lstset{caption={Insert code directly in your document}}
\lstset{label={lst:code_direct}}
\lstset{basicstyle=\footnotesize}
\begin{lstlisting}
from brg.datastructures import Mesh

mesh = Mesh.from_obj('faces.obj')
mesh.draw()
\end{lstlisting}

Or add code from a file:

\lstinputlisting[language=Python]{mesh.py}

Leave a response

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>