# How to create individual chapter PDFs

My document structure has

• one file with preamble (i.e. MainTex)
• and around 75 individual .tex files (i.e. Chapter_01, Chapter_02, ..., Chapter_75) representing each chapter.

I am using \include command to create a one big PDF file, the output is MainTex.pdf. Life is all good up to this point.

If I have to create individual PDF for each chapter then do I need to run 75 times with each chapter name and rename the PDF manually to match to chapter name. Because if I include only Chapter_01 and run it creates PDF named as MainTex.pdf.

Is there a way I can automate this workflow?

-
Not an automatic solution, but the program pdfsam allows one to split a PDF based on the bookmark levels of the PDF, so if you use the hyperref package, that is perhaps less work. –  Torbjørn T. Oct 12 '11 at 14:41
Not automated here either, and very inelegant, but better than compiling and modifying 75 times: what about simply splitting the resulting pdf file, ignoring its origin? For instance, in Mac OS X's Preview you can simply "print" on a new pdf file a range of pages from an existing file. Not so helpful, perhaps... –  DaG Oct 12 '11 at 15:46
–  Martin Schröder Oct 12 '11 at 16:51
@Torbjørn T.: Thanks for making me aware of pdfsam. It works great. –  chandra Apr 21 '12 at 8:53

Suppose your main file is maintex.tex:

\documentclass[a4paper]{book}
\begin{document}
\include{chap01}
\include{chap02}
\include{chap03}
\end{document}


Then you can compile only chapter 1 by calling, from the command line

pdflatex -jobname=thechap01 "\includeonly{chap01}\input{maintex}"


In order to do all at once, the command might be (bash shell)

