# another luacode new line question, how to generate new line without making everything string

I wanted to print something from luacode, and have it show up on its own line in Latex, not on the same line. The question here, which is similar, but uses strings, but I am not printing strings from lua. A simple example will explain:

\documentclass[11pt]{scrartcl}
\IfFileExists{luatex85.sty}
{
\usepackage{luatex85}
}{}
\usepackage{luacode}
\begin{luacode*}
function foo(list)
tex.print(type(list))

local i = 0
for _ in pairs(list) do
i = i + 1
tex.print(list[i])
end
end
\end{luacode*}
\begin{document}
\directlua{foo({1,2,3,4})}
\end{document}


The above produces all the output on same line.

Even though documentation clearly says it will insert new line

Ok, but it says strings in the above, and what I am sending from Lua to Latex is not string. So is one really supposed to convert everything to strings before passing stuff back to Latex from lua? I found I can get new line if I do this:

\documentclass[11pt]{scrartcl}
\IfFileExists{luatex85.sty}
{
\usepackage{luatex85}
}{}
\usepackage{luacode}
\begin{luacode*}
function foo(list)
tex.print({type(list),"\\\\"})

local i = 0
for _ in pairs(list) do
i = i + 1
tex.print({list[i],"\\\\"})
end
end
\end{luacode*}
\begin{document}
\directlua{foo({1,2,3,4})}
\end{document}


Which gives what I wanted

but I really do not want to write the above, I simply wanted to write tex.print(list[i]) and have it show on its own line in Latex. There is function called texio.write_nl but this is for logging and not what I wanted.

TL 2016

Update:

WHen I run this:

\documentclass[11pt]{scrartcl}
\IfFileExists{luatex85.sty}
{
\usepackage{luatex85}
}
{}
\usepackage{luacode}
\begin{luacode*}
function foo(list)
tex.print(type(list))
tex.print("\\newline")
tex.print(type(list[1]))
tex.print("\\newline")

local i = 0
for _ in pairs(list) do
i = i + 1
tex.print(list[i])
end
end
\end{luacode*}

\begin{document}
\directlua{foo({1,2,3,4})}
\end{document}


The output is

In in Lua land, the type is number and table. But if the input to tex.print is supposed to be string, how is it converted to string?

• the number to string conversion is surely as you would expect, the number 10 becomes the string "10" ?? – David Carlisle Jun 18 '16 at 9:14
• the lua type function always returns a string, that is its function: to return the type name as a string. see where tex.print is described (9.3.10.1) it says tex.print(<string> s, ...) or tex.print(<table> t) so the argument needs to be a list of strings or a table which is interpreted as a list of strings as described in the following paragraph. In general every function in the manual is shown in that form, showing the type of all its arguments, and of any return values. – David Carlisle Jun 18 '16 at 9:37

Your description "converting to string" does not really match what is happening.

When the text you circled says that a newline character is appended it means that your list of print statements is equivalent to

1
2
3
4


rather than

1 2 3 4


in the TeX input character stream, but in most contexts these two inputs make the same typeset output.

The argument to tex.print is always a Lua string (I'm not sure what you mean when you say it is not a string?), so if you want to print 1\\2\\3 then you need that as a string which is "\\\\2\\\\3\\\\4" because of Lua quoting rules, then if you are putting this inside \directlua rather than in a lua file to be included you need to stop Tex expanding \\ by using \string or similar.

I would use a paragraph break so print a blank line:

\documentclass[11pt]{article}

\directlua{
function foo(list)
tex.print(type(list))
tex.print("")
local i = 0
for _ in pairs(list) do
i = i + 1
tex.print(list[i])
tex.print("")
end
end
}

\begin{document}

\directlua{foo({1,2,3,4})}
\end{document}

• @Nasser see my update the argument to tex.print is a string, if you give it something else, general lua type conversion rules mean that types toString method gets called on it. so yes it is converted to a string by lua (which is a good thing as tex has no numeric literals) – David Carlisle Jun 18 '16 at 9:10