Ruby highlighter for LaTeX

I'm looking for a package that can highlight ruby source code in LaTeX. I've found this thread: http://stackoverflow.com/questions/1966425/source-code-highlighting-in-latex

But it is from 2009, and I'm wondering if there is any better solution. I can't get minted to work on my Mac. Maybe someone knows a better, easier solution. I'm currently playing with

\begin{lstlisting}[
label=listing:RubyTest,
float=h,
caption=test.rb,
firstnumber=1,
language=Ruby
]
a = 5
b = a * 5
puts b
puts b + 3

class Test < Test::SomeClass
@test = 3
def bar
if foo
puts "foo"
else
puts "bar
end
end
end
\end{lstlisting}


But it gets only formatted without any colors.

-

migrated from stackoverflow.comMay 4 '11 at 12:17

This question came from our site for professional and enthusiast programmers.

If you are using OS X 10.5 or 10.6, just run easy_install pygments on the command line and you should be good to go with minted. – Sharpie May 4 '11 at 16:43

To include color in the code listing, you must specify what colors you want with the \lstset command, as shown in the following code :

\documentclass[]{minimal}

\usepackage[dvipsnames]{xcolor}

\usepackage{listings}
\lstset{%
basicstyle=\ttfamily\color{black},
keywordstyle=\ttfamily\color{blue},
stringstyle=\color{orange}}

\begin{document}

\begin{lstlisting}[language=Ruby]

#this is a comment

a = 5
b = a * 5
puts b
puts b + 3

class Test < Test::SomeClass
@test = 3
def bar
if foo
puts "foo"
else
puts "bar"
end
end
end
\end{lstlisting}

\end{document}


As for the minted package, you also have to install Pygments, which is external to TeX. Once this is done, minted will work fine.

Added: The difference between my solution and Mike Renfro's is where the info about the color is given. Mike's solution is best when you have different formats to use and often change the settings. What I wrote is best if you always want to use the same (color) settings throughout the document (even when possibly using different programming languages). You may mix the two : set some global options and occasionnaly use other options for a specific code listing.

-

The best for me is pygmentize (python library)

as output you may set latex file, input for example ruby or anything other and you will get beautiful colored code.

-
Welcome to TeX.sx! – hpesoj626 Nov 20 '12 at 9:34

You're missing various style options for the listings package (and a closing quote on bar). You might also need the color package unless it's loaded through some other package. For a minimal example, try

\documentclass{article}
\usepackage[english]{babel}
\usepackage{xcolor,listings}

\begin{document}

\begin{lstlisting}[
label=listing:RubyTest,
float=h,
caption=test.rb,
firstnumber=1,
language=Ruby,
basicstyle=\ttfamily,
keywordstyle=\color{red},
stringstyle=\color{blue},
]
a = 5
b = a * 5
puts b
puts b + 3

class Test < Test::SomeClass
@test = 3
def bar
if foo
puts "foo"
else
puts "bar"
end
end
end
\end{lstlisting}

\end{document}


for the following result

-