2

I want to define an environment to hide text, with warning about it before and after in the pdf-output. What is wrong with the following definition?

\newenvironment{foldit}
{NB! NB! FOLDED TEXT BELOW\\ NB! NB! FOLDED TEXT BELOW\\ \iffalse}
{\fi ~~\\ NB! NB! FOLDEDTEXTABOVE \\ NB! NB! FOLDEDTEXTABOVE}

When I try to enclose some texts with \begin{foldit} .... \end{foldit}, I get the error message: Incomplete \iffalse; all text was ignored after line 105.

2

2 Answers 2

5

Consider using environ to define foldit. It handles the environment definition in a similar way you'd expect from \newcommand, with the argument being \BODY - the body of the environment (between \begin{<env>}...\end{<env>}).

enter image description here

\documentclass{article}

\usepackage{environ,lipsum}

\NewEnviron{foldit}{%
  \par\noindent NB! NB! FOLDED TEXT BELOW
  \par\noindent NB! NB! FOLDED TEXT BELOW
  %\BODY
  \par\noindent NB! NB! FOLDED TEXT ABOVE
  \par\noindent NB! NB! FOLDED TEXT ABOVE
}
\begin{document}

1: \lipsum[1]

\begin{foldit}
2: \lipsum[1]
\end{foldit}

3: \lipsum[3]

\end{document}
4
  • There seems to be a bug: If I try this over several sections, it crashes. Aug 27, 2021 at 18:00
  • @Sapiens: Are you nesting them? Ps. I should have used \BODY, not \Body.
    – Werner
    Aug 27, 2021 at 19:00
  • I solved the problem now. Aug 27, 2021 at 19:11
  • I added colors for the messages to stand out. Aug 28, 2021 at 3:51
5

You might want to try package environ or comment:

\documentclass{article}
\usepackage{parskip}
\usepackage{comment}
\usepackage{environ}
\NewEnviron{foldit}{%
NB! NB! FOLDED TEXT BELOW\\
NB! NB! FOLDED TEXT BELOW\\
%\BODY
NB! NB! FOLDEDTEXTABOVE\\
NB! NB! FOLDEDTEXTABOVE\\
}

\NewEnviron{unfoldit}{%
NB! NB! FOLDED TEXT BELOW\\
NB! NB! FOLDED TEXT BELOW\\
\BODY\\
NB! NB! FOLDEDTEXTABOVE\\
NB! NB! FOLDEDTEXTABOVE\\
}

\begin{document}
Line~1.

\begin{comment}
Line~2.
\end{comment}

Line~3.

\begin{foldit}
Line~4.
\end{foldit}

\begin{unfoldit}
Line~5.
\end{unfoldit}
\end{document}

enter image description here

5
  • No, I want the output: Line 1. NB! NB! FOLDED TEXT BELOW NB! NB! FOLDED TEXT BELOW NB! NB! FOLDED TEXT ABOVE NB! NB! FOLDED TEXT ABOVE Line 3. NB! NB! FOLDED TEXT BELOW NB! NB! FOLDED TEXT BELOW NB! NB! FOLDED TEXT ABOVE NB! NB! FOLDED TEXT ABOVE Line 4. Aug 27, 2021 at 16:25
  • I tried using \begin{comment} and \end{comment} instead of \iffalse and \fi in the definition of the new environment. The definition was accepted, but the text was not commented out. Aug 27, 2021 at 16:36
  • 1
    @Sapiens, just updated my answer. Please see if it works for you.
    – citsahcots
    Aug 27, 2021 at 16:39
  • Thanks. This time Werner beat you to it with a minute! :) Aug 27, 2021 at 16:46
  • @Sapiens, cool!!
    – citsahcots
    Aug 27, 2021 at 16:48

You must log in to answer this question.

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