# printing sample chapters in a book

I'd like to prepare a version of a book in which only selected chapters are printed, but for which the chapter titles and cross-references are all present as if the entire book had been printed. The idea is to make a "sample" of the book in which one can view some of the chapters and also see what is missing in the TOC and index, and to not have the numbering screwed up in the sample version. AFAICT \includeonly is not what I want, because it would then only process those chapters that are included (they are all in separate files). Is there a solution to this?

• it seems that \includeonly does exactly what you want. – David Carlisle Feb 13 '15 at 17:34
• so, i tried that, and what happens is that at the first \include, which is not in the \includeonly list, i get an error "Counter chnumber:firstchap is not defined", where "firstchap" is the \label of the first chapter. (actually, it is chap:firstchap, and so on). I can't figure out where this is coming from. – Bob Harper Feb 13 '15 at 17:53
• Can you provide a minimal (non)working example (MWE) showing what you tried, and how it failed? Strip out as much as you can while still reproducing the error. – Ethan Bolker Feb 13 '15 at 18:22
• @BobHarper user error:-) You have to first process the full document so that all cross reference information is resolved and stored in the aux files. Then you can selectively typeset while having all the references from the bits you skip. – David Carlisle Feb 13 '15 at 20:03
• thanks, that's the principle as far as i understand it. but if the .aux, etc files are created by a full run, the subsequent compile with \includeonly will cause errors about counters being undefined. specifically, all non-included includes will generate such an error. this seems like a bug somewhere, but maybe it's actually that it's an abuse of \includeonly to think that this approach should work. i don't know, but thank you. – Bob Harper Feb 13 '15 at 20:57

If you don't mind a bit of manual intervention, you can create the full pdf as normal and then use the pdftk program to create a new pdf having only some of the pages in the original. Using pdftk, you will need to specify the page numbers to keep. For example, to write pages 1, 5, and 12 thru the end of file you would use

pdftk in.pdf cat 1 5 12-end output out.pdf

• yes, that appears to be the best option, thank you. – Bob Harper Feb 13 '15 at 20:55

The root of the problem is that one cannot put \newcounter into a \include'd file. If you do, and then exclude it using \includeonly, then the counter is set without being "created", hence the error. One can ignore the errors and get the correct output, but the error itself is a bug in TeX, I would say. The creation of the counter should probably be recorded in the aux file, as well as the set of the counter.

• Good detective work. You can put the \newcounter in a preamble.tex that you \input before all the \includeonly material. I'm not sure I'd classify the error you found as a bug in (La)TeX. – Ethan Bolker Feb 13 '15 at 22:22