1

I have the below document that I try to compile with LuaLaTex (using the MiKTeX package).

\documentclass{article}

\usepackage{pgfplots}

\pgfplotsset{compat=newest}

\begin{document}
\expandafter\pgfplotstableread\directlua {
    global json = utilities.json.load("test.json")
    tex.sprint("{\string\r")
    tex.sprint("x y\string\r")
    for n, row in ipairs(json.data) do
        tex.sprint(row[1] .. " " .. row[2] .. "\string\r")
    end
    tex.sprint("}")
}\loadedtable

\begin{tikzpicture}
  \begin{axis}
    \addplot table {\loadedtable};
  \end{axis}
\end{tikzpicture}
\end{document}

but I get a compilation error due to a Syntax error in the Lua part:

[\directlua]:1: syntax error near 'json'.
l.17 }
  \loadedtable

I'm a noob at LaTeX and unfortunately I have no clue of Lua either, but from what I have researched I can't figure out where there is a syntax error.

The respective test.json is located in the same folder like the .tex script and has this form:

{
    "name":"list_of_xy_values",
    "data":[[1,2],[2,4],[3,6],[4,3],[5,0]]
}

This question refers to this other question

2
  • Variables are global by default in Lua. No need to add global if you want to reuse json in further Lua calls. Just remove global Commented Dec 28, 2020 at 16:36
  • That was the problem, thank you! If you add a short answer, I'll accept it. Commented Dec 28, 2020 at 16:43

1 Answer 1

1

Variables in Lua are global by default, so only local has to be explicitly stated. See a brief explanation on Lua Unofficial FAQ and Lua 5.3 Reference Manual for additional info. In LuaTeX's \directlua and luacode environments is usual to define variables as local, what you can see in Henri's answer to your other question, to avoid clashes and unexpected redefinitions. :)

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.