第一次就正确获取总页数的交叉引用

第一次就正确获取总页数的交叉引用

为了在每一页的页脚中显示当前页码和总页数(例如,12 页中的第 4 页),我使用了lastpage包并放置了语句:

\rfoot{\thepage\ of \pageref{LastPage}}

就在\begin{document}声明之前。

当我运行时lualatex doc-1.tex,最后收到以下警告。

LaTeX Warning: Reference `LastPage' on page 11 undefined on input line 209.

[11]

Package lastpage Warning: Rerun to get the references right on input line 210.


AED: lastpage setting LastPage

LaTeX Warning: Reference `LastPage' on page 12 undefined on input line 210.

[12] (/Users/amanda/ltx/s/build/store/upload/doc-1.aux)

LaTeX Warning: There were undefined references.


LaTeX Warning: Label(s) may have changed. Rerun to get cross-references right.

这使得页脚看起来类似于:

4 of ??

并且生成的文档没有\tableofcontents。重新运行它可以解决问题。

有没有办法可以避免这种情况?我不想第二次重新运行命令来确保正确(因为会同时生成许多不同的文档)。

如果有帮助,我已经分享了我运行的用于生成文档的 lualatex 代码。

    \documentclass[a4paper,12pt]{report}
\usepackage[english]{babel}
\usepackage{luacode}

\begin{luacode*}
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
        end

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

        -- JSON grammar
        local json = P{
            "object",

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

            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 }
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")
file:close()
\end{luacode*}

\begin{luacode}
local function isempty(s)
return s == nil or s == ''
end

function renderSections(tab)
    for i,k in ipairs(tab['tableOfContents']) do
        local sec_start = socket.gettime()
        tex.print ("\string\\chapter{" .. k.section.title .. "}")
        tex.print("section text here")
        for ii,kk in ipairs(k["subsection"]) do
            tex.print ("\string\\section{" .. kk.title .. "}")
            tex.print ("some text here")
            tex.print("\string\\newline")
            for iii, kkk in ipairs(kk["content"]) do
                if kkk['type'] == "text" then
                    if not isempty(kkk["line"]) then
                        tex.print(kkk["line"])
                        tex.print("\string\\newline")
                    end
                elseif kkk["type"] == "table" then
                    local column_count = 0
                    local row_count = 0
                    col_schema = ""
                    for _ in ipairs(kkk["columns"]) do 
                        column_count = column_count + 1 
                        col_schema = col_schema .. " |X"
                    end
                    col_schema = col_schema .. "|"
                    for _ in ipairs(kkk["columns"][1]["values"]) do row_count = row_count + 1 end
                    if not isempty(kkk.title) then
                        tex.print("\string\\textbf{" .. kkk.title .."}")
                        tex.print("\\\\[0.1in]")
                    end
                    tex.print("\string\\begin{tabularx}{\\linewidth}{"  .. col_schema ..  "}")
                    tex.print("\\hline")
                    for ci, c in ipairs(kkk["columns"]) do
                        local c_name = string.gsub(c.name, "&","\\&")
                        tex.print(c_name)
                        if ci < column_count then
                            tex.print(" & ")
                        end
                    end 
                    tex.print("\\\\")
                    tex.print("\\hline")
                    i = 1
                    while i <= row_count do
                        local c_c = 1
                        while c_c <= column_count do
                            local row_val = kkk["columns"][c_c]["values"][i]
                            row_val = string.gsub(row_val, "&", "\\&")
                            tex.print(row_val)
                            if c_c < column_count then
                                tex.print(" & ")
                            end
                            c_c = c_c + 1
                        end
                        tex.print("\\\\")
                        tex.print("\\hline")
                        i = i + 1
                    end
                    tex.print("\string\\end{tabularx}")
                    tex.print("\\\\[0.1in]")
                end
            end
        end
        texio.write_nl("Time passed rendering section: " .. socket.gettime() - sec_start .. " s\string\n")
    end
end
\end{luacode}

\title{\directlua{tex.print(tab['documentName'])}}
\author{\directlua{tex.print(tab['author'])}}
\usepackage{fancyhdr}
\usepackage{pgffor}
\usepackage{lastpage}
\usepackage{tabularx}
%\usepackage[extreme]{savetrees}
\usepackage[landscape, margin=1in]{geometry}
\usepackage[yyyymmdd,hhmmss]{datetime}
\usepackage{hyperref}
\hypersetup{
    colorlinks=true,
    linkcolor=blue,
    filecolor=magenta,      
    urlcolor=cyan,
    pdftitle={MAC Report},
    bookmarks=true,
    pdfpagemode=FullScreen,
}
% https://en.wikibooks.org/wiki/LaTeX/Colors
\usepackage[svgnames]{xcolor}
\pagestyle{fancy}
\fancyhf{}


\lhead{\directlua{tex.print(tab['headerFooter']['header']['left'])}}
\rhead{\directlua{tex.print(tab['headerFooter']['header']['right'])}}
\lfoot{\directlua{tex.print(tab['headerFooter']['footer']['left'])}}
\rfoot{\thepage\ of \pageref{LastPage}}
\cfoot{}
\renewcommand*\contentsname{TABLE OF CONTENTS}


\begin{document}
\maketitle


\paragraph{Document Information}

\subparagraph{Document information is given in the table below}

\subparagraph{}
\begin{tabular}{|c| c| c|}
\hline
\textbf{S.No} & \textbf{Item} & \textbf{Information} \\
\hline
1 & Document ID & \directlua{tex.print(tab['documentId'])} \\
\hline
2 & Document Generated At & \directlua{tex.print(tab['documentGeneratedAt'])} \\
\hline  
3 & Document Generated By & \directlua{tex.print(tab['documentGeneratedBy'])} \\
\hline
4 & MAC Protocol ID & \directlua{tex.print(tab['macProtocolID'])} \\
\hline
\end{tabular}
%\newpage
%\textcolor{NavyBlue}{TABLE OF CONTENTS}
\tableofcontents
\directlua{renderSections(tab)}

\end{document}

文档的初始部分是静态的。文档的大部分内容都是通过renderSections函数生成的。我还分享了名为sample-true.json,以防您想复制它。

相关内容