2

I would like to generate LaTeX template based on JSON Input. It is possible to take JSON as input in LaTeX3 and create LaTeX template? I had the solutions are found in LuaLaTeX.

Ways to parse JSON in LaTeX?

How do achieve this in LaTeX3? My JSON Input is below:

{
    "LaTeXPage": {
        "obj": "LaTeXPage",
        "pro": {
            "paperWidth": {
                "property": "paperWidth",
                "value": "129",
                "units": "mm",
                "remarks": null
            },
            "paperHeight": {
                "property": "paperHeight",
                "value": "198",
                "units": "mm",
            },
            "topMargin": {
                "property": "topMargin",
                "value": "3",
                "units": "mm",
            },
        }
    }
    }

Expected LaTeX Package output:

\setlength{\paperwidth}{129mm}
\setlength{\paperheight}{198mm}
\setlength{\topmargin}{3mm}

Especially i would like to use expl3 LaTeX Package. Or anyother solutions also welcome!!!

3
  • Why don't you use LuaLaTeX and a real JSON parser library?
    – TeXnician
    Apr 18 '20 at 22:01
  • @TeXnician: I have knowledge in LaTeX only and i'm new in LuaLaTeX. So i would like to do in LaTeX. Any solution greatly appreciate.
    – Balaji
    Apr 19 '20 at 3:32
  • @TeXnician: I had tried many ways and not found the solution. Could you please provide LuaLaTeX solution?
    – Balaji
    Apr 23 '20 at 16:58
1

I have found the solution.

\documentclass{article}
\usepackage{luatextra} % luacode
\usepackage{environ}  % LaTeX Stylefile Writing in NewEnvironment

% load json file
\begin{luacode*}
function read(File)
    local handler = io.open(File, "rb")
    local content = handler:read("*all")
    handler:close()
    return content
end

JSON = (loadfile "JSON.lua")()
tab = JSON:decode(read("sample.json"))
\end{luacode*}

\newcommand*\JSONpagewidth{\directlua{tex.print(tab['LaTeXPage']['pro']['pageWidth']['value'])}\directlua{tex.print(tab['LaTeXPage']['pro']['pageWidth']['units'])}}

\newwrite\writer
\NewEnviron{styleFileWrite}
    {\immediate\write\writer{%
    \string\setlength\paperwidth{\JSONpagewidth}^^J%
    \unexpanded\expandafter{\BODY}
    }}
\immediate\openout\writer=LaTeX-\jobname.sty    
\begin{styleFileWrite}
\end{styleFileWrite}
\immediate\closeout\writer

\begin{document}

Pagewidth values is ::  {\JSONpagewidth}

\end{document}

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.