Sign up ×
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'm trying to compile a LaTeX code with an older version of some package. Thus, I'm having a few issues due to undefined commands.

How can I add conditional code so that I can define some work-around commands of this older package version?

% I want something like this:
if command \foobar is not defined

% I don't need an "else" clause here, but it would be good
% to know how to add one, in case I need it in future

end if
share|improve this question
Doesn't \newcommand contains a built-in check that the command you are trying to define is not already defined? – Willie Wong Aug 27 '10 at 18:08
@Willie Wong: But then it complains horribly that it's already defined. I guess the point here is to silently fail if the command is already defined. – Loop Space Aug 27 '10 at 18:28
Ah, it seems I misunderstood the question. – Willie Wong Aug 27 '10 at 21:01

4 Answers 4

up vote 30 down vote accepted

The LaTeX kernel command \providecommand{\foobar}{FooBar} does exactly what you want.

share|improve this answer

I think that \providecommand is what you want for this, as lockstep says, but it's worth mentioning the \@ifundefined LaTeX command which tests for whether or not a command is defined and executes some code if it is or isn't. It's more flexible than \providecommand in that it doesn't just deal with defining a particular command. Here's a simple example that I use when anglicising a few commands:


So if the command \centre is not defined then it defines the environment centre.

share|improve this answer
This should be promoted. This is a nice way to define environments as well as commands conditionally which I was about to ask about separately. – Huck Bennett Oct 21 at 21:45

The ifthen package provides some easy-to-use macros for this. For example:

  %% Do this if it is undefined
  %% Do this if it is defined

You can also do it in plain TeX (which doesn't require any external packages):

  %% Do this if it is undefined
  %% Do this if it is defined
share|improve this answer

Having just figured out how to do this at the TeX level, the TeX primitives that LaTeX wraps (apparently), are \ifdefined and \ifcsname. Use like this:

\ifdefined \foobar \else

Use \ifcsname if you need to construct the command name with macros:

\ifcsname foobar\endcsname \else

The \ifdefined and \ifcsname control sequences do not exist in the original tex program, but they do in all latex/xetex/pdftex variants on my Ubuntu system.

share|improve this answer
These commands are no TeX primitives, but defined in e-tex. In newer tex distributions, latex usually links to pdftex, which includes the e-tex extensions already. If you run your code snippets through plain tex you'll see, that they won't work, running them through etex works. Good answer nonetheless. – Elmar Zander Nov 30 '11 at 13:18
There seems to be conflicting statements here. The first sentence says \ifdefined and \ifcsname are TeX primitives, and the last sentence says otherwise. – SimplyKnownAsG Mar 19 '14 at 18:21

Your Answer


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.