5

If I compile the following code with LuaLaTeX I get the word "RED" as output. The call of token.set_macro inside of luacode* seems to be noneffective.

\listfiles
\documentclass{minimal}
\usepackage{luacode}

\newcommand*\foo{RED}

\iftrue
\begin{luacode*}
  token.set_macro('foo', 'GREEN')
\end{luacode*}
\else  
  \directlua{token.set_macro('foo', 'GREEN')}
\fi  

\begin{document}
  \foo
\end{document}

After changing \iftrue to \iffalse I get "GREEN". Can anyone explain this behavior? Many thanks.

Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
rolfn
  • 939
  • 4
  • 13

1 Answers1

6

An environment is a group, if you add a group in the \directlua case, then you see the same:

\listfiles
\documentclass{minimal}
\usepackage{luacode}

\newcommand*\foo{RED}

\iffalse
\begin{luacode*}
  token.set_macro('foo', 'GREEN')
\end{luacode*}
\else
{  
  \directlua{token.set_macro('foo', 'GREEN')}
}
\fi  

\begin{document}
  \foo
\end{document}
David Carlisle
  • 757,742
  • 1
    Oh, so easy is the explanation. I must write token.set_macro('foo', 'GREEN', 'global'). Many thanks, David. – rolfn Dec 12 '18 at 08:24
  • 1
    @rolfn you could, although personally I'm not convinced by the need for the luacode environments, and I'd probably use \directlua directly. – David Carlisle Dec 12 '18 at 08:57