In the following code, the function named json_parser is meant to parse the JSON. I had initially kept this function as a separate module named json.lua and required it using:

local json = require("json")

but for some reason, I wanted to pack this in one file and tried to create a function out of it. It worked fine but after making it a function, I am getting an error. I am using this code inside a latex file and running it using lualatex.

    local function json_parser()
        local lpeg = assert(require("lpeg"))
        local C, Cf, Cg, Ct, P, R, S, V =
            lpeg.C, lpeg.Cf, lpeg.Cg, lpeg.Ct, lpeg.P, lpeg.R, lpeg.S, lpeg.V

        -- number parsing
        local digit    = R"09"
        local dot      = P"."
        local eE       = S"eE"
        local sign     = S"+-"^-1
        local mantissa = digit^1 * dot * digit^0 + dot * digit^1 + digit^1
        local exponent = (eE * sign * digit^1)^-1
        local real     = sign * mantissa * exponent / tonumber

        -- optional whitespace
        local ws = S" \t\n\r"^0

        -- match a literal string surrounded by whitespace
        local lit = function(str)
            return ws * P(str) * ws

        -- match a literal string and synthesize an attribute
        local attr = function(str,attr)
            return ws * P(str) / function() return attr end * ws

        -- JSON grammar
        local json = P{

            value =
                V"null_value" +
                V"bool_value" +
                V"string_value" +
                V"real_value" +
                V"array" +

            null_value =
                attr("null", nil),

            bool_value =
                attr("true", true) + attr("false", false),

            string_value =
                ws * P'"' * C((P'\\"' + 1 - P'"')^0) * P'"' * ws,

            real_value =
                ws * real * ws,

            array =
                lit"[" * Ct((V"value" * lit","^-1)^0) * lit"]",

            member_pair =
                Cg(V"string_value" * lit":" * V"value") * lit","^-1,

            object =
                lit"{" * Cf(Ct"" * V"member_pair"^0, rawset) * lit"}"

        return { parse = function(str) return assert(json:match(str)) end }

    local socket = require("socket")

    local json = json_parser()
    local file = io.open("sample-true.json")
    local start = socket.gettime()
    tab = json.parse(file:read("*all"))
    texio.write_nl("Time passed parsing JSON: " .. socket.gettime() - start .. " s\string\n")

The part in the yellow is the error, that I get. What could be the reason for it? enter image description here

  • 1
    Always try to make complete examples so that other people don't have to build the test document themselves (and guess what you have in the preamble). May 9 '19 at 8:07
  • 1
    If you are using non-trivial amounts of Lua, it is much better to put it into a separate file and require() that from \directlua. The differences in handling of characters by Lua and TeX make intermixing beyond short fragments painful.
    – Joseph Wright
    May 9 '19 at 10:11

The luacode-environment still executes tex-commands and so the \t explodes. Use the luacode* environment:


% gives error:
 -- \t

% works:
 -- \t 


  • It gives an error saying invalid escape sequence near '\s' Is there any other change I could need to make?
    – Amanda
    May 9 '19 at 8:38
  • 1
    \string should be imho \\string. Or perhaps remove that line ... May 9 '19 at 8:42
  • Okay! Could you tell the difference between \begin{luacode} and \begin{luacode*}? Will be helpful, if you could update in the original answer.
    – Amanda
    May 9 '19 at 8:44

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.