How to create individual chapter PDFs from included TeXs?

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).


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.


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

Tags:

Pdf

Chapters