How can I write transparent wrapper around xdef that, depending on package options, report error, override or obey previous definition without sacrificing all flexibility? I have no idea how to pass all this # in.

EDIT: Example what I can do and what I want to do:

%% This is not valid. xdef have different syntax.
%% \ifpackage@force\xdef\package@define\xdef\fi

So I can have generic interface for providecommand and newcommand. I want add checking in xdef.

  • What do you want to achieve? A package? – Marco Daniel May 30 '13 at 19:53
  • Yes. Package with options, controlling what to do with conflicts. – KAction May 30 '13 at 20:04
  • 1
    Maybe you should provide a minimal working example to demonstrate your issue. – Marco Daniel May 30 '13 at 20:17
  • Do you mean similar to \newcommand in the sense of checking for an existing definition, etc.? – Joseph Wright May 30 '13 at 20:23
  • 3
    you could define a command like \newcommand or \renewcommand but which used \xdef rather than \def internally, but it would be a bad idea to redefine \xdef to add checking capability, it would almost certainly break latex. – David Carlisle May 30 '13 at 20:28

something like this



  \PackageError{cxdef}{\string#1 already defined}{think of a new name}%








\myxdef\mbox#1#2\zz{this is bad}

\myxdef\zzzz#1#2\zz{this is good}



which produces

! Package cxdef Error: \mbox already defined.

See the cxdef package documentation for explanation.
Type  H <return>  for immediate help.

l.39 \myxdef\mbox
                 #1#2\zz{this is bad}
macro:#1#2\zz ->this is good

\xdef, \errorxdef and \preservexdef are available or you can use \myxdef which is \let to one of those three depending on a package option.

As egreg noted in a comment on the question, it would be better to use \protected@xdef rather than \xdef wherever the latter appears in the package code, but I'll leave as \xdef for now as that was the original question.

|improve this answer|||||

This answer tries to answer the question, if \xdef can be replaced by a macro to catch the command to be defined.

The syntax of \xdef is quite complicate, the relevant aspects:

  1. \xdef is unexpandable.
  2. The definition can be prefixed by \long, \outer, and e-TeX adds \protected.
  3. \afterassignment might insert a token right after the definition.

Vanilla TeX already fails issue 1. At least e-TeX provides an approximation, if the new macro is defined using \protected.

Issue 2 is tried by the following example for various formats with e-TeX (etex, pdflatex):

\catcode`\@=11 % same as \makeatletter
\def\msg#{\immediate\write16}% short for printing something on the console




    \ifx     \xdef@test\xdef@test@

  \expandafter\ifx     \csname xdef@test\expandafter\endcsname
                       \csname xdef@test@O\endcsname
  \else\expandafter\ifx\csname xdef@test\expandafter\endcsname
                       \csname xdef@test@LO\endcsname
  \else\expandafter\ifx\csname xdef@test\expandafter\endcsname
                       \csname xdef@test@PO\endcsname
  \else\expandafter\ifx\csname xdef@test\expandafter\endcsname
                       \csname xdef@test@PLO\endcsname

    \chardef\xdef@ok=1 %
    \chardef\xdef@ok=1 %
    \chardef\xdef@ok=0 %
    \errmessage{\string#1 is already defined}%

% testing

% end of job (LaTeX or plain TeX)
\csname @@end\endcsname\end

The prefixes can only be caught another definition, therefore the new \xdef starts with the definition of \xdef@test. Then the new definition is tested against a series of other macros to find the prefixes that was active. Prefix \outer complicates the testing because of its nature. There are restrictions, where \outer macros can be used. The found prefixes are stored in \xdef@prefix.

Then the new command token is tested, if it is undefined or has the meaning of \relax (LaTeX's interpretation of "undefined" in \@ifundefined). This is checked via \meaning. An error is thrown, if the new command is already defined and a dummy command \xdef@dummy is defined instead.

However this redefined \xdef is NOT a valid replacement for the original \xdef. It fails issue 3, the \afterassignment feature:

  • Expandable stuff between the prefixes and the final definition does not hurt. Thus we could test for the command to be defined. But throwing an error message (or printing a warning) is not expandable and TeX complains:

    ! You can't use a prefix with `\errmessage'.

    Therefore we need at least one test definition to catch the prefixes.

  • If \afterassignment is in use, then the token is inserted right after the first test definition, way to early:

    • The inserted token might want to use the new definition, that is not yet defined.
    • The definition business is not finished. The token might want to clean up some stuff and mess up things.

Thus I am pretty sure, that there is no way with TeX or e-TeX that can simulate all aspects of \xdef by a macro that catches the command to be defined to print warnings or to throw errors, if the command is already defined.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.