for i in chap*.tex; do j=${i%.tex}; pdflatex -jobname=the$j "\includeonly{$j}\input{maintex}"; done  It's necessary to change the output file name from chap01 to something different, because otherwise the reading of the .aux file would lead to an infinite loop. It's easy to rename the obtained PDF files afterwards (or in the same command, by adding ; mv the$j.pdf $j.pdf before done). - @awesome it worked great, all I had to do was to replace pdflatex to xelatex. I did not have to rename the files, it did not go into infinite loop. – Aku Oct 16 '11 at 0:41 Is invoking pdflatex once per chapter enough? – Please don't touch Jan 1 '12 at 15:49 @CounterTerrorist This should be performed when the "big" document has already resolved all cross-references, so one run is sufficient. – egreg Jan 1 '12 at 15:53 This works very well but seems to be missing the bibtex references. I built the main file separately to get the references working but split chapters have ?s. – recluze Jan 26 '12 at 3:45 @recluze First you have to compile the whole document, so that all cross references are resolved, and be sure that the bibliography has been produced. – egreg Jan 26 '12 at 7:32 show 2 more comments Yes, this workflow is supported, but it has to be performed from the start, rather than after the fact. Here's what I mean by that. The subfiles package provides the means to actually have individual sub files that are included in your main .tex file (using \include or \includeonly) but also are compilable themselves. The package should be included in the main .tex, while each subfile has a working preamble. You can then iterate (loop) over the respective subfiles (chapters in your case) using a standard bash script, and compile each chapter using latex, pdflatex or xelatex. For example, under DOS, the following should work: for /f %%a IN ('dir /b Chapter_??.tex') do call pdflatex %%a  You may have to run this entire script at least two times for references in each chapter to work. The standalone document class provides a similar functionality, skipping preambles of included files and only considering content contained within the document environment when compiling the main .tex. Included files, however are individually compilable. The main drawback from compiling this way is referencing across chapters and page numbers that will restart at 1 for each chapter. The former might be addressed using the xr package, while the latter may be addessed by inserting the respective page number modification in the document preamble (via \setcounter{page}{...}), perhaps even reading it from the main .aux file. Regardless, this fiddling may be difficult to master if not set up properly. - add comment I wrote a bunch on the standalone package, and then saw Werner beat me too it. Ah well! As an aside, I use Chris Monson's Ultimate LaTeX Makefile to build documents. If you have a UNIX system (or cygwin), it just works. For example, you could type "make Chapter_01" (to use your file name) and it would produce a nice PDF with correct references, etc. However, its best feature is automatic dependency tracking. It would be able to determine that your main file depends on the 50+ chapter files, and it will recompile as necessary. This is great if you are tweaking a chapter but building the entire document. It even takes care of running latex and biblatex enough times to get everything working right! - add comment Here's a way that uses the fabulous arara tool to implement @egreg's solution main.tex % arara: makechapters: {items: [lions, zebras]} \documentclass{report} \begin{document} \include{lions} \include{zebras} \end{document}  When you call arara main.tex  you will get lions.pdf and zebras.pdf. You can list any number of chapter files in the items argument, and you can also choose to set compileAll: off if you don't want to compile main.tex first. The default is compileAll: on, and I'd recommend only turning it off if you are 100% sure that the necessary .aux files are up to date. For a big document, it'll take a while, but it's the kind of thing that you set running before stepping away from your desk for a bit. makechapters.yaml !config # Make chapter files rule for arara # author: Chris Hughes # last edited by: cmh, May 20th 2013 # http://tex.stackexchange.com/questions/31334/how-to-create-individual-chapter-pdfs # requires arara 3.0+ # # Sample usage: Assume you have the following directives in main.tex, with chapter files # lions.tex, zebras.tex # # % arara: makechapters: {items: [lions]} # % arara: makechapters: {items: [lions, zebras]} # % arara: makechapters: {items: [lions, zebras], compileAll: no} # % arara: makechapters: {items: [lions, zebras], compileAll: yes} # # which will create lions.pdf, zebras.pdf # # Note that, by default, this compiles main.tex first so that all of the necessary .aux # files are generated- this is vital for cross referencing to work, particularly in # the case of a *forward* cross reference (e.g chapter 2 refers to chapter 3). # # If you set compileAll to false/no/off, then it will *not* compile the main file # first- be careful with this one, as the necessary .aux files may not be present, and # your cross references may break. identifier: makechapters name: MakeChapters commands: - <arara> @{ isTrue( compileAll, engine.concat(' "').concat(file).concat('"') )} - <arara> @{engine} -jobname=tmpCMH "\includeonly{@{item}}\input{@{file}}" - <arara> @{engine} -jobname=tmpCMH "\includeonly{@{item}}\input{@{file}}" - <arara> @{ isWindows( "cmd /c move", "mv" ) } tmpCMH.pdf @{item}.pdf arguments: - identifier: engine flag: <arara> @{parameters.engine} default: pdflatex - identifier: compileAll flag: <arara> @{parameters.compileAll} default: true  - add comment I have a similar workflow, and what I do is only semi-automatic, but it's enough for my use case. After I read this post, I wrote a simple (and funny, I guess) LaTeX template for doing this. (It might not be useful for you, since one has to know the pagenumbers, but it is still more comfortable for me than pdfsam or "print to file" solutions.) - add comment See my reply at Extract document statistics? - How many pages has chapter xy? Count fixmes?: python script which extracts chapters/sections/... from .toc file and splits the final PDF. - add comment I'm experimenting with the following approach. Let the master file contain this: \documentclass{...} \usepackage{newclude} \def\inmaster{1} \ifdefined\subonly\includeonly{\subonly}\fi \begin{document} \include{subone} \include{subtwo} \end{document}  and the subfiles begin like this (no preamble or \begin{document}): \ifdefined\inmaster\else\def\subonly{\jobname}\input{master}\fi \chapter{Chapter Name} ...  Finally, make all the .aux files symbolically linked, e.g. with the bash command for f in *.tex; do ln -s master.aux${f%.tex}.aux; done


Now the master file can be compiled on its own to produce output containing all the chapters, and likewise each individual chapter file can be compiled on their own to produce output containing only that chapter (but which will still be correctly numbered and can refer to labels in other chapters).

This has the advantage that once it's set up, each .tex file can be compiled just like normal; no fancy makefiles, scripts, or compilation tools are required. However, it has the disadvantage of requiring the initial setup of symbolic links (and in particular of requiring an OS that supports them). But one can trade-off the symbolic links for a slightly more complicated compilation, compiling each chapter file with e.g.

cp master.aux chapterone.aux && pdflatex chapterone


Either way, a script could easily loop over the chapter files and compile them all.

-