13

I am using package xr to cross reference between two files.

Is there a way to make the equation numbers in one of the files follow the ones in the other? What I mean is, if the last equation in file A is say (28), then the first equation in file (B) be (29) and so on.

Another question is, can this be done with counters for lemmas, theorems, etc?

1
  • 1
    I am interested in the answer for this, ie, imagine producing a multi-compendium book series... Commented May 29, 2013 at 11:21

2 Answers 2

4

A solution using package zref:

Document A

The first document test-A.tex writes the latest value of counter equation in the zref reference eq-last:

% test-A.tex
\documentclass{article}

\usepackage{zref-base}
\usepackage{atveryend}
\makeatletter
\AfterLastShipout{%
  \if@filesw   
    \begingroup
      \zref@setcurrent{default}{\the\value{equation}}%
      \zref@wrapper@immediate{%
        \zref@labelbyprops{eq-last}{default}%
      }%
    \endgroup
  \fi
}
\makeatother

\begin{document}
\begin{equation}E=mc^2\end{equation}
\end{document}

Remarks:

  • Instead of \AtEndDocument it uses the hook \AfterLastShipout that is executed after the last page is shipped out and before the .aux file is closed.

  • \zref@wrapper@immediate maps \write to \immediate\write in order to write into the .aux file directly. At this point deferred writing without \immediate will not work, because the last page is already shipped out and there is no next page.

  • The code is put inside \if@filesw and \fi. That supports \nofiles, that forbids writing to auxiliary files.

Document B

The next "book in the series" imports the references from document test-A. The reference names are prefixed with A- to avoid name clashes (\zexternaldocument):

% test-B.tex
\documentclass{article}

\usepackage{zref-xr}
\makeatletter
\zexternaldocument[A-]{test-A}
\AtBeginDocument{%
  \setcounter{equation}{\zref@extractdefault{A-eq-last}{default}{0}}%
  \zref@refused{A-eq-last}%
}
\makeatother

\begin{document}
\begin{equation}v=\frac{s}{t}\end{equation}
\end{document}

Remarks:

  • \zref@extractdefault is expandable and can be directly used inside the value part of \setcounter.
  • \zref@refused complains, if the reference is not defined. LaTeX clears the warning status for undefined refernces in \begin{document}. Thus we need \AtBeginDocument (otherwise the final "LaTeX warning: There were undefined references." might be missing).
3
  • Thanks. I don't want to prefix reference names from the previous document with A-. I tried to remove [A-] and changed A-eq-last to eq-last, to no effect. If I add the lines \usepackage{xr} and \externaldocument{A-test} early on, it seems to work.
    – passerby51
    Commented May 31, 2013 at 3:45
  • Also, do you have a short write-up about those zref commands? I mean more explanation of the inner workings. What is \the\value construct and why you need to change and read default. What is default anyway?
    – passerby51
    Commented May 31, 2013 at 3:48
  • @passerby51 There is the documentation of package zref. \value{<counter>} returns the count register for the LaTeX counter <counter>. Here \the or \number prints the value of the count register. Commented May 31, 2013 at 5:32
4

Here is one way. In this MWE, I save two counters to an external file: \thesection and \theequation. Obviously, you can expand the solution to include saving whatever status variables you need to transfer from one document to the next (such as \thepage, etc.).

I have created two commands: \recallstatus and \savestatus that should be invoked, respectively, at the beginning of the document part (except for part 1) and at the end of the document part. In this MWE, I define them in each preamble, but it would be easier to stuff them into a multipart.sty or some name like that for which you could just \usepackage.

Here is part 1 of the document

\documentclass{article}
\newcounter{docpart}
\def\savestatus{%
  \newwrite\tempfile%
  \immediate\openout\tempfile=docstatus\arabic{docpart}.dat%
  \immediate\write\tempfile{\thesection}%
  \immediate\write\tempfile{\theequation}%
  \immediate\closeout\tempfile%
}
\newcounter{olddocpart}
\def\recallstatus{%
  \setcounter{olddocpart}{\arabic{docpart}}
  \addtocounter{olddocpart}{-1}
  \newread\rtempfile%
  \openin\rtempfile=docstatus\arabic{olddocpart}.dat%
  \read\rtempfile to \tmp%
  \setcounter{section}{\tmp}
  \read\rtempfile to \tmp%
  \setcounter{equation}{\tmp}%
  \closein\rtempfile%
}

\setcounter{docpart}{1}
\begin{document}
\section{The First Section}
\begin{equation}
y = x
\end{equation}
\begin{equation}
y = 1/x
\end{equation}
\savestatus
\end{document}

The \savestatus at the end creates a file docstatus1.dat that contains the data (current section and equation):

1
2

Here is part 2 of the document:

\documentclass{article}
\newcounter{docpart}
\def\savestatus{%
  \newwrite\tempfile%
  \immediate\openout\tempfile=docstatus\arabic{docpart}.dat%
  \immediate\write\tempfile{\thesection}%
  \immediate\write\tempfile{\theequation}%
  \immediate\closeout\tempfile%
}
\newcounter{olddocpart}
\def\recallstatus{%
  \setcounter{olddocpart}{\arabic{docpart}}
  \addtocounter{olddocpart}{-1}
  \newread\rtempfile%
  \openin\rtempfile=docstatus\arabic{olddocpart}.dat%
  \read\rtempfile to \tmp%
  \setcounter{section}{\tmp}
  \read\rtempfile to \tmp%
  \setcounter{equation}{\tmp}%
  \closein\rtempfile%
}

\setcounter{docpart}{2}
\begin{document}
\recallstatus
\section{The Second Section}
\begin{equation}
y = x^2
\end{equation}
\begin{equation}
y = 1/x^2
\end{equation}
\savestatus

\end{document}

The first thing it does is read the part 1 data file to reset the appropriate counters. As you see from the output, it picks up the sectioning and equation numbers where it left off. At the end, I do another \savestatus (which writes docstatus2.dat) to prepare for part 3.

Here is the document image from part 1:

enter image description here

and here it is from part 2:

enter image description here

Obviously, if you modify a counter in part 1, parts 2, 3, etc. all have to be successively recompiled in order that their counters get updated.

You must log in to answer this question.

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