Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question

migrated from stackoverflow.com May 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
add comment

3 Answers

up vote 7 down vote accepted

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}
\lstloadlanguages{Ruby}
\lstset{%
basicstyle=\ttfamily\color{black},
commentstyle = \ttfamily\color{red},
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.

share|improve this answer
add comment

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

enter image description here

share|improve this answer
add comment

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.

share|improve this answer
    
Welcome to TeX.sx! –  hpesoj626 Nov 20 '12 at 9:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.