Usually in luacode { and } are passed through to Lua if not used as the argument to a TeX command, however if an entire segment to end of line is a brace group the braces are dropped in #1\@nil due to TeX delimited argument parsing rules.
at https://tex.stackexchange.com/a/762809/1090 I suggest a fix of adding and removing a token so that the delimited argument is never a single brace group.
\documentclass{standalone}% compile with lualatex
\usepackage{luacode}
\makeatletter
\show\luacode@strip@sp@look
\renewcommand \luacode@strip@sp@look {%
\expandafter\ifx\luacode@spaceother\@let@token
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi{%
\afterassignment\luacode@strip@sp@peek
\let\@let@token=
}{%
\luacode@strip@sp@def!%
}}
\def \luacode@strip@sp@def #1\@nil{%
\expandafter\def\expandafter\luacode@curr\expandafter{\@gobble#1}}
\makeatother
\begin{document}
\begin{luacode}
local f = function(x, y, opt)
opt = opt or {}
opt.sg = opt.sg or '+'
if opt.sg == '+' then return x+y
else
return x-y
end
end
local x =
f(2,
2,
{sg='+'}
) -- < this causes an error with luacode, but not luacode*
tex.print("Result ="..x)
\end{luacode}
\end{document}
Usually in
luacode{and}are passed through to Lua if not used as the argument to a TeX command, however if an entire segment to end of line is a brace group the braces are dropped in#1\@nildue to TeX delimited argument parsing rules.at https://tex.stackexchange.com/a/762809/1090 I suggest a fix of adding and removing a token so that the delimited argument is never a single brace group.