11

I have just started exploring the world of LuaTeX and wanted to write a piece of code, that prints a name to a new line in the document.

\directlua{
for i=1,3,1 do
tex.print("FOO")
end
}

which should produce the following output:

FOO
FOO
FOO

rather than:

FOO FOO FOO

How do I use Lua correctly? I have tried messing with \\ and many other things over the course of an hour. Am I approaching this problem from the wrong angle? Any help is highly appreciated.

3
  • 1
    Try tex.print("Foo", "\par")
    – Marco
    Apr 23, 2012 at 21:22
  • 1
    @Marco Somehow I have the feeling that it is pure luck that this works. Lua does not see the \par but something else, which I don't know at the moment.
    – topskip
    Apr 24, 2012 at 6:29
  • Khaled explains why in his answer. (tex.stackexchange.com/a/53075)
    – topskip
    Apr 24, 2012 at 9:35

3 Answers 3

15

If you are using LaTeX (LuaLaTeX), I'd strongly advice against using \directlua{} for more than a simple call to execute another Lua file. \directlua{} is in no way safe. For example:

  • if you use a Lua comment (--), your code gets Q="!%"§I. This is because in \directlua{} everything is read on one line.
  • If you try to use a literal % sign, for example in tex.print() you... well, try yourself . Good luck.
  • If you try to use an active character such as ~ surprising results are guaranteed.
  • When you want to use tex.print() with a macro, such as \par, you have do crazy stuff to not let TeX see the macro before Lua sees the macro.
  • And good luck inserting strings like "\n" for a new line character.

Now what to do? Use the environment luacode* from the luacode package:

\begin{luacode*}
  for i=1,3,1 do
    tex.print("FOO\\par")
  end  
\end{luacode*}

If you can't use the luacode package (for example when you use plain TeX), use dofile() or require() to load a package and put your code there. TeX can't find and interpret the code there, only Lua. And then you can write

for i=1,3,1 do
  tex.print("FOO\\par")
end

without trouble.

9

tex.print appends \endlinechar to each string it prints except the last, so just print an empty string at the end.

\directlua{
for i=1,3,1 do
tex.print("FOO", "")
end
}
\bye
6

Two problems here: A "\\" in Lua just gets you one literal backslash which TeX stumbles over; as in the answers to this question you must use "\\\\" or [[\\]]. Also, \directlua expands control sequences within it; so if you want to pass a control sequence verbatim into Lua you'll need to either\noexpand it or use \luaexec instead.

Any of these three would work:

\directlua{
    for i=1,3,1 do
    tex.print([[FOO\noexpand\\]])
    end
}

\luaexec{
    for i=1,3,1 do
    tex.print([[FOO\\]])
    end
}

\luaexec{nl = [[\\]]}
\directlua{
    for i=1,3,1 do
    tex.print("FOO" .. nl)
    end
}
1
  • Any ways to be able to print "\n\n"? I tried that but I get an error? I know it works in lua itself but texlua seems different.
    – ArTourter
    Apr 24, 2012 at 1:05

You must log in to answer this question.

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