Você pode usar o listings
package . Algumas informações de uso podem ser encontradas em Wikilivros :
Using the package
listings
you can add non-formatted text as you would do with\begin{verbatim}
but its main aim is to include the source code of any programming language within your document. It supports highlighting of all the most common languages and it is highly customizable. If you just want to write code within your document the package provides the lstlisting environment:\begin{lstlisting} put your code here \end{lstlisting}
Another possibility, that is very useful if you created a program on several files and you are still editing it, is to import the code from the source itself. This way, if you modify the source, you just have to recompile the LaTeX code and your document will be updated. The command is:
\lstinputlisting{source_filename.py}
in the example there is a Python source, but it doesn't matter: you can include any file but you have to write the full file name. It will be considered plain text and it will be highlighted according to your settings, that means it doesn't recognize the programming language by itself. You can specify the language while including the file with the following command:
\lstinputlisting[language=Python]{source_filename.py}