[Tex/LaTex] LaTeX error trying to close comment environment

commentserrors

I'm trying to create a document in which answers could be shown or not shown depending on the inclusion of \includecomment{answer}. I decided to use the comment package because the examples I found here seemed really simple, but it is stumping me nonetheless. I also need for individuals to be able to select which questions/answers are relevant for their projects.

I'm trying to use the answer environment, but it doesn't seem to work within an if/then. Here is a little bit of the sample that I think should run. I know that \end{answer} needs to be on its own line. There are no blank spaces before or after it. The code will compile if (a) I comment out the \begin and \end{answer}, or if (b) I comment out the if/then lines before and after the item. If I try to compile with both, I get the following error:

…Including 'answer' comment.)

Runaway argument?

! File ended while scanning use of \next.

< inserted text >

           \par

< * > example.tex

\documentclass{article}
\usepackage{ifthen}
\usepackage{comment}

\newcommand{\usetwo}{1} %Use Q2? 1 for yes, 2 for no. 
\includecomment{answer}

\begin{document}

\begin{enumerate}
  \item Question 1
  \ifthenelse{\equal{\usetwo}{1}}{
  \item Question 2\\
    \begin{answer}   
      Answer 2 goes here.
\end{answer}
  }%
  {}% if not set to 1
\end{enumerate}

\end{document}  

Best Answer

You cannot pass comment-like environments as arguments to other macros. Instead, use a \if construction like below:

\documentclass{article}

\usepackage{comment}

\newif\ifquestiontwo
\questiontwofalse% \questiontwotrue
\includecomment{answer}

\begin{document}

\begin{enumerate}
  \item Question 1

  \ifquestiontwo
  \item Question 2

\begin{answer}
  Answer 2 goes here.
\end{answer}

   \fi
\end{enumerate}

\end{document}

You can switch between using Question 2 or not via \questiontwotrue/\questiontwofalse.

Related Question