Latex Coloured Code Listings

Easy way to include code in your latex document and have the syntax coloured correctly

\usepackage{color}
\usepackage{listings}

\lstset{
	tabsize=4,
	language=matlab,
        basicstyle=\scriptsize,
        %upquote=true,
        aboveskip={1.5\baselineskip},
        columns=fixed,
        showstringspaces=false,
        extendedchars=true,
        breaklines=true,
        prebreak = \raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}},
	frame=single,
        showtabs=false,
        showspaces=false,
        showstringspaces=false,
        identifierstyle=\ttfamily,
        keywordstyle=\color[rgb]{0,0,1},
        commentstyle=\color[rgb]{0.133,0.545,0.133},
        stringstyle=\color[rgb]{0.627,0.126,0.941},
	language=Java
}
\begin{document}
\lstinputlisting[title=\textbf{Source Code: filename.java}]{filename.java}
\end{document}

Related Posts:

This entry was posted in Off Topic and tagged . Bookmark the permalink.

2 Responses to Latex Coloured Code Listings

  1. korbsan says:

    very helpful, thanks!

  2. Nick says:

    Thank you. Isn’t it strange, though, that there are no default colored modes for any language?

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>