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?

  • 3
    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
  • 1
    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.

Thanks for your help.

  • 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
  • I agree that's it debatable whether it's a bug. But by not putting the newcounter declaration into the aux file, it is guaranteed that excluding a file using \includeonly will generate this very mysterious error, once for each \newcounter in each of the excluded files. – Bob Harper Feb 14 '15 at 1:58

First compile the whole work, including any indexing, etc. Then insert the \includeonly{} line and recompile only the text (don't re-index). It may throw some errors like "no counter ... defined" or other things, but it should still generate a new pdf which will have a full table of contents, the chapters you want (with correct numbers) and the original index.

At least that's what happened which I tested this solution on my book. (I'm using bookest document style.)

  • ah! the whole issue is that it throws those errors, which i assumed were some sort of bad package interaction. but my attempts to debug it failed. it seems that the full run leaves behind stuff in the aux files (and perhaps other such files) that causes these mysterious errors. i am going to leave the matter at that, thank you. – Bob Harper Feb 13 '15 at 20:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.