An environment for conditional compilation using a switch

I want to bar certain parts of my document from compiling based on a toggle switch. The reason is that my books contain a lot of PGF diagrams and graphs and the whole book takes a lot of time to compile. The number of chapters is also huge. When I am writing a particular chapter, I usually comment out chapters corresponding to other \input or \include files.

I thought of creating a new environment which has a toggle switch to do this automatically. Just one comment/uncomment will instantly switch back and forth between single chapter document and full document.

This is my MWE

\documentclass{book}
\usepackage{etoolbox}

\edef\leftbracechar{\string{}
\edef\rightbracechar{\string}}

\newcommand{\dontcompile}{true} % 'true' is meaningless, any word should do
\newenvironment{condcompile}{
\ifdef{\dontcompile} \leftbracechar } { \rightbracechar }

\begin{document}

\begin{condcompile}
\chapter{first} % a chapter is usually a seperate file but here this should do
Text of first
\end{condcompile}

\chapter{second}
Text of second

\chapter{third}
Text of third

\end{document}


If I comment out the line \newcommand{\dontcompile}{true} I expect the document to behave as if all chapters are to be compiled and it seems to work well.


Is there a way to rectify it?

• You want to look at the comment package – egreg Jun 24 '17 at 19:20
• – Andrew Jun 24 '17 at 21:42
• @Andrew Thanks! It serves the purpose beautifully. – magguu Jun 25 '17 at 4:58