# Saving all the latex to a file and then render reading from the file

I am rendering Latex that spawns ~2600 pages. The document is rendered from a JSON using Lua. It takes around 32 seconds to prepare the document of ~2600 pages. To verify, if there is a difference in timing, when the Latex is rendered without Lua, I am trying to save all the latex in a file, and then trying to render the latex by directly reading the file.

But it appears that I am not saving the latex in the correct format. The reason being an error that says Undefined controlled sequence as I try to render the latex directly from the saved file.

I read the JSON file as :

\begin{luacode}
local json = require("json")
local file = io.open("sample.json")
file:close()
\end{luacode}


The function named renderSections in the following snippet saves all the latex accumulated in the variable named r in a file named render.tex.

\begin{luacode}
local function isempty(s)
return s == nil or s == ''
end

local f = assert(io.open("render.tex", "rb"))
f:close()
tex.print(content)
end

function renderSections(tab)
r = ""
for i,k in pairs(tab['tableOfContents']) do
r = " " .. r .. ("\string\\section{" .. k.section.title .. "}")
r = " " .. r .. ("section text here")
for ii,kk in pairs(k["subsection"]) do
r = " " .. r ..  ("\string\\subsection{" .. kk.title .. "}")
r = " " .. r ..  ("some text here")
r = " " .. r .. ("\string\\newline")
for iii, kkk in pairs(kk["content"]) do
if kkk['type'] == "text" then
r = " " .. r .. (kkk["line"])
r = " " .. r .. ("\string\\newline")
elseif kkk["type"] == "table" then
local column_count = 0
local row_count = 0
col_schema = ""
for _ in pairs(kkk["columns"]) do
column_count = column_count + 1
col_schema = col_schema .. " | l"
end
col_schema = col_schema .. "|"
for _ in pairs(kkk["columns"][1]["values"]) do row_count = row_count + 1 end
if not isempty(kkk.title) then
r = " " .. r .. ("\string\\textbf{" .. kkk.title .."}")
r = " " .. r .. ("\\\\[0.1in]")
end
r = " " .. r .. ("\string\\begin{tabular}{"  .. col_schema ..  "}")
r = " " .. r .. ("\\hline")
for ci, c in pairs(kkk["columns"]) do
r = " " .. r .. (c.name)
if ci < column_count then
r = " " .. r .. (" & ")
end
end
r = " " .. r .. ("\\\\")
r = " " .. r .. ("\\hline")
i = 1
while i <= row_count do
local c_c = 1
while c_c <= column_count do
r = " " .. r .. (kkk["columns"][c_c]["values"][i])
if c_c < column_count then
r = " " .. r .. (" & ")
end
c_c = c_c + 1
end
r = " " .. r .. ("\\\\")
r = " " .. r .. ("\\hline")
i = i + 1
end
r = " " .. r .. ("\string\\end{tabular}")
r = " " .. r .. ("\\\\[0.5in]")
end
end
end
end

file = io.open("render.tex", "w")
io.output(file)
io.write(r)
io.close(file)
end
\end{luacode}


Is this not the correct way to save latex in the file? The function named renderRead in the above code tries to render the latex saved in the file. This works absolutely fine, if I try to directly from renderSection using tex.print as tex.print("\string\\section{" .. k.section.title .. "}") in place of " " .. r .. ("\string\\section{" .. k.section.title .. "}").

The saved latex in the file looks similar to:

• I would start with adding a space after \newline and \hline. Like .. ("\string\\newline") .. " " – Sergei Golovan May 8 '19 at 6:11
• @SergeiGolovan You mean replacing r = " " .. r .. ("\string\\newline") with r = " " .. r .. ("\string\\newline") .. " " – Amanda May 8 '19 at 6:15
• If you add \newline (or any command) then you need to add a space after it (your image shows the command \newlineNext which is not defined, (but you should almost never have \newline in tex text, how come you are adding it? – David Carlisle May 8 '19 at 7:02
It looks like your \string command needs to be double escaped like your other commands are, or it will be converted to simply the word "string" without the preceding backslash.
Try adding a backslash, as in \\string to each of those commands in your function.