New Environment – Does \newenvironment Have a \provideenvironment Equivalent?

environmentsmacros

To avoid the problem of having to decide whether to use \newcommand or \renewcommand, LaTeX has the option of \providecommand.

Is there something corresponding to this for environments? I'd like to be able to type \provideenvironment (or similar) rather than first using \newenvironment and then using \renewenvironment.

Best Answer

I don't believe there is a \provideenvironment defined anywhere, but you can define it.

\documentclass{article}

\makeatletter
\def\provideenvironment{\@star@or@long\provide@environment}
\def\provide@environment#1{%
        \@ifundefined{#1}%
                {\def\reserved@a{\newenvironment{#1}}}%
                {\def\reserved@a{\renewenvironment{dummy@environ}}}%
        \reserved@a
}
\def\dummy@environ{}
\makeatother

\provideenvironment{foo}[1][blarg]{begin foo: #1 }{end foo}
\provideenvironment{foo}{asdf}{asdf}
\begin{document}
\begin{foo}
whee
\end{foo}
\end{document}

The first time foo gets defined. The second time it does not because it is already defined.

Related Question