Produce pdf of selected sections

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.


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}