# Cross-referencing on total counts between different files

Suppose that we have three .tex files in one folder called PhDThesis.tex, Synopsis.tex and conclusion.tex. There is also one bib-file called my_bibio.bib

In Synopsis we have a short description of PhDThesis. The conclusion is added both in Synopsis and in PhD_thesis.

We need to cite in Synopsis some total counts from PhD_thesis:

1. chapters
2. references (generated by biber)
3. figures
4. tables
5. pages of full text (the last page of the PhD)
6. pages of appendix (difference between 1st and last pages)
7. pages of full text without appendix (the last page of conclusion)

At the same time it would be great to cite some Equations and Propositions in PhDThesis from Synopsis. Hyperref package using is desirable to refer on the PhDThesis from a beamer presentation. Some questions are described here

To avoid some unexpected further difficulties with hyperref I would prefer to see both a variant without hyperref and with this package.

I guess that we can use somehow xr package.

Here is a link about application of this package to cite some labeled equations, propositions e.c.t. It works good in MWE.

Here is a link about totalcount and totcount for counting of total numbers of tables&figures, and references, respectively.

totalcount package works good with xr, however I can not reference total value like \ref{I-totalfigures}, \ref{I-totalpropositions} or \ref{I-totaltables}.

xr package doesn't want to work with totcount package at all! xr-hyperref doesn't want to work either, see please this page

One general solving is described here however I do not prefer to create a great number of counters and to have a complex way of theirs using.

Moreover I can't imagine how to save a difference of two \pagerefs to form a total count of pages in appendix.

Current view of Synopsis with highlighted desirable values of counters. How to fill these values?

MWE

PhDThesis.tex

    %\def\bibcite{\stepcounter{citenum}\oldcite}

%total count of figures and tables
% works good with xr package, however how to use this with xr package?!
\usepackage[figure,table]{totalcount}
% different from totcount, and not on CTAN. Found at
% http://www.mrunix.de/forums/showpost.php?p=257537&postcount=10
% Reference: http://www.latex-community.org/forum/viewtopic.php?f=5&t=9028

\begin{document}

\chapter{Approaches to the PhD problems} \label{chapt1}

\begin{figure}[ht]
\caption{First thesis figure}
\label{thesis-fig-first}
\end{figure}

\begin{figure}[ht]
\caption{Second thesis figure}
\label{thesis-fig-second}
\end{figure}

\begin{table}[ht]
\caption{First thesis table}
\label{thesis-tab-first}
\end{table}

\begin{table}[ht]
\caption{Second thesis table}
\label{thesis-tab-second}
\end{table}

\chapter{The core of PhD thesis} \label{chapt2}

\begin{propn}\label{thesis-prop-setminus}
$A \setminus B = C$
\end{propn}

\begin{propn}\label{thesis-prop-cup}
$A\cup B = C$
\end{propn}

$$\label{thesis-eq-closure} A = \overline{\overline{A}}$$

$$\label{thesis-eq-cap} A\cap B = C$$

Here we should mention two interesting papers \cite{firstPaper} and \cite{secondPaper}.

This document contains \totalfigures{} figures, \totaltables{} tables, and
%\total{citenum} reference (possibly with multiple citations). % does not want to work with xr package

\newpage
\include{conclusion}

\printbibliography

\newpage
\chapter{Appendix} \label{AppendixRef}

This is appendix
\label{thesis-app-last-page}
\end{document}


Synopsis.tex

    \documentclass[a5paper,10pt,twoside,openany,article]{memoir}

\usepackage{amsmath, amsthm, amssymb}
\newtheorem{prop}{Proposition}

\usepackage{xr}
\externaldocument[I-]{PhDThesis}

%unfortunately hyperref variant does not want to work
%this example is taken from: https://tex.stackexchange.com/questions/41539/does-hyperref-work-between-two-files
%\usepackage{xr-hyper}
%\usepackage{hyperref}
%\externaldocument[I-]{docPhDThesis}[PhDThesis.pdf]% <- full or relative path

\begin{document}

Here we need to mention that there are XXX chapters, XXX references, XXX figures, and XXX tables, XXX propositions in PhDThesis. PhDThesis consists of \ref{I-thesis-app-last-page} total pages, and XXX pages of Appendix, and \ref{I-common-concl-last-page} pages without Appendix.

The following proposition has \ref{I-thesis-prop-cup} number in PhDThesis.

\begin{prop}
$A\cup B = C$
\end{prop}

Here we need to mention, that \eqref{syn-eq-cap} equation is equal to equation, which has \eqref{I-common-concl-eq-cap}  number in common conclusion on page \pageref{I-common-concl-eq-cap} in PhDThesis. However the same equation has also \eqref{common-concl-eq-cap} number in common conclusion on page \pageref{common-concl-eq-cap} in Synopsis.

$$\label{syn-eq-cap} A\cap B = C$$

\input{conclusion.tex}

\end{document}


conclusion.tex

    This is a short description of PhDThesis results. The current part of the text is from the common conclusion of  PhDThesis and Synopsis.

$$\label{common-concl-eq-cap} A\cap B = C$$

\label{common-concl-last-page}


my_biblio.bib

    @INCOLLECTION{firstPaper,
author = {FirstAuthor, Name},
title = {Interesting first paper}
}

@INCOLLECTION{secondPaper,
author = {FirstAuthor, Name},
title = {Interesting second paper}
}


