I have a multi-language CV written in LuaLaTeX. To switch between the different languages I am using two Lua functions that are called through these TeX commands:
\directlua{code = require("code")}
\newcommand{\lc}[1]{\directlua{code.sel_lang(#1)}}
\newcommand{\setlang}{\directlua{code.set_lang()}}
\begin{document}
\setlang{}
\lc{{
eng="Ravenna, Italy",
ita="Ravenna, Italia",
swe="Ravenna, Italien"
}}
\lc{{
eng="B.Sc.\\ in Computer Science",
ita="Laurea triennale in Scienze Informatiche",
swe="Kandidatexamen i datavetenskap"
}}
The Lua code is the following:
-- Language Table
local languages = {}
languages["eng"] = "english"
languages["ita"] = "italian"
languages["swe"] = "swedish"
-- Main Language
local main_language = "english"
-- Language defined in the jobname cmd parameter
local cmd_language = string.sub(tex.jobname, -3)
texio.write_nl("Command line language: "..cmd_language)
local language = languages[cmd_language]
-- Set language for TeX directives
function set_document_language()
texio.write_nl("Setting babel language")
tex.sprint("\\selectlanguage{"..language.."}")
end
-- Print text in the desired language
function select_language(langmap)
if (langmap[cmd_language] == nil) then
tex.sprint("!! Missing Text !!")
end
tex.sprint(langmap[cmd_language])
end
return { sel_lang = select_language, set_lang = set_document_language }
This used to work with an older version of LuaLaTeX but now the "\\
" after B.Sc. trigger the following error:
[\directlua]:1: invalid escape sequence near '"B.Sc.\A'.
\lc #1->\directlua {code.sel_lang(#1)}
Attempt to fix the error
To fix the error I have tried to pass the Lua function an escaped string instead:
\newcommand{\lc}[1]{\directlua{code.sel_lang("\luaescapestring{\unexpanded{#1}}")}}
and I have modified the Lua function as follows:
-- Print text in the desired language
function select_language(langmap)
texio.write_nl("String received:" ..langmap) --debug
string = load("return "..langmap)()
if (langmap[cmd_language] == nil) then
tex.sprint("!! Missing Text !!")
end
tex.sprint(langmap[cmd_language])
end
but now I get errors like the following one:
String received:{ eng="ABOUT ME", ita="PRESENTAZIONE", swe="OM MIG" }...exmf-dist/tex/luatex/luaotfload/luaotfload-auxiliary.lua:970: attempt to call a nil value (field 'lower').
where Lua is trying to call non-existent functions on the table structure.
Can you give me an idea on how to proceed/what to try?
I hope the issue is understandable and I apologize for any mistake and unclear explanation, I am not an expert in either Tex or Lua.
Thank you very much!
\string\\
I guess\luastringN
, which passes its argument unexpanded to Lua. It would allow the following, simpler definition of\lc
:\newcommand{\lc}[1]{\directlua{ code.sel_lang(\luastringN{#1})}}
.