4

I am a newbie in LuaTeX, but it is very interesting.

Sometimes, I need process some macro arguments by Lua code, so it will be useful that if we can access the variables defined in Lua. An example:

\def\foo#1{%
  \directlua{var = [[#1]] .. " " .. system}
  % other part of this macro
  ...}

\foo{typesetting}

\bye

how can I access the var defined in the \directlua? Say, after \foo{typesetting}, I can get typesetting system by only using the var.


Another question is that how can I access the TeX command in \directlua environment?

1
  • 1
    What do you mean by "how can I access the TeX command in \directlua environment?" What kind of access?
    – topskip
    Commented Apr 22, 2013 at 20:02

2 Answers 2

8

See my answer here: Which Lua environment should I use with LuaTeX (LuaLaTeX)?

You can do this:

\newcommand\myrepeat[2]{%
\luaexec{
  for i=1,#1 do
     tex.sprint("\luatexluaescapestring{#2}")
   end
 }}

\myrepeat{4}{Hello world}

to access the macro parameters.

\newcommand\mymacro[1]{%
   \directlua{myvar=\luatexluaescapestring{#1}}}

should be enough to assign parameter 1 to the variable myvar.

For your plain TeX solution, this prints out 'typesetting system':

\def\foo#1{%
  \directlua{var = [[\luaescapestring{#1}]] .. " system"
  tex.sprint(var)
}% other part of this macro
}

\foo{typesetting}

\bye
5
  • Thanks. But I am using plain LuaTeX(so, I can't use the \luatexluaescapestring), are there any methods to achieve that? As the example I have given, after \foo{typesetting}, I should get typesetting system.
    – yanpengl
    Commented Apr 23, 2013 at 1:42
  • thanks, topskip. I have read your answer carefully. What part of the lua programming language do you think it is enough to program in lua when using LuaTeX?
    – yanpengl
    Commented Apr 23, 2013 at 10:18
  • @TimLi Since Lua's syntax is not really big, I'd say "everything except for the : notation". But this depends on the problem you're trying to solve. If you look at my profile/answers, you find some LuaTeX answers that manipulate the nodelists. These might give you an idea about the average LuaTeX program. You can also look at github.com/speedata/publisher/tree/develop (in the subdir "src/lua" for a more advanced LuaTeX program.
    – topskip
    Commented Apr 23, 2013 at 10:20
  • Thanks for your advice(now, I think I should read the PIL from cover to cover). I have read your answers to somebody before. It is impressive that your magic Lua code can manipulate the node lists so freely. At that time, I decide to learn Lua and LuaTeX.
    – yanpengl
    Commented Apr 23, 2013 at 10:31
  • @TimLi it's not my magic but the magic of the LuaTeX developers (by providing callbacks and such). My routines tend to be rather simple.
    – topskip
    Commented Apr 23, 2013 at 10:35
1

I suppose you mean using the variable also outside of Lua, right?

\def\foo#1{%
  \directlua{var = "Year: " .. [[\luaescapestring{#1}]]
    tex.sprint(var)
    tex.sprint{"\string\\def\string\\var{\luaescapestring{#1}}"}
  }% other part of this macro
}

\foo{\the\year}

\var
\bye

with the output:

enter image description here

3
  • thanks. Is it mean that I write \directlua{tex.sprint"\string\\def\string\\var{hello}"} is like writing \def\var{hello} directly in the Knuth82? It seems like so.
    – yanpengl
    Commented Apr 23, 2013 at 9:55
  • sure, but that is only a simple example. There can be other sitatuations where it makes sense to define a TeX macro inside Lua.
    – user2478
    Commented Apr 23, 2013 at 9:58
  • yeah, I see its potential. We can deal with something(Do you think CVS is OK?) by using Lua, which maybe is difficult for TeX82. Where can I find some examples about using Lua in LuaTeX? (I am learning the Lua programming language, so I only want some examples which can show me what LuaTeX can do better and easier than TeX82 or pdfTeX)
    – yanpengl
    Commented Apr 23, 2013 at 10:13

You must log in to answer this question.

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