Q:

How to easily write conditional pages?

Is there any easy way to make latex write only some of the pages?
My current idea is to make a function using the ifthen-package in the following way:
\newcommand{\WhatDoYouWantToWrite}[1]{
      \ifthenelse{\equal{#1}{All}}{
text that will be written if all is wanted
}
      \ifthenelse{\equal{#1}{PartA}{
text that will be written if part A is wanted
}
}
{Wrong input chosen. Don't know what to write}

This works, but now my problem is that if I'm changing the text I have to do it several places, which is frustrating. Then my idea was to make a separate file and use \input, but how can I then use the \ifthenelse-statement?
I have tried using the comment-package, but then I have to keep moving around in the text turning on and off \begin{comment} \end{comment}. Can it be used in a clever way in this setup?
It looked like the exam class, where one can choose if one wants to show solutions of problems, but then there is only two options of output, with or without solutions. I want more! 
My hope is to just use one function in the beginning of the document which turns on/off the parts that I want/don't want.

A:

Here is a flexible solution using pgfkeys that allows for arbitrary switches that are given as keys to the command/environment. I define two (user) commands \SelectCommentsToPrint and \SelectiveComment. The first command controls which comments will be printed and the second command defines the comments and specifies under what conditions they should be printed. Both commands take arbitrary "keys". For example.
\SelectCommentsToPrint{one,three,five}

specifies that comments labeled "one", "three" or "five" should be printed and all other comments should be ignored. After this a comment can be given as
\SelectiveComment[one,four,eleven]{This is a silly comment}

and this comment will be printed only when any one of "one", "four" and "eleven" have been set using \SelectCommentsToPrint. At any point you can issue another \SelectCommentsToPrint to add extra comments to print. You can also use
\SelectCommentsToPrint{all}

to print all comments and
\SelectCommentsToPrint{unset/four}%      stop four from being printed
\SelectCommentsToPrint{unset/all,five}%  turn off all, add five - previously set comments will print

to stop "four" etc from being printed. There is also a SelectiveEnvironment environment for printing longer comments, this uses the environ package. The syntax is similar:
\begin{SelectiveEnvironment}[one,four]% print if one or four is set
  some nice stuff
\end{SelectiveEnvironment}

Here is the output from the MWE:

If a comment is not printed in the MWE then a ? is printed instead. The remarks after the --- say why something was printed or not printed. The words in blue indicate when the printing of comments is turned on or off.
Here is the code:
\documentclass{article}
\usepackage{pgf,pgffor}
\usepackage{environ}
\usepackage{xcolor}% only needed for highlighting selections in MWE

% https://tex.stackexchange.com/questions/15204/is-there-a-way-to-set-a-global-key-value-using-pgfkeys
\makeatletter
\newcommand{\pgfkeysgsetvalue}[2]{\pgfkeys@temptoks{#2}\expandafter\xdef\csname pgfk@#1\endcsname{\the\pgfkeys@temptoks}}
\makeatother

\pgfkeys{/SelectiveComment/.is family,/SelectiveComment,
  set/.unknown/.code={
      \pgfkeysgsetvalue{/SelectiveComment/\pgfkeyscurrentname}{1}
  },
  set/unset/.unknown/.code={
      \pgfkeysgsetvalue{/SelectiveComment/\pgfkeyscurrentname}{0}
  }
}

\newif\ifPrintComment% print comment if true
\newcommand\SelectCommentsToPrint[1]{%
   \textcolor{blue}{Setting: #1!}% delete when using properly
   \foreach \key in {#1}{\pgfkeys{/SelectiveComment,set/\key} }
}
\newcommand\MakeSelection[1]{
  \PrintCommentfalse% comments off by default
  \foreach \key in {#1,all} {
    \pgfkeysifdefined{/SelectiveComment/\key}%
        {\pgfkeysgetvalue{/SelectiveComment/\key}{\temp}
         \ifnum\temp=1\global\PrintCommenttrue\fi% print if key=1
        }{}
  }
}

\newcommand\SelectiveComment[2][]{\MakeSelection{#1}\ifPrintComment#2\fi}
\NewEnviron{SelectiveEnvironment}[1][]{\MakeSelection{#1}\ifPrintComment\BODY\fi }

\begin{document}\obeylines% onlyto decrease space taken up by MWE
  \SelectCommentsToPrint{one, two, four}% print one, two or four
  \SelectiveComment[one]{This is one}\hfil--- printed as one set
  \SelectiveComment[two]{This is two}\hfil--- printed as two set
  \SelectiveComment[three]{This is three}\hfil? --- NOT printed as three not set
  \SelectiveComment[three, four]{This is three/four}\hfil--- printed as four set
  \SelectCommentsToPrint{unset/four}% stop printing four
  \SelectiveComment[four]{This is four}\hfil? --- NOT printed as four not set
  \SelectCommentsToPrint{four}% restart printing four
  \SelectiveComment[four]{This is four}\hfil--- printed as four set
  \SelectCommentsToPrint{all}% print everything
  \SelectiveComment[five]{This is five}\hfil--- printed as all set
  \SelectCommentsToPrint{unset/all}% revert to printing only selected comments
  \SelectiveComment[six]{This is six}\hfil? --- NOT printed as six not set
  \begin{SelectiveEnvironment}[one,four]
     Inside an environment with one/four
  \end{SelectiveEnvironment}\hfil--- printed as one and four set
  \begin{SelectiveEnvironment}[three,six]% will NOT  print environment
     Inside an environment with three/six
  \end{SelectiveEnvironment}\hfil? --- NOT printed as three and six not set
\end{document}

The command \SelectCommentsToPrint calls keys inside /SelectiveComment/set that, in turn, sets the key /SelectiveComment/<key> equal to 1. The unset commands calls /SelectiveComment/set/unset and this then sets /SelectiveComment/<key> equal to 0. I did it this way to stop the keys from printing their values when they are being set and unset. The actual printing is controlled by the switch \ifPrintComment. By default this is off and all the keys do is issue \PrintCommenttrue if one of the keys for the comment is set to 1.
Both the /SelectiveComment/set and /SelectiveComment/set/unset keys are controlled using the .unknown key handler. This allows arbitrary key names to be used. I use a hack of Martin Scharrer's to set all of the pgfkeys globally.
The \textcolor{blue}{Setting: #1!} line in the definition of \SelectCommentsToPrint is only there to make the output from the MWE easier to understand. It should be removed if you want to use this.

