0

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")
    tab = json.parse(file:read("*all"))
    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

    function renderRead()
        local f = assert(io.open("render.tex", "rb"))
        local content = f:read("*all")
        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:

enter image description here

Please let me know if I must add more information.

  • I would start with adding a space after \newline and \hline. Like .. ("\string\\newline") .. " " – Sergei Golovan May 8 at 6:11
  • @SergeiGolovan You mean replacing r = " " .. r .. ("\string\\newline") with r = " " .. r .. ("\string\\newline") .. " " – Amanda May 8 at 6:15
  • you could provide a small lua example and show the generated tex code then people can reproduce the error. – David Carlisle May 8 at 7:00
  • 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 at 7:02
0

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.

  • sorry i deletd my earlier comment, the OP changed the usage – David Carlisle May 8 at 6:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.