4

I'm using Latex since years but I'm new to embedded luacode (with Lualatex). Below you can see a simplified example:

\begin{filecontents*}{data.json}
    [
        {"firstName":"Max", "lastName":"Möller"},
        {"firstName":"Anna", "lastName":"Smith"}
    ];
\end{filecontents*}

\documentclass[11pt]{article}

\usepackage{fontspec}
%\setmainfont{Carlito}

\usepackage{tikz}
\usepackage{luacode}

\begin{document}
\begin{luacode}
    require("lualibs.lua")
    local file = io.open('data.json','rb')
    local jsonstring = file:read('*a')
    file.close()

    local jsondata =  utilities.json.tolua(jsonstring)

    tex.print('\\begin{tabular}{cc}')
    for key, value in pairs(jsondata) do
        tex.print(value["firstName"] .. ' & ' .. value["lastName"] .. '\\\\')
    end
    tex.print('\\hline\\end{tabular}')
\end{luacode}
\end{document}

When executing Lualatex following error occurs:

LuaTeX error [\directlua]:6: attempt to index field 'json' (a nil value) [\directlua]:6: in main chunk. \end{luacode}

When commenting the line \usepackage{fontspec} the output will be produced. Alternatively, the error can be avoided by commenting utilities.json.tolua(jsonstring) and all following lua-code lines.

So the question is: How can I use both "fontspec" package and json-data without generating an error message? Apart from this I have another question: How to enable german umlauts in output of luacode (see first "lastName" in example: Möller)?

Ah, I'm using TeX Live 2015/Debian on Ubuntu 16.04.

Thank you, Jerome

Jerome
  • 45
  • 6

0 Answers0