2

How can I check if a macro is already defined? I want to make a command \dd. However, if the physics package is loaded I need to do \renewcommand and if it is not loaded I need to do \newcommand. So I want some sort of way to check if the macro \dd is defined or not.

Here is some pseudocode which showes what I want:

if <\dd> is defined:
    \renewcommand ....
else:
     \newcommand ...

Here is a MWE:

\documentclass{article}

\usepackage{physics} % may or may not be loaded

% Definition of \dd
% \newcommand*{\dd}{\mathop{}\!{\operator@font d}}
% or
% \renewcommand*{\dd}{\mathop{}\!{\operator@font d}}

\begin{document}

Hello $\dd f$

\end{document}
2
  • 3
    \providecommand{\dd}{}\renewcommand{\dd}{<what you want>}
    – egreg
    Commented Nov 13, 2022 at 13:24
  • 2
    if you want to define \dd as \mathop{}\!{\operator@font d} in both cases, you don't have to check whether it's defined, you can just use \def\dd{\mathop{}\!{\operator@font d}} Commented Nov 13, 2022 at 15:20

5 Answers 5

4

To answer the question in the first sentence, "How can I check if a macro is already defined?":

\ifdefined\dd
    %% it is already defined
\else
    %% it is not yet defined
\fi

I actually use this a lot, e.g., as a boolean switch (which I can switch on with \def\myswitch{} and switch off with \let\myswitch\undefined).

5

The simplest way is to do

\def\dd{<whatever you like>}
4

The simplest way is to do

\providecommand{\dd}{}% defines \dd if not already defined
\renewcommand{\dd}{<whatever you like>}

If it's for your personal use, fine.

Don't do it in a package code. A user loading your package might want to use \dd from physics (I wouldn't, but people seem to like packages that promise much more than they can actually offer).

I don't like physics: it's badly written and has really weird syntax.

2
  • Also, I found that it was defined somewhere else, using \DeclareRobustCommand. I guess this also works.
    – Vebjorn
    Commented Nov 13, 2022 at 19:11
  • @Vebjorn Be careful, because \DeclareRobustCommand doeno check. In this case, no problem, becayiu do want to redefine anyway.
    – egreg
    Commented Nov 13, 2022 at 20:09
2

I found that the command \ifcsdef from etoolbox works like this. It can check whether a macro is defined or not, and executes some code. Here is how it works:

\ifcsdef{<macro>}
    {<true code>}
    {<false code>}

If the macro is defined, <true code> is executed, and if not, <false code> is executed. Here is the MWE:

\documentclass{article}

\RequirePackage{etoolbox} 
\usepackage{physics} % may or may not be loaded

\makeatletter
\ifcsdef{dd}
    {\renewcommand*{\dd}{\mathop{}\!{\operator@font d}}}
    {\newcommand*{\dd}{\mathop{}\!{\operator@font d}}}
\makeatother

\begin{document}

Hello $\dd f$

\end{document}
3
  • 1
    there is not a lot of point using \renewcommand here as its only function is to warn of redefinition which you are subverting anyway. \let\dd\relax\newcommand*{\dd}{\mathop{}\!{\operator@font d}} is simpler and does not require any test or duplicated code Commented Nov 13, 2022 at 12:04
  • 3
    \ifcsdef is available as \@ifundefined in core latex with no package needed (just true and false branches reversed) Commented Nov 13, 2022 at 12:07
  • If you really want to do a test, how about \ifcsdef{dd}{\renewcommand}{\newcommand}*{\dd}{\mathop{}\!{\operator@font d}} Commented Nov 13, 2022 at 14:18
1

In this particular case, I found this solution:

\makeatletter
\DeclareRobustCommand{\dd}{\relax\ifmmode\ddaux\relax\fi}
\newcommand*{\ddaux}{\mathop{}\!{\operator@font d}}
\makeatother

Which 1) overwrites \dd from the physics package if it is imported, and 2) ensures that the command only works in mathmode.

You must log in to answer this question.

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