views:

10496

answers:

8

I am looking for a LaTeX package that does syntax highlighting on code. For example, right now I use the verbatim block to write code:

\begin{verbatim}
    <html>
       <head>
           <title>Hello</title>
       </head>
       <body>Hello</body>
    </html>
\end{verbatim}

And this works fine to display the code on my document. But, suppose I wanted to highlight the HTML markup the way an IDE would in the output document? Is there a package that could help?

I would like to do the same for various languages such as Java, C#, HTML, CSS and so on.

+3  A: 

LGrind does this. It's a mature LaTeX package that's been around since adam was a cowboy and support for many programming languages.

ConcernedOfTunbridgeWells
+41  A: 

You can use the listings package. It supports many different languages and there are lots of options for customising the output.

\documentclass{article}
\usepackage{listings}

\begin{document}
\begin{lstlisting}[language=html]
<html>
    <head>
        <title>Hello</title>
    </head>
    <body>Hello</body>
</html>
\end{lstlisting}
\end{document}
ChrisN
Excellent...this is exactly what I was looking for. Thanks.
Vincent Ramdhanie
Does this do colors by default? I'm not seeing colored output in my test usage, and I'm not sure if that's an error on my part, or simply not a feature.
Benson
I found an answer here: http://www.tjansson.dk/?p=419. You can simply use the lstset command to set all kinds of styles for different properties.
Benson
+4  A: 

I'm not satisfied with lstlisting. It does not highlight XML code at all and screws up when using foreign Unicode characters. JavaScript is not supported, too, as it seems.

Can LGrind handle such things?

webjunkie
+2  A: 

I mostly use lstlistings in papers, but for coloured output (for slides) I use pygments instead.

Paul Biggar
+6  A: 

I recommend Pygments. It accepts a piece of code in any language and outputs syntax highlighted LaTeX code. It uses fancyvrb and color packages to produce its output. I personally prefer it to the listing package. I think fancyvrb creates much prettier results.

reprogrammer
While I admire the macrology behind listings, I also agree that pygments has the edge: nicer highlighting, easier customisation, some ability to mix syntaxes, and the ability to output to formats other than Latex, such as HTML.
Charles Stewart
+1  A: 

I would suggest defining your own package based on the following tex code; this gives you complete freedom. http://ubuntuforums.org/archive/index.php/t-331602.html

Thanks. I will look into this.
Vincent Ramdhanie
The link just shows specific settings for syntax highlighting Python code with the 'listings' package...
las3rjock
+11  A: 

After asking a similar question I’ve created another package which uses Pygments, and offers quite a few more options than texments. It’s called minted and is quite stable and usable.

Just to show it off, here’s a code highlighted with minted:

Example code

Konrad Rudolph
This is actually quite a good job. Having to install Pygments does pose a bit of a challenge but I'm sure most persons will be willing to do so.
Vincent Ramdhanie
@Vincent: Unfortunately, installing Pygments on Windows is quite a bit more complicated at the moment (the user has to adapt the `PATH` variable and create a cmd script). I’m hoping to convince the Pygments maintainers to ship `pygmentize` with an `.exe` wrapper to make this step easier.
Konrad Rudolph
After trying both listings and minted/Pygments I decided minted was much better (more flexible). HOWEVER it will require jumping through a dozen hoops, upgrading to MiKTeX 2.8, installing Python, installing other components and so on. Minted documentation is poor and it doesn't support breaking long lines.But eventually I got it to render Java/XML in Eclipse colours! http://www.jevon.org/wiki/Eclipse_Pygments_Style
jevon
@soundasleepful: The minted documentation is continually updated. ;-) I actually find it quite extensive (of course I’m biased) but I’m happy to address any complaints. As it stands, the “requirements” section is admittedly awful.
Konrad Rudolph
It's also extremely easy to define your own Lexers for new languages in Pygments (they're just regular expressions), so if you're working with weird and esoteric academic programming languages, Pygments/minted is definitely the best.
jevon
A: 

With minted is it possible to format a piece of source code to go inline? Much like \verb and \lstinline.

Romildo
@Romildo: this should be an own question. But for now the answer is simply “no”, because pygments doesn’t support this yet and I didn’t think there actually was a use-case for this. I have opened a feature request for this: http://code.google.com/p/minted/issues/detail?id=15
Konrad Rudolph