[Tex/LaTex] Lua syntax for the listings package

listingslua

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?

Best Answer

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}