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
  • Doesn't \newcommand contains a built-in check that the command you are trying to define is not already defined? Commented Aug 27, 2010 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. Commented Aug 27, 2010 at 18:28
  • Ah, it seems I misunderstood the question. Commented Aug 27, 2010 at 21:01

4 Answers 4


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


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.

  • 1
    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. Commented Oct 21, 2015 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
  • How do you define foobar? Like, \def\foobar would work?
    – hola
    Commented May 30, 2022 at 10:18

The e-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.

  • 3
    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. Commented Nov 30, 2011 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. Commented Mar 19, 2014 at 18:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .