I want to define a macro if it is not already defined. I can do this inline as the example below shows. But I would like to have a macro to do this functionality for me. My attempt to make this work is DefineMacroIfUndefined
, but not sure how to adjust it to make it work.
I have commented out the call to DefineMacroIfUndefined
as this does not compile otherwise.
\documentclass{article}
\newcommand{\CheckIfCommandDefined}{
\ifdefined\MyNewCommand
MyNewCommand is defined.\par%
\else
MyNewCommand is not defined.\par%
\fi
}
% Define macro to the given value if not already defined
\newcommand{\DefineMacroIfUndefined}[2]{
% #1 is name of macro
% #2 is value to be assigned if this macros is not already defined.
\ifdefined\csname#1\endcsname
% Do nothing since it is already defined
\else
\newcommand{\csname#1\endcsname}{#2}
\fi
}
\begin{document}
% This works great.
\ifdefined\MyNewCommand
% Do nothing since it is already defined
\else
\newcommand{\MyNewCommand}{true}
\fi
\CheckIfCommandDefined
\let\MyNewCommand\undefined % Undefine \MyNewCommand
\CheckIfCommandDefined
% But want to be able to do the above thru a macro
%\DefineMacroIfUndefined{MyNewCommand}{true}
\CheckIfCommandDefined
\end{document}