Search code examples
syntaxpackagelatexhighlight

LaTeX package for syntax highlighting of code in various languages


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.


Solution

  • 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}