我正在循环遍历 json 文件并以以下格式显示内容:
Zone 1
Projection(Main.tex)
Grat Table(gratsection.tex)
Zone 2
Projection (Main.tex)
Grat table (gratsection.tex)
主文本
\documentclass{article}
%%%%%%%%%%%%% Font Packages %%%%%%%%%%%%%
\usepackage[document]{ragged2e}
\usepackage[nohead,paperheight=11.0in,paperwidth=8.5in,left=0.5in,right=0.5in,top=0.5in,bottom=1.0in]{geometry}
%%%% Font %%%%
\usepackage{fontspec}
%%%%% dynamic table package %%%%%
\usepackage{datatool}
\DTLsetseparator{,}% Set the separator between the columns.
\usepackage{longtable}
\usepackage{tabularx}
\usepackage{amsmath}
\usepackage{hhline}
\usepackage{multicol}
\usepackage{multirow}
\usepackage{booktabs}
\usepackage{makecell}
\usepackage{color, colortbl}
\usepackage[table]{xcolor}
\usepackage{xparse}
\usepackage{etoolbox}
\usepackage{array}
\usepackage{luacode}
\usepackage{xcolor}
\usepackage{graphicx}
%%%%%%% Section %%%%%%
\usepackage[compact]{titlesec}
\titleformat{\section}
{\normalfont\Large\bfseries\filcenter}{\thesection}{1em}{}
\renewcommand{\familydefault}{\sfdefault}
\renewcommand{\arraystretch}{1.5}
\setlength{\arrayrulewidth}{1pt}
\titlespacing*{\section}{0pt}{*0}{*0}
\font\myfont=cmr12 at 16pt
\begin{document}
\newcolumntype{L}{|>{\raggedright\arraybackslash\bfseries}X|X}
\catcode10=9\
\begin{luacode*}
local json = require("json")
local file = io.open("data.json")
tab = json.parse(file:read("*all"))
file:close()
for ii,jj in ipairs(tab["Zones"]) do
%% PROJECTION
tex.sprint(
[[\begin{table}[ht!]
\centering
\begin{tabularx}{\textwidth}{@{} LL @{}}
\hline]])
for w, v in ipairs(jj.Projection.Header) do
tex.sprint(
[[\textbf]],
v.key, [[& ]],
v.val, [[\\]],
[[\hhline{--}]])
end
tex.sprint(
[[
\end{tabularx}
\end{table}]])
%%GRAT SECTION
tex.sprint([[\input{gratsection}]])
end
\end{luacode*}
\end{document}
如果在 'for' 循环之外调用 '\input{gratsection.tex}',我的 MWE 会显示两次投影信息(Zone 数组中的 2 个对象)和一次 Grat 表:
但我无法显示每个投影区域下的部分文件中的 Grat 表。在“FOR”循环中,Grat 表(来自 gratsection.tex)不会显示。代码陷入无限循环,无法编译,但看不到任何错误。有什么方法可以实现这一点吗?
gratsection.tex
\section*{ SECTION 2(GRAT)}
\newcolumntype{L}{|>{\raggedright\arraybackslash\bfseries}X|X}
\catcode10=9\
\definecolor{lightgray}{gray}{0.9}
\let\oldtabularx\tabularx
\renewcommand*{\tabularx}{\rowcolors{1}{}{lightgray}\oldtabularx}
\begin{luacode*}
local json = require("json")
local file = io.open("graticule_table.json")
tab = json.parse(file:read("*all"))
file:close()
tex.sprint(
[[\begin{table}[ht!]
\centering
\begin{tabularx}{\textwidth}{@{} LL @{}}
\hline]])
for k, v in ipairs(tab["GratTable"]) do
tex.sprint(
[[\textbf]],
v.LATDEG, [[& ]],
v.LATMIN, [[\\]],
[[\hhline{--}]])
end
tex.sprint(
[[
\end{tabularx}
\end{table}]])
\end{luacode*}
数据.json
{
"Zones":[
{
"name":"Zone 1",
"Projection": {
"Zone": "29",
"Header": [
{
"key": "MAP NAME",
"val": "XX"
},
{
"key": "PROJECTION",
"val": "YY"
}
]
},
"Grat": {
"Header": [
{
"key": "SHEET NAME",
"val": "AA"
},
{
"key": "PROJECTION",
"val": "BB"
}
]
}
},
{
"name":"Zone 2",
"Projection": {
"Zone": "30",
"Header": [
{
"key": "MAP NAME",
"val": "AA"
},
{
"key": "PROJECTION",
"val": "BB"
}
]
},
"Grat": {
"Header": [
{
"key": "SHEET NAME",
"val": "XX"
},
{
"key": "PROJECTION",
"val": "YY"
}
]
}
}
]
}
graticule_table.json(这是一个很大的文件,需要将其单独保存)
{
"GratTable": [
{
"LATDEG": "1",
"LATMIN": "30"
},
{
"LATDEG": "2",
"LATMIN": "45"
}
]
}
解析 JSON 的 json.lua 文件来自http://regex.info/blog/lua/json