[Tex/LaTex] How to create individual chapter PDFs from included TeXs

chapterspdf

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?

Best Answer

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

Related Question