I am new to latex and I wonder if I can copy some java code to my latex document, and use some kind of package or something where I can specify that from "here" to "here" I have javacode and I would like it to show linenumbers, syntax with colors and stuff


1 Answer 1


You can use the listings package. Some usage information can be found on Wikibooks:

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:

put your code here

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:


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:


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .