[Tex/LaTex] another luacode new line question, how to generate new line without making everything string

lualuatex

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.

Mathematica graphics

Even though documentation clearly says it will insert new line

Mathematica graphics

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

Mathematica graphics

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

Mathematica graphics

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?

reference:
http://wiki.luatex.org/index.php/Writing_Lua_in_TeX

Best Answer

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}
Related Question