# Lua syntax for the listings package

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?

-
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

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.

\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:")
print(fact(a))
\end{lstlisting}
\end{document}

-
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
@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 data = {}
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

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