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}
2
  • 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
    Commented Oct 3, 2014 at 8:07
  • \ifthenelse{\boolean{lecturer}}{true}{false}; but \begin{comment} cannot be used in the definition of an environment anyway.
    – egreg
    Commented Oct 3, 2014 at 8:45

2 Answers 2

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}
2
  • What if I want to customize the solution environment? For example setting the color to red? Commented Oct 19, 2018 at 14:57
  • 1
    @AlecJacobson --- take a look at section 3 (Special comments) in the comment package documentation. Commented Oct 20, 2018 at 8:58
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

2
  • Many thanks. I learned from all answers. David Commented Oct 4, 2014 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
    Commented Oct 4, 2014 at 18:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .