1

I've got a study book split into multiple individual books. To continue chapter and part numbers across volumes, I've used the solution by Werner provided here: Carrying counters over to another file

It's excellent! One problem: the command to store counters generates output in the document. The command works for my situation, but it produces extra output upon saving the counters. My question is: how can I get rid of the extra output?

MWE (which gives a "Missing number, treated as zero" error but does produce a PDF if you power through by pressing enter):

\documentclass[a4paper,10pt,fleqn,openany]{book}

% ---------------------------------------------------------------------------
% load chapter/part counters from previous workbooks 
% https://tex.stackexchange.com/questions/129312/carrying-counters-over-to-another-file
% ===========================================================================
\usepackage{refcount,xr}
\externaldocument{book1}
\externaldocument{book3}
% ---------------------------

% command for storing part/chapter counters
% ---------------------------
\makeatletter
\newcommand*{\storecounter}[2]{%
  % updated as per egreg's comment
  \edef\@currentlabel{\the\value{#1}}% Store current counter value in \@currentlabel
  \label{#2}% Store label
}
\makeatother

% command to get value of counters
% ---------------------------
\newcommand*{\getcounter}[2]{%
  \setcounterref{#1}{#2}% Retrieve label value and store it in a counter
}

% have a counter for chapters and for parts from volume 1
% ---------------------------
\newcounter{bookonecounter}
\getcounter{bookonecounter}{parts}
\setcounter{part}{\thebookonecounter}

\begin{document}

\part{A part}
\part{Another part}

% ===========================================================================
% Save chapter/part counters for use in further volumes
% XXX: this currently outputs "value in X"
% ---------------------------
\setcounter{bookonecounter}{\thechapter}\storecounter{bookonecounter}{chapters}
\setcounter{bookonecounter}{\thepart}\storecounter{bookonecounter}{parts}
% ===========================================================================

\end{document}

Using this example (volume 2) when you have volume 1 as "book1.tex" will correctly continue the part number. E.g., for a book1.aux that contains

\newlabel{chapters}{{4}{154}}
\newlabel{parts}{{2}{154}}

it compiles (with the complaint as noted above) into a pdf which continues the count. However, it adds a page to the end of the PDF which contains IV: output of MWE: extra page with "IV"

Commenting out the two setcounter/storecounter lines removes this from the pdf.

edit: egreg's proposal is definitely an improvement. The screenshot is the result of using his definition. Unfortunately, there's still an extra, unwanted page with some text on it on account of the \setcounter..\storecounter lines.

Any ideas on how to prevent the lines with \setcounter{}..\storecounter to produce an extra page?

  • 1
    I'm afraid your code doesn't compile on my system (MacTeX2016), possibly because I don't have access to book1.tex and book3.tex. Could you give a hint at what crucial commands might be in book1.tex? – Mico Jun 29 '16 at 16:36
  • \setcounter{bookonecounter}{\theparts} won't work if \thepart is defined to use \Roman, i.e. it will use I, II, III, not 1, 2, 3.... the same problem arises with \getcounter{bookonecounter}{parts} then! –  Jun 29 '16 at 17:49
  • Simple: change \edef\@currentlabel{\csname the#1\endcsname} into \edef\@currentlabel{\the\value{#1}} – egreg Jun 29 '16 at 20:21
  • Now Werner has fixed his answer. I think that we can close this. – egreg Jun 29 '16 at 20:24
  • @mico: sorry, it also gives a compilation bug for me, which I can just ignore. Should've mentioned that. relevant part of book1.aux: \newlabel{chapters}{{4}{154}} \newlabel{parts}{{2}{154}} – Hugo Jonker Jun 29 '16 at 21:55
  • @egreg: thanks! I've now got it working - it's indeed what you and christian hupfer pointed out. – Hugo Jonker Jun 30 '16 at 11:33

1 Answers1

1

There are some issues here: \setcounter{bookonecounter}{\thepart} won't work if \thepart will use \Roman{...} as counter output formatting, i.e. I, II, III etc. -- this can't be understood by \setcounter as valid input for counter values.

So never use \setcounter{foo}{\thefoobar} in almost any case!

\documentclass[a4paper,10pt,fleqn,openany]{book}

\begin{filecontents}{book1.tex}
\documentclass{book}

\renewcommand{\thepart}{\arabic{part}}
\begin{document}
\part{First part}\label{parts}

\chapter{First chapter}

\end{document}
\end{filecontents}


% ---------------------------------------------------------------------------
% load chapter/part counters from previous workbooks 
% http://tex.stackexchange.com/questions/129312/carrying-counters-over-to-another-file
% ===========================================================================
\usepackage{refcount,xr}
\externaldocument{book1}
\externaldocument{book3}
% ---------------------------

% command for storing part/chapter counters
% ---------------------------
\makeatletter
\newcommand*{\storecounter}[2]{%
  \edef\@currentlabel{\csname the#1\endcsname}% Store current counter value in \@currentlabel
  \label{#2}% Store label
}
\makeatother

% command to get value of counters
% ---------------------------
\newcommand*{\getcounter}[2]{%
  \setcounterref{#1}{#2}% Retrieve label value and store it in a counter
}

% have a counter for chapters and for parts from volume 1
% ---------------------------
\newcounter{bookonecounter}
\getcounter{bookonecounter}{parts}
\setcounter{part}{\value{bookonecounter}}

\begin{document}

\part{A part}
\part{Another part}

% ===========================================================================
% Save chapter/part counters for use in further volumes
% XXX: this currently outputs "value in X"
% ---------------------------
\setcounter{bookonecounter}{\value{chapter}}
\storecounter{bookonecounter}{chapters}
\setcounter{bookonecounter}{\value{part}}
\storecounter{bookonecounter}{parts}
% ===========================================================================

\end{document}