The trick is to write explicitly a label to the .aux
file with the page or some other counter value as the page
, such that \pageref{labelname}
will yield that value (and therefore the counter value)
This requires running latex/pdflatex twice or even three times.
I added support for the part
counting, taking care of total part numbers, if the part number is reset somewhere in between, also for the page number, by introducing Total....
counters, which are not added to the reset list of TeX
.
For external references, it is necessary to use the xr
package and explicitly
say to use, which documents are to be used, in the preamble.
readinpartnumber.tex Driver with references to other files
\documentclass[twoside,landscape]{scrreprt}
\usepackage{etoolbox}
\usepackage{everypage}
\usepackage{forloop}
\usepackage{pdfpages}
\usepackage{background}
\usetikzlibrary{calc}
\usepackage[a4paper]{geometry}
\geometry{top=6mm, left=6mm, right=0mm, bottom=0mm}
\usepackage{xr}
\backgroundsetup{
scale=1,
opacity=1,
angle=0,
color=black,
contents={%
\begin{tikzpicture}[remember picture,overlay]
\draw[thick] ($(current page.north west)+(0mm,-9mm)$) -- ++(\paperwidth,0mm); %o
\draw[thick] ($(current page.south west)+(0mm,9mm)$) -- ++(\paperwidth,0mm); %u
\draw[thick] ($(current page.north east)+(-9mm,0mm)$) -- ++(0,-\paperheight); %r
\draw[thick] ($(current page.north west)+(9mm,0mm)$) -- ++(0,-\paperheight); %l
\draw[thick] ($(current page.north)+(0mm,0mm)$) -- ++(0,-\paperheight); %m
\end{tikzpicture}%
}}
%\newcommand{\ing}[1]{\includegraphics[width=(\textwidth-6mm)/2,height=\textheight-6mm,page=#1]{empty}}
\newcommand{\pdf}[4]{
\hspace*{-11pt}%
\ing{\numexpr#1\relax}%
\ing{\numexpr#2\relax}%
\clearpage%
\hspace*{6pt}%
\ing{\numexpr#3\relax}%
\ing{\numexpr#4\relax}%
\clearpage%
}
\pagestyle{plain}
\newcounter{i}
\newcommand\maxpage{???}
\newcommand\emptypage{3}
\makeatletter
\newrobustcmd{\LastPageInFile}{%
\immediate\write\@auxout{\string
\newlabel{LastPageInDocument}{{}{\arabic{DocumentTotalPageCounter}}}}%
}% End of LastProblem
\newrobustcmd{\LastPartInFile}{%
\immediate\write\@auxout{\string
\newlabel{LastPartInDocument}{{}{\arabic{DocumentTotalPartCounter}}}}%
}% End of LastProblem
\makeatother
\AtEndDocument{%
\LastPartInFile%
\LastPageInFile%
}
\newcounter{DocumentTotalPartCounter}% No resetting
\preto{\part}{\stepcounter{DocumentTotalPartCounter}}%
\newcounter{DocumentTotalPageCounter}
\AddEverypageHook{\stepcounter{DocumentTotalPageCounter}}
\newrobustcmd{\GetLastPageNumber}[1][]{\pageref{#1LastPageInDocument}}%
\newrobustcmd{\GetLastPartNumber}[1][]{\pageref{#1LastPartInDocument}}%
\externaldocument[A-]{a}
\externaldocument[B-]{b}
\begin{document}
\textbf{\LARGE There are \GetLastPageNumber~pages in this document and it contains \GetLastPartNumber~parts}
\textbf{\LARGE There are \GetLastPageNumber[A-] pages in the other document and it contains \GetLastPartNumber[A-] parts}
\textbf{\LARGE There are \GetLastPageNumber[B-] pages in the other document and it contains \GetLastPartNumber[B-] parts}
\part{First}
\part{Second}
%%% Cover
%\pdf{1}{3}{3}{3}
%%% Inhalt
%\forloop{i}{1}{\value{i} < 34}{%
%\pdf{\thei*4+2}{\thei*4-1}{\thei*4}{\thei*4+1}
%}
%%% letzte 3 Seiten
%\pdf{3}{137}{138}{139}
\null\clearpage
% Maliciously setting page counter to zero!
\setcounter{page}{0}
% Reset part counter
\setcounter{part}{0}
\part{Starting over again}
\part{Starting over again, again;-)}
\end{document}
Files a.tex and b.tex are basically identical copies of readinpartnumber.tex
where I stripped them of unnecessary code.
a.tex
\documentclass[twoside,landscape]{scrreprt}
\usepackage{etoolbox}
\usepackage{everypage}
\usepackage{forloop}
\usepackage{pdfpages}
\usepackage{background}
\usetikzlibrary{calc}
\usepackage[a4paper]{geometry}
\geometry{top=6mm, left=6mm, right=0mm, bottom=0mm}
\makeatletter
\newrobustcmd{\LastPageInFile}{%
\immediate\write\@auxout{\string
\newlabel{LastPageInDocument}{{}{\arabic{DocumentTotalPageCounter}}}}%
}% End of LastProblem
\newrobustcmd{\LastPartInFile}{%
\immediate\write\@auxout{\string
\newlabel{LastPartInDocument}{{}{\arabic{DocumentTotalPartCounter}}}}%
}% End of LastProblem
\makeatother
\AtEndDocument{%
\LastPartInFile%
\LastPageInFile%
}
\newcounter{DocumentTotalPartCounter}% No resetting
\preto{\part}{\stepcounter{DocumentTotalPartCounter}}%
\newcounter{DocumentTotalPageCounter}
\AddEverypageHook{\stepcounter{DocumentTotalPageCounter}}
\newrobustcmd{\GetLastPageNumber}{\pageref{LastPageInDocument}}%
\newrobustcmd{\GetLastPartNumber}{\pageref{LastPartInDocument}}%
\begin{document}
\textbf{\LARGE There are \GetLastPageNumber~pages in this document and it contains \GetLastPartNumber~parts}
\part{First}
\part{Second}
\null\clearpage
\label{somelabel}
% Maliciously setting page counter to zero!
\setcounter{page}{0}
% Reset part counter
\setcounter{part}{0}
\part{Starting over again}
\end{document}
b.tex
\documentclass[twoside,landscape]{scrreprt}
\usepackage{etoolbox}
\usepackage{everypage}
\usepackage{forloop}
\usepackage{pdfpages}
\usepackage{background}
\usetikzlibrary{calc}
\usepackage[a4paper]{geometry}
\geometry{top=6mm, left=6mm, right=0mm, bottom=0mm}
\makeatletter
\newrobustcmd{\LastPageInFile}{%
\immediate\write\@auxout{\string
\newlabel{LastPageInDocument}{{}{\arabic{DocumentTotalPageCounter}}}}%
}% End of LastProblem
\newrobustcmd{\LastPartInFile}{%
\immediate\write\@auxout{\string
\newlabel{LastPartInDocument}{{}{\arabic{DocumentTotalPartCounter}}}}%
}% End of LastProblem
\makeatother
\AtEndDocument{%
\LastPartInFile%
\LastPageInFile%
}
\newcounter{DocumentTotalPartCounter}% No resetting
\preto{\part}{\stepcounter{DocumentTotalPartCounter}}%
\newcounter{DocumentTotalPageCounter}
\AddEverypageHook{\stepcounter{DocumentTotalPageCounter}}
\newrobustcmd{\GetLastPageNumber}{\pageref{LastPageInDocument}}%
\newrobustcmd{\GetLastPartNumber}{\pageref{LastPartInDocument}}%
\begin{document}
\textbf{\LARGE There are \GetLastPageNumber~pages in this document and it contains \GetLastPartNumber~parts}
\part{First}
\part{Second}
\null\clearpage
\part{Appendix}
\part{Index}
\part{Bibliography}
\end{document}

Note
The lines in the snapshot have their origin in the \backgroundsetup
code
Important
The written labels do not conform to hyperref.
The commands \GetLast...
and the connected code should be stored in .sty
file rather!
At the moment, it is quite a hack and not more.
\AtEndDocument{\label{anchor.lastpage}}
and\pageref{anchor.lastpage}
to extract the page number. This will require thehyperref
package to work.zref
to export the value like so: tex.stackexchange.com/questions/126079/… and maybe look at this: stackoverflow.com/questions/2115379/… for a dummy file which you can read and write to (indirectly linking your two documents)