Command \excludeonly for sections

I understand that there is an \excludeonly{xyz} command where xyz is a LaTeX file name (explained here). This excludes the specified file(s) from compilation. My question is if there is equivalent command for xyz being a specific section within a file. So lets say my source latex file (say source.tex) is:

\usepackage{....}

\begin{document}
\section{Chapter 1}
...

\section{Chapter 2}
...

\section{Chapter 3}
...

\section{Chapter 4}
...

\end{document}


and my compiled file (say thesis.tex) is:

\documentclass{article}
\input{source}


How can I exclude, say Chapter 2, from the compilation process.

Apologies if this has been answered before (I couldn't find it).

• Your example is a little bit confusing. You use \section, the titles are Chapter ...
– user31729
Aug 28, 2014 at 6:26

You must have separate tex files for each section. Then it is easy for you to exclude chapter2 section. If your code is drawn as shown below then chapter2 tex file will not be compiled.

\documentclass{article}
\usepackage{excludeonly}
\excludeonly{chap2}
\begin{document}
\include{chap1.tex}

\include{chap2.tex}

\include{chap3.tex}

\include{chap4.tex}

\end{document}

• You need \usepackage{excludeonly} for this
– user31729
Aug 27, 2014 at 19:29