LaTeX package for syntax highlighting of code in various languages

SyntaxPackageLatexHighlight

Syntax Problem Overview


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.

Syntax Solutions


Solution 1 - Syntax

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}

Solution 2 - Syntax

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

Solution 3 - Syntax

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.

Solution 4 - Syntax

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.

Solution 5 - Syntax

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

Solution 6 - Syntax

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

Solution 7 - Syntax

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVincent RamdhanieView Question on Stackoverflow
Solution 1 - SyntaxChrisNView Answer on Stackoverflow
Solution 2 - SyntaxKonrad RudolphView Answer on Stackoverflow
Solution 3 - SyntaxreprogrammerView Answer on Stackoverflow
Solution 4 - SyntaxConcernedOfTunbridgeWellsView Answer on Stackoverflow
Solution 5 - SyntaxAdamView Answer on Stackoverflow
Solution 6 - SyntaxPaul BiggarView Answer on Stackoverflow
Solution 7 - SyntaxokmView Answer on Stackoverflow