3

I've set up a document to have multiple sections, and I want it to have consistent, increasing page numbers throughout. I also want to be able to typeset the individual files for printing and get the same page numbering (so that it can have a table of contents when passed out to the students).

I have this MWE working to typeset the document, but when I typeset part2 individually, it begins with page 1, not page 2. I don't want to have to manually set the page number for each of the 40 or so parts.

How do I get it to typeset with consistent page numbers for all or one file?

Here's the code.

mainMWE.tex:

documentclass{article}
\usepackage{subfiles}
\usepackage{refcount}
\usepackage{xstring}

\begin{document}

\thispagestyle{empty}
\begin{center}
\huge{My Title}
\end{center}

Some text.

\newpage

\pagenumbering{roman}
\setcounter{page}{1}

\tableofcontents

\newpage

\pagenumbering{arabic}
\setcounter{page}{1}

\subfile{part1MWE}

\subfile{part2MWE}

\end{document}

part1MWE.tex

\documentclass[mainMWE]{subfiles}

\begin{document}

\section{First Section}

Some more meaningless text.

\end{document}

part2MWE.tex:

\documentclass[mainMWE]{subfiles}

%\IfEq{\jobname}{\detokenize{main}}{}{ 
%  \usepackage{zref-xr}                       
%  \zxrsetup{toltxlabel}
%  \zexternaldocument*[mainMWE-]{mainMWE}  
%  \setcounterpageref{page}{mainMWE-part2MWE}

\begin{document}
\newpage

\section{Second Section}

Even more meaningless text.

\end{document}

The stuff commented out was suggested to solve the page number consistency problem. Any advice appreciated.

Martin

1
  • There is also \include and \includeonly. \includeonly lists only the files to be printed (and compiled) this time, but the page numbers and references will be consistent. Aug 17, 2018 at 15:02

1 Answer 1

2

You could use the xr package for this. It allows you to refer to cross references in external files. (I see now that this is basically the same as your suggested solution [and you could probably use zref-xr if you needed it], but shows a working method.)

You need to give each section a label and load the xr and refcount packages in your main document. Then set the page number in each sub file based on the section label in the main document

Obviously, you will have to build the main document once first, so you know how long part 1 is and hence what page part 2 starts on.

I've also used the titlesec passage to insert section page breaks.

Something like this:

mainMWE.tex

\documentclass{article}
\usepackage{subfiles}
\usepackage{refcount}% required for \getpagerefnumber
\usepackage{xr}% required to reference labels in external documents
\usepackage{titlesec}% to insert a page break before sections
\newcommand{\sectionbreak}{\clearpage}
\begin{document}
\title{My Title}
\author{Martin}
\maketitle
Some text.
\thispagestyle{empty}
\tableofcontents
\pagenumbering{roman}
\clearpage
\pagenumbering{arabic}
\subfile{part1MWE}
\subfile{part2MWE}
\end{document}

part1MWE.tex

\documentclass[mainMWE]{subfiles}
% set page number if compiling sub file
\ifcsname preamble@file\endcsname
  \externaldocument[main-]{mainMWE}
  \setcounter{page}{\getpagerefnumber{main-part1}}
\fi
\begin{document}
\section{First Section}
\label{part1}
Some more meaningless text.
\end{document}

part2MWE.tex

\documentclass[mainMWE]{subfiles}
% set page number if compiling sub file
\ifcsname preamble@file\endcsname
  \externaldocument[main-]{mainMWE}
  \setcounter{page}{\getpagerefnumber{main-part2}}
\fi
\begin{document}
\section{Second Section}
\label{part2}
Even more meaningless text.
\end{document}
1
  • Seems to work well, once main is typeset a few times. Thanks, David.
    – Martin
    Aug 17, 2018 at 3:36

You must log in to answer this question.

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