186

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.

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
Vincent Ramdhanie
  • 102,349
  • 23
  • 137
  • 192
  • 4
    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 Feb 04 '09 at 13:51

7 Answers7

172

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
  • 16,635
  • 9
  • 57
  • 81
  • 26
    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 Apr 11 '09 at 00:24
  • 12
    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 Apr 11 '09 at 01:40
160

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

Community
  • 1
  • 1
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 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 Jan 24 '10 at 13:58
  • 1
    @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 Jan 25 '10 at 12:45
  • 1
    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 May 25 '10 at 00:19
  • @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 May 25 '10 at 09:13
  • 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 Jun 16 '10 at 05:11
  • There is also a problem with minted, some publishers won't accept this because of pygments rwquirement. – Darek Oct 24 '12 at 21:23
  • 4
    `minted` is installed and working on both ShareLatex and Overleaf. Save yourselves the headache of installing Pygments ;) – grofte Apr 01 '18 at 17:17
  • I tired out `minted`, and it was great, however, it took some configuration steps. For anyone who is interested, if you have Python installed you can just do `pip install Pygments`. You then need to use the `-shell-escape` flag, when you call latex. If you use LyX, you need to enter this in tools > preferences > file handling > converters. Then depending on the converter you use, you enter -shell-escape in the converter field. Since I use pdflatex, I picked my converter as LaTeX(pdflatex) -> PDF (pdflatex). Then I set the converter field to be: `pdflatex -shell-escape $$i`. – Evan Rosica Feb 27 '19 at 23:25
  • I'm also quite happy with this solution. I am using it on emacs AucTeX. However, I am wondering if 1) is it possible to also see the syntax highlighted inside of the tex file that I am editing? 2) can I access the emacs mode corresponding to the language shown in the minted environment when the cursor runs over it? (something similar to [multi-web-mode](https://github.com/fgallina/multi-web-mode)). Thanks – Ajned Feb 09 '20 at 09:42
  • Its April 2021; `minted` package doesn't work on Arxiv https://arxiv.org/help/faq/mistakes#minted Atleast not without a lot of mess up. I was so happy about this minted package until I had to upload my paper to arxiv. :( – Thamme Gowda Apr 01 '21 at 06:26
  • @ThammeGowda I feel your pain, but the page you linked gives pretty detailed instructions of how to get minted to work with arXiv. Really, all that’s required is loading the package as `\usepackage[frozencache=true,cachedir=minted-cache]{minted}`, and submitting the cache directory along with the manuscript. (Admittedly this requires *creating* the cache in a first pass.) – Konrad Rudolph Apr 01 '21 at 09:07
  • @KonradRudolph Yes; I was able to do that and upload my paper to arxiv. Thanks for your awesome work with minted package. Great work and I love how nicely it highlights code. I am just joking about how well Overleaf simplified my dealings with *TeX echo system, now doing that `fronzencache` trick, felt like too much work :-D I really don't know why `-shell-escape` is needed for minted; so please excuse my ignorance. – Thamme Gowda Apr 02 '21 at 20:01
23

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
  • 14,298
  • 16
  • 57
  • 93
  • 1
    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 May 27 '10 at 17:44
  • On a `beamer` type document this is apparently the only functional option. Neither `minted` nor `listings` compile correctly with `beamer`. – Luís de Sousa Jul 08 '15 at 11:35
  • Yes, I have used `listings` with `beamer`, but you have to specify that frames that include listings are `[fragile]`. – Supernormal Sep 01 '16 at 09:23
12

I would use the minted package as mentioned from the developer Konrad Rudolph instead of the listing package. Here is why:

listing package

The listing package does not support colors by default. To use colors you would need to include the color package and define color-rules by yourself with the \lstset command as explained for matlab code here.

Also, the listing package doesn't work well with unicode, but you can fix those problems as explained here and here.

The following code

\documentclass{article}
\usepackage{listings}

\begin{document}
\begin{lstlisting}[language=html]
<html>
    <head>
        <title>Hello</title>
    </head>
    <body>Hello</body>
</html>
\end{lstlisting}
\end{document}

produces the following image:

enter image description here

minted package

The minted package supports colors, unicode and looks awesome. However, in order to use it, you need to have python 2.6 and pygments. In Ubuntu, you can check your python version in the terminal with

python --version

and you can install pygments with

sudo apt-get install python-pygments

Then, since minted makes calls to pygments, you need to compile it with -shell-escape like this

pdflatex -shell-escape yourfile.tex

If you use a latex editor like TexMaker or something, I would recommend to add a user-command, so that you can still compile it in the editor.

The following code

\documentclass{article}
\usepackage{minted}
\begin{document}

\begin{minted}{html}
    <!DOCTYPE html>
    <html>
       <head>
           <title>Hello</title>
       </head>

       <body>Hello</body>
    </html>
\end{minted}
\end{document}

produces the following image:

enter image description here

Community
  • 1
  • 1
Adam
  • 25,960
  • 22
  • 158
  • 247
11

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

SimplyKnownAsG
  • 904
  • 9
  • 26
ConcernedOfTunbridgeWells
  • 64,444
  • 15
  • 143
  • 197
4

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

Paul Biggar
  • 27,579
  • 21
  • 99
  • 152
-3

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

okm
  • 283
  • 6
  • 18
  • The link just shows specific settings for syntax highlighting Python code with the 'listings' package... – las3rjock Nov 14 '09 at 17:29