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.

How do I get LaTeX to run a command only if it exists? I need something like:

\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
% \newcommand{\mytitle}{Title Test}
\begin{document}
\if mytitle exists
\mytitle %true
\else
do not run the command
\fi
\end{document}

To prevent the error message appears: 'Undefined control sequence \mytitle' I need to decide this because I will use Boolean operation on multiple files.

share|improve this question
add comment

4 Answers

up vote 26 down vote accepted

I usually use \ifdefined:

\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
%\newcommand{\mytitle}{ducks}
\begin{document}

\ifdefined\mytitle
Yay, \mytitle!
\else
Oops.
\fi
\end{document}

As Martin Scharrer mentioned in the comment, \ifdefined is part of the e-TeX extension.

share|improve this answer
4  
Note that \ifdefined is part of the e-TeX extension, which is part of any modern LaTeX compiler. –  Martin Scharrer Sep 16 '11 at 14:18
    
Thanks @Martin, I'll add that to my answer. =) –  Paulo Cereda Sep 16 '11 at 14:20
1  
Thank you Paulo Cereda. Andrey Vihrov i dont undestand you. –  Regis da Silva Sep 16 '11 at 14:24
2  
@Regis: Please add all comments to the appropriate answers, i.e. add you comment to Andrey below his answer. –  Martin Scharrer Sep 16 '11 at 14:45
add comment

There is the kernel command \@ifundefined:

\makeatletter
\@ifundefined{mytitle}{do not run the command}{\mytitle}
\makeatother

Otherwise, as already mentioned

\ifdefined\mytitle
  \mytitle
\else
  do not run the command
\fi

The two will give different result in case

\let\mytitle\relax

has been given before the test, because \@ifundefined tests whether the control sequence name given as argument is equivalent to \relax. There are TeXnical reasons for this.

share|improve this answer
2  
Very useful explanation of the difference between \ifdefined and (negative of) \@ifundefined! –  Mico Sep 16 '11 at 18:41
add comment

An alternative way is to use \providecommand to define a command that does nothing. This will only define the command if it is not already defined. If it is already defined, the original definition is not altered. This eliminates the need to test it every time you want to use it.

\documentclass{article}
\begin{document}

\providecommand{\mytitle}{}%

This does nothing: \mytitle
\end{document}

If the command is expected to take a parameter you can do something like:

\providecommand{\mytitle}[1]{#1}%

which will just return the parameter, and hence do nothing with it.

share|improve this answer
    
Thank you all enormously. –  Regis da Silva Sep 16 '11 at 15:37
add comment

You can use \csname … \endcsname for that. \csname … \endcsname constructs a control sequence with the given name. If the control sequence does not exist, it is temporarily let equal to \relax, which does nothing.

So,

\csname mytitle\endcsname

will work both when \mytitle is and is not defined.

If your command takes parameters, this simple method can't be used anymore. Then you can use the \@ifundefined command:

\makeatletter
\@ifundefined{mytitle}{not defined}{defined}
\makeatother
share|improve this answer
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.