The goal
My objective is to have continuous page (and possibly section) counters for several independent documents. The purpose is a cumulative dissertation where each paper should retain the journal's layout as much as possible, but the school requires continuous pagination. It's arguably going to look ugly, like several papers glued together, but that's essentially what it is.
I'm aware that the clean way to proceed would probably be to extract the formatting information from the journals' .cls
-files and pack into environments, but that would probably be a lot of work for someone as illiterate in plain TeX
as I am. Therefore, I've tried, with some success, an alternative approach: a hack using the xr
package. But there are some residual issues.
Let me first explain what I've been doing:
My setup
I have a main file which includes the introduction and conclusions. Its pagecounter and sectioncounter should both start at 1, so I have to update them with the values fed in from the last chapter of the body only at the breaking point. I have been having problems when trying to use \setcounter{page}{\pageref{external-endfile}}
within the document which seem to have to do with the representation of \pageref
's output as a string rather than an integer, and seem to be triggered by the babel
package so I feed the numbers into a \newcounter
which I then read into the actual counters when needed. Here's what this main file looks like:
\documentclass{article}
\usepackage{xr} %imports labels from external document
\externaldocument[finalCh]{file1} %prefix external labels to avoid name clashes
\newcounter{finalcount}
\setcounter{finalcount}{\pageref{finalChendfile}}% probably not the most elegant way to pack the input file's page number into a numberical variable, but it works.
\stepcounter{finalcount}% +1 since counters are initialised at 0
\newcounter{othersectioncount}
\setcounter{othersectioncount}{\ref{finalChendfile}}
\usepackage{ifthen} %conditionals
\newcommand{\exportcounters}{
\ifthenelse{%
\isodd{\thepage}} % if current page is odd-numbered
{\newpage \ % new page, forced space to make sure the following command is actually parsed, i.e. the page not treated as totally empty
}%
{\relax}
\label{endfile}
\newpage
}
%... other packages
\usepackage[english]{babel} %for some reason I don't understand, this clashes with treating \pageref as a number, so you have to put it *after* determining the page number
\usepackage{lipsum}
\begin{document}
\tableofcontents
\section{Introduction}
\subsection{Subsection}
\lipsum[1-2]
\newpage
\subsection{Another subsection}
\exportcounters
\setcounter{section}{\theothersectioncount}
\section{Conclusions}
\setcounter{page}{\thefinalcount} %load the final pagenumber of the last chapter here
\lipsum[4-40]
\end{document}
Besides that, I have a file for the chapter, which looks like this:
\documentclass{article}
\usepackage{substr}
\usepackage{xr} %imports labels from external document
\usepackage{ifthen} %conditionals
\externaldocument[intro]{introandextro} %prefix external labels to avoid name clashes
\addtocounter{page}{\pageref{introendfile}}
\newcommand{\exportcounters}{
\ifthenelse{%
\isodd{\thepage}} % if current page is odd-numbered
{\newpage \ % new page, forced space to make sure the following command is actually parsed, i.e. the page not treated as totally empty
}%
{\relax}
\label{endfile}
\newpage
}
\usepackage{lipsum}
%\setcounter{section}{2}
\begin{document}
\setcounter{section}{\ref{introendfile}}
\section{Chapter I}
\subsection{intro}
\lipsum[1-4]
\newpage
\subsection{Chapter 1 body}
\lipsum[5-44]
\exportcounters
\label{endfile}
\end{document}
With this setup, I only need to run two times each "pdflatex introandextro", "pdflatex file1" and again "pdflatex introandextro" to get a continuous page and section numbering. (I have a shell script to do that for me.) I also have a Python script to extract all toc
-relevant information from the different aux
-files and compile the main file with that as its toc
in order to get a table of contents referencing all sections no matter which file they appear in which mostly works as it should.
As it should, \addtocounter{page}{\pageref{introendfile}}
imports the pagecounter at the end of the introduction into file1. Also as it should, \setcounter{section}{\ref{introendfile}}
eats the section part of the external references return value (1.2 in this case) and appropriately uses it to set the section counter. Unfortunately, it doesn't know what to do with the rest of the expression, so it spits it out again, writing ".2" on top of the page. That's ugly, so I'm trying to get rid of it. My hunch is that the best method for this would be to temporarily redefine \label
within \exportcounters
so that it only sends the topmost sectioning command to .aux
and .toc
, but my plain tex is not good enough to know where to start.
Residual Issues
So my question: Can anyone give me hints how to redefine the \label
-command appropriately? In other words, how do I make it tell the .aux
-file \newlabel{endfile}{{<thesection>}{12}}
rather than \newlabel{endfile}{{<\thesection>.<\thesubsection>}{12}}
despite being embedded in a subsection or deeper? (Alternatively, how do I strip the part from '.' on away before feeding it into \setcounter{section}{...}
? I've tried \SubStringBefore
but that produces a string rather than a number, throwing me a "Missing number, treated as zero" error during compilation.)
A second issue I'm having is that the need to insert an explicit \newpage
where I want the first page to end in both documents, otherwise it would run on for two pages worth of text. This behavior seems to be triggered by xr
(or the conjunction of xr
and lipsum
). Is this a known issue, and are there any workarounds that don't involve explicit \newpage
s?
Another (really quite minor) issue is that pdflatex complains about the use of \ref
in the preamble ("Missing \begin{document}"), but I'm fine with just ignoring that if the result looks right.
Or maybe it's possible to tackle this problem from quite a different angle? Is there a tool to assist people who don't understand much plain TeX in packing .cls
formatting commands into an environment? Something like \importasenvironment{<environmentname>}{<classname>.cls}
?