# How do get linebreaks from Lua source files

I'm creating documents in LuaTeX from Lua sources containing long multiline strings ([=[ string ]=]). When I print the strings using tex.print, LuaTeX "eats up" line breaks. For instance, I have the string

A = [=[
Here we go.
Text. More text.
And more text to go.
]=]


but tex.print outputs

Here we go. Text. More text. And more text to go.


This doesn't happen in Lua, but it does in LuaTeX. Is there a way to fix that? Consider a large amount of multiline strings, so editing strings manually is not a possibility for me.

• tex.print generates a sequence of tokens, no string with \n. TeX manipulates with sequences of tokens only, no strings. So use something different than TeX if you need to manipulate with strings. – wipet Apr 10 at 20:39
• the tex.print outputs the lines but the effect is the same as newlines in a normal tex source file. they render as spaces by default try \obeylines – David Carlisle Apr 10 at 22:11
• \obeylines does not work as I expected, but it makes some work. Thanks. – user193767 Apr 10 at 22:41

In ConTeXt, you can use (in a standalone CLD document):

local A = [=[
Here we go.
Text. More text.
And more text to go.
]=]

context.starttext()
context.startlines()
context(A)
context.stoplines()
context.stoptext()


or in a regular TeX document:

\startluacode
local A = [=[
Here we go.
Text. More text.
And more text to go.
]=]
\stopluacode

\starttext
\startluacode
context.startlines()
context(A)
context.stoplines()
\stopluacode
\stoptext


Not sure what is the equivalent to \startlines ... \stoplines in LaTeX, but that should work in LaTeX as well.

You have to split the string into lines at the \n and then use tex.print (not tex.sprint) to print them, because this will insert the end-of-line marker that is needed by \obeylines.

\documentclass{article}
\usepackage{luacode}
\begin{luacode*}
A = [=[
Here we go.
Text. More text.
And more text to go.
]=]
\end{luacode*}

\begin{document}

{\obeylines\directlua{
for _, line in ipairs(string.splitlines(A)) do
tex.print(line)
end
}}

\end{document}