3

I want to define the environment ignorethis such that

\begin{ignorethis}
This will not be shown and will  not take any space
\end{ignorethis}

Defining command that ignores the argument is simple:

\newcommand{\comments}[1]{}

Now, how can I define ignorethis based on comments? Something like this, but do not know how to escape insert } and `} properly.

\newenvironment{ignorethis}{\comments{}{}}

P.S. I know about comment package but I do not want to use it as it requires fragile frames in the beamer.

MWE

\documentclass{beamer}
\newcommand{\comments}[1]{}
\newenvironment{ignorethis}{\comments{}{}}

\begin{document}
  \begin{frame}
    \comments{This text will be for sure invisible} 
    \begin{ignorethis}
      How can I ignore this? 
    \end{ignorethis}    
  \end{frame}
\end{document}

2 Answers2

4

The previous answers mentioned the environ package. I can confirm it works for safe text. I doubt it would work for pathological examples (catcode changes, broken definitions, unmatched braces), perhaps especially with how beamer also collects everything and evaluates it. In any case, here's the MWE

\documentclass{beamer}
\usepackage{environ}
\NewEnviron{ignore}{}{}
\begin{document} 
\begin{frame}
This is a test.
\begin{ignore}
blah blah blah.

blah blah blah.
\end{ignore}
It's over.
\end{frame}
\end{document}
1

I will also post an answer which I created based on the suggestions on using environ (I don't use etoolbox but ifthen package).

The solution is slightly elaborated:

  • For locally changing the modes: Optional [h] decides whether to hide or show the ignorable parts.
  • For globally hiding everything: Defining \hideallignorables hides all ignorable environments
  • For globally showing everything: Defining \showallignorables shows all ignorable environments even if the have [h] or \hideallignorables is defined.

My intention is to use the environment to ignore complex TikZPictures to decrease the compilation time.

\documentclass{beamer}
\usepackage{environ}
\usepackage{ifthen}

\newcommand{\hideallignorables}{Hides all ignorables; It has priority over classes}
%\newcommand{\showallignorables}{Shows all ignorables; It has priority over classes and hideallignorables}

\NewEnviron{ignorable}[1][]{% #2 is a mandatory class name; #1 can be 'h' for hiding
\ifthenelse{\isundefined{\showallignorables}}
    {\ifthenelse{\isundefined{\hideallignorables} \AND \NOT\equal{#1}{h}}
        {\BODY}
        {}%
    }
    {\BODY}%
}

\begin{document}

\begin{frame}

NOSPACE AFTER%
\begin{ignorable}
First text
\end{ignorable}%
NOSPACE BEFORE

\begin{ignorable}
Visible if hideallignorables is not defined
\end{ignorable}

\begin{ignorable}[h]
This is hidden unless showallignorables is defined 
\end{ignorable}

\end{frame}
\end{document}