UPT Finally, I cope with problems in real-life complex tex-file! Many thanks to Heiko Oberdiek, Werner, Piet van Oostrum and Ulrich Diez.

Additionally to this question we discussed a question about total literature counter, a question about saving the values of zref counter on the side of a client tex-file, and a question about counting appendices and chapters

• A reasonable question, but not really a minimal working example! :) It would be better if you cut down the files so that they only contain what is necessary to demonstrate the problem and each file should compile. See minimal working example for more details as to what is expected.
– user30471
Nov 2, 2016 at 23:35
• @Andrew I forgot to mention that total counting of references should be compatible with biber. Perhaps, that's why we have some extra code lines, i.e. bib file and some extra code in PhDThesis. Other lines of code seems to be short, even figures and tables are with empty content. Nov 3, 2016 at 13:14

Package zref supports user-defined properties, which can added to labels. Module zref-lastpage creates a label LastPage at the end of the document. The properties of this label are managed in a property list LastPage. It contains the page number of the last page. Module zref-abspage adds property abspage, the absolute page number, to LastPage. This serves as total number of pages.

In addition, the following example defines a new property counter with contains the current chapter number. It is added to the property list LastPage and makes the last chapter number available for cross-referencing.

% mybook.tex
\documentclass{book}
\usepackage{zref-abspage, zref-lastpage}

\makeatletter
\zref@newprop{chapter}[0]{\thechapter}
\makeatother

% Label "LastPage" contains the following properties:
% * page: page number of last page
% * abspage: absolute page number
% * chapter: last chapter number

\begin{document}
\frontmatter
\tableofcontents
\mainmatter
\chapter{Introduction}
\chapter{Summary}
\end{document}


The mybook.aux file contains the label data, as explained above:

\zref@newlabel{LastPage}{\default{2}\page{3}\abspage{5}\chapter{2}}


## Referencing from a different document

Package zref contains module zref-xr for cross-referencing between documents. The .aux file is read by \zexternaldocument with an optional argument as prefix for the imported label names to avoid label name clashes.

\documentclass{article}
\usepackage{zref-xr, zref-user}

\zexternaldocument[book-]{test}

\begin{document}
The book has \zref[chapter]{book-LastPage} chapters
and \zref[abspage]{book-LastPage} pages.
\end{document}


## Further properties

The question contains a long list of properties to be remembered. Example sketches:

• Figure counting. It heavily depends on, how are figures specified in the document, does they contain a numbered caption per figure, how are they numbered, are there more than one caption in a figure environment. The simplest case are consecutive plainly numbered figures so that the last figure number is also the number of figures. This is implemented as chapter in the example above.

Otherwise a new figure counter can be created. The figures are then counted by hooking into environment figure or macro \caption or whatever depending on the document. A new property takes the value of this counter than as number of figures for label LastPage.

• Page ranges. Set a zref label at the begin and end of the interesting document part.

\usepackage{zref-abspage, zref-user}

\zlabel{page-range-begin}
...
\zlabel{page-range-end}


Make sure that the labels go to the correct pages. If the part starts with a chapter, then add the start label right after \chapter to avoid that the label might move to the previous page. The end label could be added at the end of the last paragraph, if there aren't floating objects following, or in the last floating object or at the start of the next page.

The calculation can be done the following way:

\makeatletter
% Mark labels as used to get LaTeX warnings for changed label values.
\zref@refused{page-range-begin}
\zref@refused{page-range-end}
% Calculation, which can be used as property value
\the\numexpr\zref@extract@default{page-range-end}{abspage}{0}
- \zref@extract@default{page-range-begin}{abspage}{0}
+ 1 % if the begin label is on the first and the end label on the last page
\relax
\makeatother


I suggest that you make different questions, if you have problems with the counting of some document elements. The links to the followup questions can be added to the corresponding items in this question. IMHO, this makes the questions and answers more clear.

• @ Heiko, thank you so much for the letter. I'll try to implement your approach for my book and ask you some additional questions. First question is how to reimplement \default value in \zref@newlabel (perhaps, this is not good for me, however I can try) or to reimplement your approach to count chapter numbers. Unfortunately, I have here \default{B}, so the total number of chapters is B for me and this is not true, there are 4 chapters in my book. Nov 3, 2016 at 15:53
• @VladimirParkhomenko Ignore the property default in LastPage. It does not serve a purpose here. It contains the value, what \ref would be using and it is not well-defined at this place. For the number of chapters use the property chapter instead. The examples of the answer show how it is defined, assigned and used. Nov 3, 2016 at 15:57
• @ Heiko, your approach works very well with MWE! Unfortunately almost all my counters in a book were hacked (except for page numbers), i.e. thefigure and other standard counter names do not work. However I can count total number of figures, tables and cites exactly in the same way as it is described here. May we somehow put these values into \zref? Where should we paste the block of code to make calculations of page ranges and how to send the result to another document (article) by means of \zref? Nov 3, 2016 at 22:32
• @VladimirParkhomenko I have no idea, what you mean with hacked figure numbers. If you can count the figures, then create a new property \zref@newprop{figure}[0]{<number of figures>} and add it to the label LastPagevia \zref@addprops{LastPage}{figure}. Make a new MWE (new question), which shows, how the figures are counted. Nov 3, 2016 at 22:43