Below are two examples. The first one works fine, no errors no warnings, but on trying to build a doc from the latter, I get a heap of errors.
Working:
\documentclass[]{article}
\usepackage{environ}
\newsavebox{\DiscardedText}
\newcommand{\Slide}[1]{}
\newcommand{\SavedSlideText}{}
\newcommand\AddToSavedSlideText[1]{\xdef\SavedSlideText{\SavedSlideText#1\endgraf}}
\NewEnviron{GobbleExceptForSlide}{%
\renewcommand{\Slide}[1]{\AddToSavedSlideText{##1}}%
\savebox{\DiscardedText}{\BODY}%
\SavedSlideText%
}
\AtBeginDocument{\GobbleExceptForSlide}
\AtEndDocument{\endGobbleExceptForSlide}
\begin{document}
Should not show in output
\Slide{Should show in output}
\end{document}
But if I put an itemize either below or into the Slide command, then it fails with Paragraph ended before \@savebox was complete \end{document}:
\Slide{
Should show in output
\begin{itemize}
\item Item one
\item Iten two
\end{itemize}
}
I'm tried replacing the 'newcommand' with 'newenvironment' and related changes, but I could not get that to work either. I'm a little rusty...
Update
The answer provided below does solve part of the problem, but for some reason it breaks when using an environment outside the Slide. The following tex won't compile, because there is an itemize environment outside of the Slide.
\documentclass{article}
\usepackage{environ}
\makeatletter
\newsavebox{\DiscardedText}
\newcommand{\Slide}[1]{}
\newcommand{\SavedSlideText}{}
\newcommand\AddToSavedSlideText[1]{\g@addto@macro\SavedSlideText{{#1}}}
\NewEnviron{GobbleExceptForSlide}{%
\renewcommand{\Slide}[1]{\AddToSavedSlideText{##1}}%
\savebox{\DiscardedText}{\BODY}%
\SavedSlideText%
}
\makeatother
\AtBeginDocument{\GobbleExceptForSlide}
\AtEndDocument{\endGobbleExceptForSlide}
\begin{document}
Should not show in output
\begin{itemize}
\item Item one
\item Iten two
\end{itemize}
\Slide{
Should show in output
\begin{itemize}
\item Item one
\item Iten two
\end{itemize}
}
\end{document}
\Slide
" really do? I doubt it's an empty command like in your MWE.\edef
or\xdef
on general tex input, almost all commands are unsafe in that context. If you want to incrementally add tokens to a macro use\g@addto@macro\SavedSlideText{#1\endgraf}}