我正在准备一些幻灯片Beamer
,我刚刚发现这个listings
包缺少该Lua
语言的语法,而它本来可以满足我的要求。我该如何配置listings
以突出显示 Lua?
答案1
你的问题前提是错误的。该listings
包在文件中定义了不少于三种 Lua“方言”lstdvrs.dtx
: [5.0]Lua
、[5.1]Lua
和[5.2]Lua
。选择合适的。您可能想改变事物的外观,但您不必从头开始重新定义所有语法。
\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}
答案2
方言5.0有定义:
\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}