如果我在 .tex 文件上运行 lualatex,我只会??
从文档中的“lastpage”获得结果。然后,如果我第二次运行 lualatex,正确的结果 ( 2
) 将显示在文档页脚中。
主要问题:如何避免在同一个文档(.tex 文件)上运行两次 Latex?
如果有解决方案就好了,因为我正在从 Java 动态生成乳胶文件,并且每次控制台lualatex doc.tex
调用大约需要 2.5 秒。
使用来源:控制台上的 lualatex 和 test.tex -> mwe
控制台命令: lualatex test.tex
故障输出:
LaTeX Warning: Reference `LastPage' on page 1 undefined on input line 23 Package lastpage Warning: Rerun to get the references right on input line 23
梅威瑟:
\documentclass[8pt,a4paper]{extarticle}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{lastpage}
\usepackage[left=1.5cm, right=1.5cm, top=0.0cm, bottom=0.0cm, headheight=42pt, includeheadfoot]{geometry}
\usepackage{fancyhdr}
\usepackage{lipsum}
\fancypagestyle{style1}{
\fancyhf{}
\fancyhead[L]{}
\fancyhead[R]{}
\fancyfoot[L]{\scriptsize Test}
\fancyfoot[R]{\scriptsize Seite \textbf{\thepage} von \textbf{\pageref{LastPage}}}
\setlength{\topmargin}{-70pt}
\setlength{\headsep}{10pt}
\setlength{\footskip}{20pt}
}
\begin{document}
\pagestyle{style1}
\lipsum[1-15]
\end{document}
答案1
通常,TeX 无法在第一页上使用诸如页数之类的数据,因为 LaTeX 还不知道会有多少页。但有一个技巧:您可以在每个应该有页数的页面上插入对 PDF XObject 的引用。XObject 是 PDF 文件中的单独对象,可以无序引用。然后,您可以稍后用页数填充此 XObject。
另一个复杂因素是,LuaTeX 强制您在使用 XObject 之前确定它的内容,但您可以通过在较低级别上操作 PDF 结构来欺骗它:
\documentclass[8pt,a4paper]{extarticle}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage[left=1.5cm, right=1.5cm, top=0.0cm, bottom=0.0cm, headheight=42pt, includeheadfoot]{geometry}
\usepackage{fancyhdr}
\usepackage{lipsum}
\directlua{
local n = pdf.reserveobj()% `n` will be a PDF object storing
% a reference to the XObject created later.
local list = token.scan_list() % First we read a list to get the dimensions.
node.flush_list(list.head) % The actual content isn't important
local whatsit = node.new('whatsit', 'pdf_literal') % Now "call" the XObject /Form "manually"
whatsit.mode = 1
whatsit.data = "/Form Do"
list.head = whatsit
% Create a XObject managed by LuaTeX which only references our manual reference
pages_xform = tex.saveboxresource(list, nil, '/XObject ' .. n .. ' 0 R')
final_form_hack = n % And save the identifiers
}\hbox{\scriptsize\textbf{29}}% This \hbox decides how much space to reserve for the page number. 29 makes sure that all numbers up to 99 should be fine
\fancypagestyle{style1}{
\fancyhf{}
\fancyhead[L]{}
\fancyhead[R]{}
\fancyfoot[L]{\scriptsize Test}
% Reference the XObject from TeX
\fancyfoot[R]{\scriptsize Seite \textbf{\thepage} von \directlua{node.write((tex.useboxresource(pages_xform)))}}
\setlength{\topmargin}{-70pt}
\setlength{\headsep}{10pt}
\setlength{\footskip}{20pt}
}
\AtEndDocument{
\clearpage
\directlua{
% Now we know how many pages there are, so we can fill the XObject.
local n = final_form_hack
final_form_hack = nil
local list = token.scan_list() % Scan the list with the number of pages
local w, h, d, m = tex.getboxresourcedimensions(pages_xform)
list.height, list.depth, list.width = h, d, w % Make sure our boxes have consistant sizes (otherwise LuaTeX will be confused)
local xform = tex.saveboxresource(list, nil, nil, true, m)% And save it in a XObject
pdf.immediateobj(n, "<</Form " .. xform .. " 0 R>>")% Finally store a reference in the object `n` created above
}\hbox{%
\scriptsize\textbf{\the\numexpr\value{page}-1\relax}% The actual number of pages
}
}
\begin{document}
\pagestyle{style1}
\lipsum[1-30]
\end{document}
当然,这只影响页数。如果你的文件中还有其他参考资料(例如\label/\ref
或只是一个目录),类似的问题还会再次出现。