# Read part and page count from auxiliary file

My main document is included into another that rearranges and adds crop marks to it.

For universalizing my script I need to extract the page and part count out of the .toc- or .aux-file.

These are the lines from .toc:

\contentsline {chapter}{XXX}{139}{section*.96}
\contentsline {part}{\bfseries \leavevmode {\color {gray}\Ordinalstringnum {9}~Teil:\hspace {0.5em}XXX}}{135}{part.9}


and .aux:

\@writefile{toc}{\contentsline {chapter}{XXX}{139}{section*.96}}
\@writefile{toc}{\contentsline {part}{\bfseries  \leavevmode {\color  {gray}\Ordinalstringnum {9}~Teil:\hspace  {0.5em}XXX}}{135}{part.9}}


Is there any way to get the page count when there's no new chapter, section, etc. on the last page?

MWE:

\documentclass[twoside,landscape]{scrreprt}
\usepackage{forloop}
\usepackage{pdfpages}
\usepackage{background}
\usetikzlibrary{calc}
\usepackage[a4paper]{geometry}
\geometry{top=6mm, left=6mm, right=0mm, bottom=0mm}

\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}

\begin{document}

%%% 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

\end{document}


I need it for these lines:

\forloop{i}{1}{\value{i} < 34}{% For this line I need something like \floor{\maxpage/4} instead of 34

\pdf{3}{137}{138}{139} This line adds the last 3 pages. If I include them in the loop, I get an error as page 140 doesn't exist. A macro containing a if-condition would be great (if #1 > \maxpage then \emptypage)

Edit:

A.pdf is my main file. B.tex includes the pages of A.pdf, rearranges them and adds crop marks for printing. The final result that gets printed in the press is B.pdf.

The code I posted above is from B.tex. Here I need the page count of A.pdf therefore I require a command for extracting the page count out of the auxiliary files A.aux or A.toc to use it in B.tex. Hopefully that helped to clarify my question.

-
What do you want to use the count for? I usually use \AtEndDocument{\label{anchor.lastpage}} and \pageref{anchor.lastpage} to extract the page number. This will require the hyperref package to work. –  1010011010 May 5 '14 at 18:10
My main file A.pdf is included into B.tex. Here it gets rearranged and crop marks added (tex.stackexchange.com/questions/175285/…). Rearranging is provided by a for loop. That's why I need the page count lest I will have to change B.tex each time the page count of A.pdf changes. –  Moldevort May 5 '14 at 18:29
Are you currently using any options that might change the internal page counter? Consider using: ctan.mirrorcatalogs.com/macros/latex/contrib/totpages/… –  1010011010 May 5 '14 at 18:32
And how can I access this information of A.pdf in B.tex? –  Moldevort May 5 '14 at 18:37
Use 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) –  1010011010 May 5 '14 at 18:42

## 2 Answers

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.

-
As you misunderstood my question, I edited my original post. –  Moldevort May 5 '14 at 20:11
@Moldevort: From first post it was not clear that page count from A has to be used in B. I will keep it in mind, but not in this evening. It should be possible with crossreferencing. –  Christian Hupfer May 5 '14 at 20:16
Isn't it possible to change the directory where the auxfile is? What about naming the two files identically, setting one file in the alternative directory so they "share" the .aux file? I'm pretty sure it's possible to extract only portions of the .aux file for use in the seperate files. –  1010011010 May 5 '14 at 20:53

I just found an answer myself (didn't see the other one):

A.tex:

.
.
.
\newcommand\addprint{%
\newwrite\tmp
\immediate\openout\tmp=_drucken.txt
{\immediate\write\tmp{\thepage}}%
\immediate\closeout\tmp
}

\begin{document}
.
.
.
\addprint
\end{document}


_drucken.txt:

139


B.tex:

\documentclass[twoside,landscape]{scrreprt}
\usepackage{forloop}
\usepackage{pdfpages}
\usepackage{background}
\usepackage{xstring}
\usetikzlibrary{calc}
\usepackage[a4paper]{geometry}
\geometry{top=6mm, left=6mm, right=0mm, bottom=0mm}

\usepackage{xparse}
\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\ceil}{m}
{
\fp_eval:n { ceil ( #1 ) }
}
\ExplSyntaxOff

\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}%
}}

\pagestyle{plain}
\newcounter{i}
\def\empty{3}

\newcommand\getprint{%
\newread\tmp
\openin\tmp=_drucken.txt
\read\tmp to \maxpage
}

\newcommand{\chk}[1]{
\ifnum#1 > \numexpr\maxpage\relax
\empty
\else
#1
\fi}

\newcommand{\ing}[1]{\includegraphics[width=(\textwidth-6mm)/2,height=\textheight-6mm,page=\chk{#1}]{A.pdf}}

\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%
}

\begin{document}
\getprint

%%% Cover
\pdf{1}{\empty}{\empty}{\empty}

%%% Inhalt
\forloop{i}{1}{\value{i} < \numexpr \ceil{\maxpage/4}+1 \relax}{%
\pdf{\thei*4+2}{\thei*4-1}{\thei*4}{\thei*4+1}
}

\null\clearpage

\end{document}

-