12

I used the following in the past and it worked:

\documentclass{minimal}
\usepackage{luacode,luatextra}
\begin{document}

\begin{luacode*}
MyVal=123
tex.print("\\def\\MyVal{"..MyVal.."}")
\end{luacode*}

MyVal=\MyVal
\end{document}

and it worked fine.

Today I had to use the same source file and now this bit of code no longer defines a macro that can be called in lualatex, i.e. I now get Undefined command message.

I read the following: Which Lua environment should I use with LuaTeX (LuaLaTeX)? and Create macros inside Lua block

I tried luacode sans and with '*' as well as tex.sprint.

I attempted tex.tprint, but I will have to delve in the documentation about that.

My questions then are:

  1. Has anything changed in the last two months?
  2. How would one do the equivalent of the following in lualatex:

    \def{Myval}{100}
    

I also added \noexpand and changed \\ to \, to no avail.

Louis
  • 1,471

2 Answers2

8

The luacode* environment forms a group, in common with other LaTeX environments. Thus if you want to use this approach and have the value 'escape' then you will need to use \gdef

\documentclass{article}
\usepackage{luacode,luatextra}
\begin{document}

\begin{luacode*}
MyVal=123
tex.print("\\gdef\\MyVal{"..MyVal.."}")
\end{luacode*}

MyVal=\MyVal
\end{document}

As observed in Which Lua environment should I use with LuaTeX (LuaLaTeX)?, the best plan is to use a separate file and load it without grouping, etc.

\RequirePackage{filecontents}
\begin{filecontents*}{\jobname.lua}
MyVal=123
tex.print("\\def\\MyVal{"..MyVal.."}")
\end{filecontents*}

\documentclass{article}
\begin{document}

\directlua{require("\jobname.lua")}

MyVal=\MyVal
\end{document}
Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
  • As a ConTeXt user who regularly uses lua to define macros, I find it strange that the luacode* environment creates a group. Writing lua code in a separate file is fine, but sometimes having all related macros in one file is easier to maintain. – Aditya Aug 19 '13 at 23:43
  • 1
    @Aditya LaTeX's \begin...\end always creates a group (except \begin{document}, which deliberately undoes this). Normally if you want to avoid it you use \foo...\endfoo instead, although this is not officially supported. It's of course possible to undo the group creation: it might well be sensible here but that is for the author of luacode* to decide and document! – Joseph Wright Aug 20 '13 at 05:53
  • 1
    (\document starts with \endgroup and \enddocument contains a \begingroup to 'neutralise' the matching grouping primitives in \begin and \end.) – Joseph Wright Aug 20 '13 at 05:55
  • 1
    @Aditya I do see the point about inline code for short sections (we have some in expl3). – Joseph Wright Aug 20 '13 at 05:56
  • @JosephWright I would not use the include method in my case, I define macros with values to a problem and I use the values to calculate the answers when I generate the solutions. I could name each problem's set different and according to the problem but that would mean that the text and macro's values become to far separated and I would page too many times up and down while I edit the document. (And pdfsync functionality does not return to lua code.) – Louis Aug 26 '13 at 22:47
7

If you only need to export a value as a macro, i.e. a macro without a signature, then you could also use the token library of LuaTeX. The same as in Joseph's answer applies here: Because luacode* is grouping you have to add the "global" specifier.

\documentclass{article}
\usepackage{luacode}
\begin{luacode*}
MyVal = 123
token.set_macro("MyVal",MyVal,"global")
\end{luacode*}
\begin{document}
\MyVal
\end{document}

enter image description here


In ConTeXt MkIV you do not need to declare the control sequence global because here \startluacode ... \stopluacode is not grouping.

\startluacode
MyVal = 123
token.set_macro("MyVal",MyVal)
\stopluacode
\starttext
\MyVal
\stoptext
Henri Menke
  • 109,596