如何在文档早期引用 (La)TeX 计数器?

如何在文档早期引用 (La)TeX 计数器?

我正在用编程方式排版数据库内容中的长文档。出于效率原因,我想尽早输出所有生成的 LaTeX 代码。我还想为报告提供一个标题页,其中引用了报告的条目数让标题页作为生成的 PDF 的第一页出现。原则上,我现在所做的大致如下:

\documentclass[a4paper]{article}
\newcounter{entrycount}
\begin{document}

\stepcounter{entrycount} helo world\par
\stepcounter{entrycount} here are\par
\stepcounter{entrycount} some entries\par
\stepcounter{entrycount} from the database\par

\clearpage\centering

Title

This report lists \theentrycount\ entries.

\end{document}

这里我指的是柜台文档的其余部分已打印出来;显然,为了使标题页可以放在第一位,我必须 (1) 使用将标题页插入其他页面前面的命令(不确定是否可行)或 (2) 以某种方式引用计数器的最终值它是可用的(当然可能,至少在运行 LaTeX 几次时?)。

我知道还有其他解决方案,包括手动将打印堆栈中的标题移到顶部,并在输出任何 LaTeX 代码之前计算 DB 条目数;但是,这么简单的事情在 LaTeX 中一定可以做到,不是吗?我尝试了参考资料,但到目前为止还没有找到可行的解决方案。

更新

查阅了@Werner 提供的链接后,我尝试

This report lists \ref{ec} entries.

\section{helo world         }\label{ec} \par
\section{here are           }\label{ec} \par
\section{some entries       }\label{ec} \par
\section{from the database  }\label{ec} \par

这种方法是可行的(别介意输出不正确——原理显然是正确的)。但当我这样做时

\stepcounter{entrycount}\label{ec} helo world\par
\stepcounter{entrycount}\label{ec} here are\par
\stepcounter{entrycount}\label{ec} some entries\par
\stepcounter{entrycount}\label{ec} from the database\par

输出中什么都没有,只有空白。我猜我必须提升我的计数器以使其特殊运行,这样它才能被标签捕获。但是怎么做呢?

答案1

@egreg 让我走上正确的轨道。一个完全正常工作的最小示例:

\documentclass[a4paper]{article}
\usepackage{totcount}
\newcounter{entrycount}\regtotcounter{entrycount}

\begin{document}

{\centering

Title

This report lists \total{entrycount} entries.
}

\clearpage
\stepcounter{entrycount}helo world\par
\stepcounter{entrycount}here are\par
\stepcounter{entrycount}some entries\par
\stepcounter{entrycount}from the database\par

\end{document}

可以说,即使是简单的事情在 TeX 中也常常很难。totcount手动的说:“引用文档中章节、页面、引用、列表项或其他任何内容的总数可能很难实现。”

相关内容