1

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!

6
  • 1
    \string\\ I guess Jul 11 at 14:42
  • Are you familiar with the luacode package? It provides a macro that may be of interest to you: \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})}}.
    – Mico
    Jul 11 at 15:31
  • 1
    Just curiosity. Why are you using lua to switch the language? Jul 11 at 15:33
  • @DavidCarlisle I tried that but it did not work, unfortunately I do not recall the exact error I got. Jul 12 at 21:17
  • 1
    @Mico Thank you very much! It was exactly what I needed! If you post it as an answer I will gladly accept it :) Jul 12 at 21:20

1 Answer 1

1

The luacode package is probably best known for providing the luacode and luacode* environments. However, that's not all this package has to offer. In particular, it also provides three LaTeX macros for passing string variables from TeX to Lua. They are

  • \luastring -- fully expands its argument

  • \luastringN -- does not expand its argument

  • \luastringO -- expands its argument exactly once

Importantly, the arguments of these macros are always treated as strings. E.g., \luastring{1} passes 1 as a string rather than as an integer to Lua.

Using \luastringN, you could simplify

\newcommand{\lc}[1]{\directlua{code.sel_lang("\luaescapestring{\unexpanded{#1}}")}

to

\newcommand{\lc}[1]{\directlua{code.sel_lang(\luastringN{#1})}}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .