0

I am writing some lecture notes, and I would like to have automatically generated two versions. One version includes everything (examples, additional explanations, etc.), and another one with only the essential information (definitions, theorems, etc.). My current approach is/was to utilize the ifthenelse package as follows:

\newenvironment{optional}
  {
    \ifthenelse{\boolean{opt}}{
  }
  {
    }{}
  }

so that I could use the environment

\begin{optional}
    some optional information.
\end{optional}

Setting the boolean accordingly, I would generate each version of the lecture notes.

The problem is, of course, that there is a mismatch of braces in my \newenvironment statement that causes trouble with parsing. How can I solve that?

D.F.F
  • 414

2 Answers2

0

Use \bgroup in place of the mismatched { and \egroup in place of the mismatched }

Of course, I’m forgetting that \ifthenelse does some weird stuff to hide the primitive syntax around conditionals.

I think the simplest approach (and one that I’m more willing to type now that I’m not on my phone), is to use \NewDocumentEnvironment with the +b¹ argument for the body. Then you would declare your environment as:

\NewDocumentEnvironment{optional}{+b}
  {\ifthenelse{\boolean{opt}}{#1}{}}
  {} % ❶

You’ll notice that everything here is defined in the \begin definition and the \end ❶ is empty. This is because b tells LaTeX to treat the whole body of the environment as an argument so it will be represented by the #1² in the definition,

The problem with this is that you can’t have verbatim in your environment, but \ifthenelse already precluded that.


  1. The + is necessary to allow multi-paragraph contents to optional.
  2. If you have other arguments to the environment that you are defining, b needs to be the last argument you define and, of course, the argument number will be different.
Don Hosek
  • 14,078
  • Thanks! Unfortunately, it does not seem to work out of the box. If the boolean is true, then it compiles normally but the first character of the optional text disappears. If the boolean is false, then it does not compile with the error: "Extra }, or forgotten \endgroup. \end{optional}" – D.F.F Oct 25 '23 at 16:19
  • Yeah, that’s what I get for typing an answer while on my phone. the \bgroup\egroup trick will work for most commands, but alas, because of how TeX's parsing works combined with environment definitions, my original suggestion doesn’t work. I’ve put in a new version above. – Don Hosek Oct 25 '23 at 16:38
0

It looks like the comment package might work for you:

\usepackage{comment}
\ifthenelse{\boolean{opt}}
 {\includecomment{optional}}
 {\excludecomment{optional}}
Teepeemm
  • 6,708