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?
Asked
Active
Viewed 4,554 times
12
-
1Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. – Heiko Oberdiek Apr 03 '14 at 10:55
2 Answers
12
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:")
a = io.read("*number") -- read a number
print(fact(a))
\end{lstlisting}
\end{document}
jub0bs
- 58,916
-
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 packagelatex-beamerinstalled, does this extension packaged, and do you know it's name ? – Rodolphe Apr 03 '14 at 11:30 -
As far as I know, as long as your TeX distro comes with the
listingspackage, you shouldn't have to worry aboutlstdvrs.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. – jub0bs Apr 03 '14 at 11:33 -
I found where the languages are defined, there are files under
/usr/share/texlive/texmf-dist/tex/latex/listings/lang*.stythere are a lot of language defined here but notLua, I agree that the problem is moreDebianrelated, so I'll look for a solution elsewhere, thanks again. – Rodolphe Apr 03 '14 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 04 '14 at 00:29
3
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}

-
Don't use
\lst@definelanguagehere. Use\lstdefinelanguageinstead; see this. Also, if you want to use*as a keyword, you need to make it a letter first:alsoletter=*. – jub0bs Apr 03 '14 at 10:01 -
-
Using
\lst@definelanguageoutside driver files can be associated with nasty side effects. See the question I link to and cgnieder's comment. – jub0bs Apr 03 '14 at 11:15 -