1

A document which I try to typeset with LaTeX may contain multi page environments. Such environment has his own page numeration which starting from 1. But it has to be total environment pages number at the first page of environment. I know how to do it while there is a single environment is in document. But there is issue when I use more than one environment in a document with different pages amount. I know why this happens (those environments use one counter). How can I change this in case that the environments amount in the document isn't known in advance?

\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 Answer 1

1

You can use xcntperchap and track the number of pages for each environment.

Later on, call \ObtainTrackedValueExp{elistcntr}{page}, which extracts the number of pages first.

\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}
12
  • Thanks for the answer but in your example in second environment I counted 9 pages, while LaTeX with xcntperchap typed 10.
    – Konstantin
    Jan 5, 2018 at 13:16
  • @Konstantin: Yes. As I said, page is very unreliable... I'll try to improve the code
    – user31729
    Jan 5, 2018 at 13:25
  • @Konstantin: see the update, please
    – user31729
    Jan 5, 2018 at 14:08
  • Well, now environment pages amount typed right, but page before environment without number.
    – Konstantin
    Jan 5, 2018 at 14:31
  • 1
    @Konstantin: Basically, it is the correct order of pagestyle setting and clearpage.
    – user31729
    Jan 5, 2018 at 16:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .