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:

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



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
    What do you mean by "how can I access the TeX command in \directlua environment?" What kind of access?
    – topskip
    Apr 22, 2013 at 20:02

2 Answers 2


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

You can do this:

  for i=1,#1 do

\myrepeat{4}{Hello world}

to access the macro parameters.


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

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

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


  • 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
    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
    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
    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
    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
    Apr 23, 2013 at 10:35

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

  \directlua{var = "Year: " .. [[\luaescapestring{#1}]]
  }% other part of this macro



with the output:

enter image description here

  • 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
    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
    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
    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 .