The easiest way to go is to create a custom float using the float
package, and put the lstlistings
environment inside it. Here's a quick-and-dirty example:
\documentclass[a4paper]{article}
\usepackage{listings}
\usepackage{float}
\usepackage{lipsum} % used to insert dummy text; not required
\floatstyle{plain} % optionally change the style of the new float
\newfloat{Code}{H}{myc}
\lstloadlanguages{Python}
\begin{document}
\lstset{basicstyle=\ttfamily\tiny}
\lipsum[1-4] % some dummy text to get to the bottom of the page
\begin{Code}
\centering
\begin{lstlisting}[language=Python]
def jacobian(function, variablelist):
"""
Calculates symbolically the Jacobian of the vector with respect to
the provided variables. Returns a square matrix
"""
n=len(variablelist)
J=np.asmatrix(np.zeros((n,n)),dtype=sy.Symbol)
for i in range(n):
for k in range(n):
J[i,k]=function[i,0].diff(variablelist[k])
return J
def vector(*arglist):
"""
A shorthand for defining a symbolic column vector. Arguments are
supplied as a normal comma-separated list.
"""
return np.asmatrix(np.array(arglist), dtype=sy.Symbol).transpose()
\end{lstlisting}
\caption{This is my code. There are many like it, but this one is mine.}
\end{Code}
\end{document}
You can use captions, labels, list of code samples, etc. in the same way you would with normal figures. Check the float
package for some extra information about customizing those.