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'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
  \newcommand{\foobar}{FooBar}

% 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
add comment

4 Answers 4

up vote 28 down vote accepted

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

share|improve this answer
add comment

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:

\makeatletter
\@ifundefined{centre}{%
\newenvironment{centre}{\center}{\endcenter}%
}{}
\makeatother

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

share|improve this answer
add comment

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

\ifthenelse{\isundefined{\foobar}}{
  %% 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):

\ifx\foobar\undefined
  %% Do this if it is undefined
\else
  %% Do this if it is defined
\fi
share|improve this answer
add comment

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
  \newcommand{\foobar}{FooBar}
\fi

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

\ifcsname foobar\endcsname \else
  \newcommand{\foobar}{FooBar}
\fi

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
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. –  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 at 18: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.