# Allow only certain commands in own environment

I would like to define an environment myenv and allow only certain predefined commands to be executed betwwen \begin{myenv} and \end{myenv}.

For example I want text typed there to be ignored and also to give a list of commands which are allowed. Is there a way to achieve this?

Thanks.

• Could you add an example of what you mean? – egreg Oct 3 '13 at 15:50
• You can use \nullfont to ignore the text, but restricting other commands is harder as Matthew has said. It might help if we knew more about your application. For example, if \fred is allowed within myenv, do you want the user to be able to do \newcommand{\xyzzy}{\fred\fred} and use that? – cyberSingularity Oct 3 '13 at 20:41
• Unfortunately \nullfont is inserting an undesired extra line. I would like to have something similar to the itemize environmant. There every line between \begin{itemize} and \end{itemize} has to begin with \item. Otherwise you get an error. – user26756 Oct 4 '13 at 11:27

For ignoring text, you could use myenv as a special type of comment environment (from the package of the same name). I don't think there's a way to only allow a certain number of macros, however. You can disallow any macro by redefining it to give an error message.

\documentclass{article}
\usepackage{comment}
\usepackage{lipsum}

\newcommand{\foo}{bar}

\specialcomment{myenv}{
\newcommand{\nomacros}{%
\GenericError{}{%
Illegal macro%
}{}{
This macro is not allowed within the myenv' environment
}}%
\let\foo\nomacros
\let\bar\nomacros
}{}

\begin{document}

\lipsum[1]

\begin{myenv}
\lipsum[1]
\foo\bar
\end{myenv}

\end{document}


The console will give:

! Illegal macro.

Type  H <return>  for immediate help.
...

l.2 \foo
\bar
? h
This macro is not allowed within the myenv' environment
?

! Illegal macro.

Type  H <return>  for immediate help.
...

l.2 \foo\bar

? x
No pages of output.