我尝试使用 LaTeX 排版的文档可能包含多页环境。此类环境有自己的页码,从 1 开始。但环境的第一页必须是总环境页数。我知道在文档中只有一个环境时该怎么做。但当我在具有不同页数的文档中使用多个环境时会出现问题。我知道为什么会发生这种情况(这些环境使用一个计数器)。如果事先不知道文档中的环境数量,我该如何更改这种情况?
\documentclass{article}
\usepackage{blindtext}
\usepackage{totcount}
\usepackage{tikz}
\usepackage{eso-pic}
\usepackage{calc}
\newcounter{ccnt}%current page counter
\newcounter{fcnt}%first page counter
\newtotcounter{tcnt}%total page counter
\newenvironment{elementlist}%
{\clearpage
\pagestyle{empty}
\Environmenttrue
\FirstBackgroundtrue
\AddToShipoutPicture{\Background}
\linespread{2}\selectfont
\enlargethispage{-3\baselineskip}
\sffamily\slshape\selectfont
\setcounter{fcnt}{\thepage}}
{\setcounter{tcnt}{\value{page}-\value{fcnt}+1}
\clearpage
\ClearShipoutPicture
\pagestyle{plain}}
\newif\ifEnvironment
\newif\ifFirstBackground
\newcommand\Background
{\ifFirstBackground
\formfirst
\global\FirstBackgroundfalse
\else
\formposterior
\fi
}
\newcommand\formfirst{
\begin{tikzpicture}[overlay]
\setcounter{ccnt}{\value{page}-\value{fcnt}+1}
\ifEnvironment
\draw (2.0, 1.5) node[align=center,text width=4.5cm] {\total{tcnt}};
\draw (19.5, 1.5) node[align=center,text width=4.5cm] {\arabic{ccnt}};
\fi
\end{tikzpicture}}
\newcommand\formposterior{
\begin{tikzpicture}[overlay]
\setcounter{ccnt}{\value{page}-\value{fcnt}+1}
\ifEnvironment
\draw (19.5, 1.5) node[align=center,text width=4.5cm] {\arabic{ccnt}};
\fi
\end{tikzpicture}}
\begin{document}
\Blindtext
\begin{elementlist}
\Blindtext
\end{elementlist}
\Blindtext
\begin{elementlist}
\Blindtext
\Blindtext
\end{elementlist}
\end{document}
答案1
您可以使用xcntperchap
并跟踪每个环境的页面数量。
稍后,调用\ObtainTrackedValueExp{elistcntr}{page}
,首先提取页数。
\documentclass{article}
\usepackage{blindtext}
\usepackage{totcount}
\usepackage{tikz}
\usepackage{eso-pic}
\usepackage{calc}
\newcounter{elistcntr}
\newcounter{ccnt}%current page counter
\newcounter{fcnt}%first page counter
\newtotcounter{tcnt}%total page counter
\usepackage{xcntperchap}
\RegisterTrackCounter{elistcntr}{page}
\newenvironment{elementlist}{%
\clearpage
\pagestyle{empty}
\Environmenttrue
\FirstBackgroundtrue
\refstepcounter{elistcntr}%
\clearpage
\AddToShipoutPicture{\Background}
\linespread{2}\selectfont
\enlargethispage{-3\baselineskip}
\sffamily\slshape\selectfont
\setcounter{fcnt}{\thepage}
}{%
\setcounter{tcnt}{\value{page}-\value{fcnt}+1}
\clearpage%
\refstepcounter{elistcntr}%
\ClearShipoutPicture
\pagestyle{plain}%
\clearpage
}
\newif\ifEnvironment
\newif\ifFirstBackground
\newcommand\Background
{\ifFirstBackground
\formfirst
\global\FirstBackgroundfalse
\else
\formposterior
\fi
}
\newcommand\formfirst{
\begin{tikzpicture}[overlay]
\setcounter{ccnt}{\value{page}-\value{fcnt}+1}
\ifEnvironment
\draw (2.0, 1.5) node[align=center,text width=4.5cm] {\ObtainTrackedValueExp{elistcntr}{page}};
\draw (19.5, 1.5) node[align=center,text width=4.5cm] {\arabic{ccnt}};
\fi
\end{tikzpicture}}
\newcommand\formposterior{
\begin{tikzpicture}[overlay]
\setcounter{ccnt}{\value{page}-\value{fcnt}+1}
\ifEnvironment
\draw (19.5, 1.5) node[align=center,text width=4.5cm] {\arabic{ccnt}};
\fi
\end{tikzpicture}}
\begin{document}
\Blindtext
\begin{elementlist}
\Blindtext
\end{elementlist}
\Blindtext
\begin{elementlist}
\Blindtext
\Blindtext
\Blindtext
\Blindtext
\end{elementlist}
\Blindtext
\begin{elementlist}
\Blindtext
\Blindtext
\Blindtext
\Blindtext
\end{elementlist}
\Blindtext
\begin{elementlist}
\Blindtext
\Blindtext
\Blindtext
\Blindtext
\Blindtext
\Blindtext
\Blindtext
\Blindtext
\end{elementlist}
\end{document}