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.

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.

share|improve this question
2  
Good question! I think that anyone writing a generic newX macro ought to also provide both renewX and provideX variants. (I got so fed up of journals defining all different types of theorem variants - and therefore messing up my own definitions - that I wrote a \providetheorem command.) –  Loop Space Jun 14 '11 at 10:07
3  
It's worth pointing out that \providecommand isn't to avoid having to decide which to use, it's to provide a definition if one does not already exist. If it does, then \providecommand does not redefine it. –  TH. Jun 14 '11 at 10:37
1  
Yes, I think that's probably a better description of its intended usage. But because I often generate LaTeX automatically it's quite convenient for me to use \providecommand rather than keep track of whether my LaTeX code has already defined a command (hence the weird way I described its purpose). –  bryn Jun 19 '11 at 7:49

3 Answers 3

up vote 19 down vote accepted

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.

share|improve this answer
    
Like! Can you tell us/point us to what \@star@or@long is and does? It appears very mystical ... –  user1129682 Oct 29 '13 at 12:00
    
@user1129682: \@star@or@long is defined in the latex kernel (ltdefns.dtx): "Look ahead for a *. If present reset l@ngrel@x so that the next definition, #1, will be non-long". –  wasteofspace Nov 20 '13 at 13:12

No.

If you are familiar with expl3 (but not necessary), you can use \ProvideDocumentEnvironment in xparse package. The syntax is a little different. For example:

\usepackage{xparse}
\ProvideDocumentEnvironment{foo}{ o m }{%
  \IfNoValueTF{#1}{only #2}{#1 and #2}%
}{%
  \IfNoValueTF{#1}{only #2}{#1 and #2}%
}
share|improve this answer

There is a \provideenvironment in the package makecmds. I suppose that is what you are looking for. Its syntax is analog to that of \newenvironment.

share|improve this answer

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.