Produce pdf of selected sections

I'm writing a commissioned chapter for a handbook and I want to produce a pdf of three specific sections of my document to send to a colleague who is writing a different chapter for the same handbook (this is because there are some issues that span both chapters and we want to minimize overlap). Obviously, I can just copy-paste the relevant sections into a fresh tex file and produce a pdf from there, but I'm curious if there is a way of telling LaTeX "produce a pdf that contains only sections xxx, yyy, and zzz of this tex file".

I'm running TeX Live 2009, Emacs 23, and Ubuntu 12.04. I'm happy to use the command line if I need to.

EDIT, since some people seem to be thinking along these lines: I know about \includeonly{}, and I've used it in the past for this specific purpose with some large documents. I don't want to use it here because this chapter is not long enough to get into the trouble of managing a separate tex file for each section. I just want to know if it is possible to have one tex file and then produce a pdf containing only selected parts of that one tex file.

-
\includeonly{} command? However, this requires splitting the source file into separate files... – Christian Hupfer Jul 3 '14 at 9:31
By the way: TeXLive 2009 is a tiny bit, ehm, outdated(?) ;-) – Christian Hupfer Jul 3 '14 at 9:34
Well, that's effectively equivalent to pasting the relevant sections into a new tex file. I might end up doing just that, but my question is specifically whether one can compile specific regions/sections of one tex file without going to the trouble of keeping each region/section in its own subfile. – Koldito Jul 3 '14 at 9:37
Christian: I know, I know :/ but then: itre.cis.upenn.edu/~myl/languagelog/archives/000606.html – Koldito Jul 3 '14 at 9:38
I cheated a bit, but I edited my solution so that you may have only one tex file and compile on demand the part you want, using filecontents and includeonly. – Clément Jul 3 '14 at 9:52

One of the most used way to do that is to split your main file in several files, and to include them with the include command.

\documentclass{article}
% Preamble

\begin{document}
\include{chap1}
\include{chap2}
\include{chap3}
\include{chap4}
\end{document}


chap1.tex, …, chap4.tex being tex files containing only the content of those chapter (no preamble whatsoever).

If you want to produce a pdf containing only the chapters 2 and 3, you simply have to add

\includeonly{chap2, chap3}


in your preamble. This will need several compilation, to get the proper references, hyperlinks, toc, etc.

EDIT A kind of weird way to achieve that without splitting your main file could be to create each file with the filecontents package, i.e.:

\documentclass{article}
% Preamble
\usepackage{filecontents}
\begin{filecontents*}{chap1.tex}
% The content of chapter 1
\end{filecontents*}
% The same goes for the other files.

\begin{document}
\include{chap1}
\include{chap2}
\include{chap3}
\include{chap4}
\end{document}


You keep everything in one tex file, that is the advantage.

-
I actually kind of like the "weird way" better (for certain situations). – Kimball Apr 22 '15 at 9:57

This is a quick and dirty solution, but I do not recommend it really (;-))

Use the command \printthis[false]{%

before the portions of code you do not want to end up in the .pdf file and add } after the end of that portions. It hides the content as if would be a comment.

\documentclass{book}

\usepackage{blindtext}
\usepackage{etoolbox}

\newcommand{\printthis}[2][true]{%
\ifbool{#1}{%
#2%
}{%
% Drop it!!!!!
}%
}% End of \printthis
%

\begin{document}

\chapter{First}
\blindtext

\printthis[false]{%
\chapter{Two}
\blindtext

\chapter{Three}
\blindtext

}% End of \printthis

\chapter{Four}
\blindtext

\end{document}

-