5

I want to produce two versions of one document with different content. The idea is to comment out certain sections if a logical variable is false. This logical variable will be inputted eventually through an argument to a package. My difficulty is that my code either does not work or it ignores boolean variable. Here is sample code which does not work. Error is "missing number treated as zero"

\usepackage{ifthen,comment}
\newboolean{iflecturer}
\newenvironment{myitemize}
{\ifthenelse{iflecturer}
    {\begin{itemize}}{\begin{comment}}
}
{\ifthenelse{iflecturer}
    {\end{itemize}}{\end{comment}}
}
\begin{document}
\setboolean{iflecturer}{true}% \setboolean{iflecturer}{false}
\begin{myitemize}
\item Monday
\item Tuesday
\item Wednesday
\end{myitemize}
\end{document}
  • the problem is that the \begin{comment} begins a comment in the definition, it is not seen as then part of the if then. – Guido Oct 03 '14 at 08:07
  • \ifthenelse{\boolean{lecturer}}{true}{false}; but \begin{comment} cannot be used in the definition of an environment anyway. – egreg Oct 03 '14 at 08:45

2 Answers2

2

I think it's better to use the \excludecomment macro provided by the comment package. This way you don't need to start and end comment environments from inside macros or environments of your own.

\documentclass{article}
\usepackage{ifthen}
\usepackage{comment}
\specialcomment{solution}{\textbf{Solution}\quad}{}
\newboolean{iflecturer}
\setboolean{iflecturer}{false}

\ifthenelse{\boolean{iflecturer}}{}{\excludecomment{solution}}

\begin{document}
This is a question.

\begin{solution}
And this is the solution.
\end{solution}

\end{document}
Ian Thompson
  • 43,767
2

You can use the environ package instead of comment to discard the content of an environment.

\documentclass{article}

\usepackage{etoolbox}
\usepackage{environ}
\newtoggle{lecture}

\NewEnviron{myitemize}{%
  \iftoggle{lecture}
    {\begin{itemize}\BODY\end{itemize}}
    {}%
  }

\begin{document}
\toggletrue{lecture}
The environment is visible
\begin{myitemize}
\item test
\item test
\end{myitemize}
\togglefalse{lecture}
after this the environment is hidden.
\begin{myitemize}
\item test
\item test
\end{myitemize}
Test
\end{document}

This is what you get:

enter image description here

Guido
  • 30,740
  • Many thanks. I learned from all answers. David – David Reynolds Oct 04 '14 at 15:26
  • @DavidReynolds glad they helped. The best to thank is to upvote and to accept the answer it helped you the most. To ‘Accept’ an answer click on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). – Guido Oct 04 '14 at 18:32