I wish to include a question/solution bank at the end of the chapter. I want to be able to control its printing via a single comment/uncomment operation. The MWE is an article but my intention is to produce a collection of books with 30+ chapters each. All the books share a common preamble file.
The questions are to be printed as a two column multicol. I am using a conditional compilation as outlined in [ How to easily write conditional pages? ] I dont claim to understand it, but it has worked for me so far. The only problem I face is when nesting it inside a \newenvironment.
In particular, there are two problems (indicated in the MWE with PROB-1 and PROB-2 tags) I face here -
- Nesting
multicolin a new environmentqbankthrows up compilation error. But nestingmulticolin a\newenvironment[quescol]and nestingquescolinqbankseems to work. Which puzzles me. In any case I can possibly live with it but would still like to know the real reason. - The conditional compilation scheme does not work as desired. The MWE compiles correctly but I cannot comment out
\SelectPartsForCompilation{queb}as desired. I cannot put\DeclareCompilationNameOfBlock[queb]outside\begin{qbank}block asqbankis in literally 100s of chapters (each in a separate file).
Actually nesting this conditional compilation scheme in any \newenvironment does produce a mess. PS: In my real documents there are several more compile tags besides queb.
\documentclass{article}
\usepackage{multicol,pgffor,environ,exsheets}
%https://tex.stackexchange.com/questions/247379/how-to-easily-write-conditional-pages/249823#249823
\makeatletter
\newcommand{\pgfkeysgsetvalue}[2]{
\pgfkeys@temptoks{#2}\expandafter\xdef\csname pgfk@#1\endcsname{\the\pgfkeys@temptoks}
}
\makeatother
\pgfkeys{/DeclareCompilationNameOfLine/.is family,/DeclareCompilationNameOfLine,
set/.unknown/.code={
\pgfkeysgsetvalue{/DeclareCompilationNameOfLine/\pgfkeyscurrentname}{1}
},
set/unset/.unknown/.code={
\pgfkeysgsetvalue{/DeclareCompilationNameOfLine/\pgfkeyscurrentname}{0}
}
}
\newif\ifPrintComment
\newcommand\SelectPartsForCompilation[1]{%
\foreach \key in {#1}{\pgfkeys{/DeclareCompilationNameOfLine,set/\key} }
}
\newcommand\MakeSelection[1]{
\PrintCommentfalse
\foreach \key in {#1,all} {
\pgfkeysifdefined{/DeclareCompilationNameOfLine/\key}%
{\pgfkeysgetvalue{/DeclareCompilationNameOfLine/\key}{\temp}
\ifnum\temp=1\global\PrintCommenttrue\fi% print if key=1
}{}
}
}
\newcommand\DeclareCompilationNameOfLine[2][]{\MakeSelection{#1}\ifPrintComment#2\fi}
\NewEnviron{DeclareCompilationNameOfBlock}[1][]{\MakeSelection{#1}\ifPrintComment\BODY\fi }
\setlength{\columnseprule}{0.4pt}
\newenvironment{quescol}{ % PORB-1 :: I am forced to use this clunky envirnoment
\begin{multicols}{2}
} {
\end{multicols}
\hrule
}
\newenvironment{qbank}{
\medskip
\DeclareCompilationNameOfBlock[queb]
\hrule
\begin{center}
\textbf{\textit{\LARGE Question Bank}}
\end{center}
\hrule
\quescol % PROB-1 :: if I directly use \begin{multicols}{2}, i get compliation error
} {
\endquescol
\endDeclareCompilationNameOfBlock
}
% PROB-2 :: If I comment out the follwing line, it throws up a compilation error
\SelectPartsForCompilation{queb} % comment this line for not including the question bank in PDF
\begin{document}
\section{first} Text of first
\section{second} Text of second
\section{third} Text of third
\begin{qbank}
\begin{question}
Transitive
\end{question}
\begin{question}
Reflexive
\end{question}
\begin{question}
Symmetric
\end{question}
\end{qbank}
\end{document}