Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
1  
One would thing of \provideenvironment, which apparently does not exist. However, 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
add comment

1 Answer

up vote 3 down vote accepted

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.

share|improve this answer
    
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
1  
@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
    
Yes. Thanks a lot. –  ASdeL Jan 18 '12 at 21:21
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.