4

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 3 '14 at 8:07
  • \ifthenelse{\boolean{lecturer}}{true}{false}; but \begin{comment} cannot be used in the definition of an environment anyway. – egreg Oct 3 '14 at 8:45
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}
  • What if I want to customize the solution environment? For example setting the color to red? – Alec Jacobson Oct 19 '18 at 14:57
  • 1
    @AlecJacobson --- take a look at section 3 (Special comments) in the comment package documentation. – Ian Thompson Oct 20 '18 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

  • Many thanks. I learned from all answers. David – David Reynolds Oct 4 '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 4 '14 at 18:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.