# What is the difference between \empty and \@empty?

I want to elaborate a command that would print differently simple differential dx and multidimensional differential d^3x. For this purpose I invented the command \dif with one optional argument. It is defined as follows:

\newcommand{\dif}[1][]{%
\def\@tmp@a{#1}%
\ifx\@tmp@a\@empty
[b1]\mathrm{d}
\else
[b2]\mathrm{d}^{#1}{} \!
\fi
}


Here [b1] and [b2] are added for debugging. Branch [b1] is intended for the case when the command is called without any argument, e.g. \dif{x}. Second branch [b2] should be taken in case where, e.g., the command \dif[3]{x} is used to print d^3x. The definition, shown above, seems to work fine. However, if \@empty is substituted with \empty first branch is always ignored.

The command \empty is explained in The TeXBook, so that initially I tried it for this code and only occasionally reverted to \@empty having remembered a post at this site where it was said that the \end command of Plain TeX is redefined to \@@end in LaTeX.

Can somebody explain what is the difference between \empty and \@empty in LaTeX and, perhaps, suggest a better solution for my problem?

-

The \@empty and \empty macros are defined in the source file for the LaTeX format latex.ltx on line 122 and 441, respectively:

\def\@empty{}

\let\empty\@empty


So both are actually identical. I as well wondered about having two names for the same thing around. I guess the LaTeX developers wanted a macro which is safer for being redefined by the user or other packages.

You code actually should work with both \@empty and \empty. Put a \show\@empty and \show\empty before your \ifx to debug the issue. Maybe one gets redefined somewhere. In that case you could place a \tracingall\tracingassigns=1\relax \tracingonline=0\relax very early of your document preamble and then search for the redefinition in the log file.

-
I guess that \empty remains for historic reasons; probably at the time lplain.tex was written it was called \empty as in plain.tex and then \@empty was added for uniformity. Actually \empty is useful for avoiding \makeatletter and \makeatother sometimes. –  egreg Sep 18 '11 at 9:44
Guilty is polyglossia pkg with option [babelshorthands] and \setdefaultlanguage{russian}. It seems to redefine \empty since without polyglossia both \empty and \@empty variants take the branch [b2] wich is not what I want. –  Igor Kotelnikov Sep 18 '11 at 10:35
\empty to my knowledge is a plain TeX leftover. –  Yiannis Lazarides Sep 18 '11 at 15:21
@YiannisLazarides: There is \space so why not have \empty? I mean, why having \@empty at all? LaTeX doesn't define a \@space as well. –  Martin Scharrer Sep 18 '11 at 15:23
@Martin I think the initial idea and there is a note in the LaTeX source to this effect was to remove all the plain definitions, but then LaTeX got frozen. Possibly there might have bee concerns of breaking older code. –  Yiannis Lazarides Sep 18 '11 at 15:34

I get the same output either with \@empty or with \empty. I suggest you another definition:

\newcommand{\dif}[1][]{\mathop{}\!\mathrm{d}
\if\relax\detokenize{#1}\relax
\else
^{#1}\mkern-1.5mu
\fi}


This has two advantages over yours:

1. It takes care automatically of the thin space before the d
2. It uses a slight smaller backspace in case there's an exponent to the d.

Of course I'd prefer an italic d, but that's another story. :)

-
but d is not a variable ... –  Herbert Sep 18 '11 at 10:01
@Herbert Should we start a religion war? :) –  egreg Sep 18 '11 at 10:07
The ISO standard is also an upright 'd' (as of course it should be: I'm a scientist, not a pure mathematician :-) –  Joseph Wright Sep 18 '11 at 10:38
@Yiannis Whether adding \ensuremath or not is another potential topic for a religion war. Remaining to the main topic: I know that in physics and engineering there is the need of clear standards about the symbology. This is by no means necessary in pure mathematics, where change of alphabets is done on different bases than in applied sciences. To talk mathematics: the d in dx is definitely not an operator, as the two letters form an inscindible unit. –  egreg Sep 18 '11 at 15:40
@LoverofStructure ...the term preceded Church by 130 years or so:) –  Yiannis Lazarides Feb 18 '13 at 16:16

For the purpose of testing whether the argument of a command is empty I would recommend using the \@ifmtarg switch provided by the ifmtarg package. Thus:

\documentclass{article}

\usepackage{ifmtarg}

\makeatletter

\newcommand*{\dif}[1][]{%
\@ifmtarg{#1}{[b1]\mathrm{d}}{[b2]\mathrm{d}^{#1}{} \!}%
}

\makeatother

...


The \@ifmtarg switch relies on neither \empty nor \@empty.

-
I would prefer to have code not bounded to a 3rd-party package. –  Igor Kotelnikov Sep 18 '11 at 15:54
@Igor: Note that \@ifmtarg is quite a short macro. So you could easily define it yourself if you don't like to load external packages. –  mhp Sep 18 '11 at 21:10