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 preparing some slides with Beamer and I've just discovered that the listings package, which otherwise does exactly what I want, is missing the syntax for the Lua language. How can I configure listings to highlight Lua?

share|improve this question
1  
Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. –  Heiko Oberdiek Apr 3 at 10:55

2 Answers 2

up vote 9 down vote accepted

The premise of your question is wrong. The listings package defines no fewer than three Lua "dialects" in the file lstdvrs.dtx: [5.0]Lua, [5.1]Lua, and [5.2]Lua. Pick whichever is appropriate. You may want to change the way things look, but you shouldn't have to redefine all the syntax from scratch.

enter image description here

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage{inconsolata}
\usepackage{textcomp}
\usepackage{listings}

\lstdefinestyle{myLuastyle}
{
  language         = {[5.0]Lua},
  basicstyle       = \ttfamily,
  showstringspaces = false,
  upquote          = true,
}

\lstset{style=myLuastyle}

\begin{document}
\begin{lstlisting}
-- defines a factorial function
    function fact (n)
      if n == 0 then
        return 1
      else
        return n * fact(n-1)
      end
    end

    print("enter a number:")
    a = io.read("*number")        -- read a number
    print(fact(a))
\end{lstlisting}
\end{document}
share|improve this answer
    
Thanks for your answer, effectively I was wrong. So the problem is elsewhere, your sample doesn't work for me, I must miss the file lstdvrs.dtx, I'm under Debian Wheezy with package latex-beamer installed, does this extension packaged, and do you know it's name ? –  Rodolphe Apr 3 at 11:30
    
As far as I know, as long as your TeX distro comes with the listings package, you shouldn't have to worry about lstdvrs.dtx. All the necessary files should come with the package. Unfortunately, I'm not familiar with Debian. Perhaps you should ask a separate question about that specific problem. –  Jubobs Apr 3 at 11:33
    
I found where the languages are defined, there are files under /usr/share/texlive/texmf-dist/tex/latex/listings/lang*.sty there are a lot of language defined here but not Lua, I agree that the problem is more Debian related, so I'll look for a solution elsewhere, thanks again. –  Rodolphe Apr 3 at 11:41
1  
@Rodolphe: Looks like this should be of interest for you: How to install "vanilla" TeXLive on Debian or Ubuntu? –  Speravir Apr 4 at 0:29

There is dialect 5.0 defined:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{listings,inconsolata}

\begin{document}

\begin{lstlisting}[language={[5.0]Lua},basicstyle=\ttfamily\footnotesize,keywordstyle=\bfseries]
local words = io.open('hyphens-' .. tex.jobname .. '.txt', 'w');
local outchar = unicode.utf8.char
local function dumphyphens (head)
   local data = {}
   for v in node.traverse(head) do
       if v.id == node.id('glyph') then
         data[#data+1] = outchar(v.char);
       elseif v.id == node.id('disc') then
          data[#data+1] = '-'
       elseif v.id == node.id('glue') then
         data[#data+1] = outchar(32)
       elseif v.id == node.id('hlist') then
         data[#data+1] = dumphyphens(v.list)
       end
   end
   return table.concat(data)
end
callback.register ('hyphenate', function (head,tail)
   lang.hyphenate(head, tail) 
   words:write (dumphyphens(head) .. outchar(10))
   end)
\end{lstlisting}

\end{document}

enter image description here

share|improve this answer
    
Don't use \lst@definelanguage here. Use \lstdefinelanguage instead; see this. Also, if you want to use * as a keyword, you need to make it a letter first: alsoletter=*. –  Jubobs Apr 3 at 10:01
    
It makes absolutely no difference! –  Herbert Apr 3 at 11:13
    
Using \lst@definelanguage outside driver files can be associated with nasty side effects. See the question I link to and cgnieder's comment. –  Jubobs Apr 3 at 11:15
    
The only difference is that \lst@DefLang is set to \false. –  Herbert Apr 3 at 11:19

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.