5

I am not sure which test to use. This seems to work:

\ifcsmacro{myenvironment}{}{
    \newenvironment{myenvironment}{Not seen so far.}{End.}
}

Is that ok, or should I use another test? Moreover, should I not use etoolbox? I am using etoolbox for other tasks, so using it here seems natural.

ASdeL
  • 4,925
  • 1
    One would thing of \provideenvironment, which apparently does not exist. However, http://tex.stackexchange.com/a/20691/3751 presents a pure-Latex implementation of it. – Daniel Jan 18 '12 at 15:52
  • You should probably also test for \endmyenvironment as \end{myenv} does \endmyenv (after some sanity testing) – kahen Jan 18 '12 at 16:54
  • @Daniel: Thanks for the link to \provideenvironment. I think that could be ok, but I was looking for a simple high level test using etoolbox, since I have that package already loaded. If \provideenvironment existed in plain LaTeX, I surely would use it. – ASdeL Jan 18 '12 at 19:54

1 Answers1

4

This is sufficient, since LaTeX defines commands \<env> and \end<env> when you execute \newenvironment{<env>}. However, just to be on the safe side, you may want to remove any previous definitions of \end<env> that may have been defined using \def\end<env>{...} for whatever reason:

\ifcsmacro{myenvironment}{}{
  \let\endmyenvironment\undefined%
  \newenvironment{myenvironment}{Not seen so far.}{End.}
}

Here, \undefined is an undefined macro. It may just as well have been anything that is undefined, like \thismacrodoesnotexist. Of course, if there is concern that \end<env> might be important, then you should test for that as well before defining <env>, possibly by nesting it within the <false> condition of \ifcsmacro.

Werner
  • 603,163
  • I do not know how the environment was defined, if it was actually defined, so it could be better to take the safe way. However, I am not sure to understand that part of your answer. Are you trying to ensure that the definition of \end<end> will not fail when \newenvironment is executed? – ASdeL Jan 18 '12 at 20:01
  • 2
    @ASdeL: If someone defined a macro \end<env> and you only test for the existence of \<env> then performing \newenvironment{<env>} would cause an error, since it defines both \<env> and \end<env> (the latter already existing). So, adding \let\end<env>\undefined removes any former definition assigned to \end<env> so that \newenvironment{<env>} does not fail. Does this help? – Werner Jan 18 '12 at 20:07