15

This question already has an answer here:

I am a LaTeX guy, slowly working into the miracles of TeX.

Can anybody tell me the differences between \def, \edef, \gdef and \xdef?

Where shall I use which, what are the pros and cons? Or is it suspected to be unclean code, if I mix TeX commands in LaTeX?

Please give a short example, of how to use the command.

marked as duplicate by Henri Menke, Stefan Pinnow, user36296, gernot, Jan Feb 10 '17 at 15:47

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 3
    I can recommend 'Notes on Programming in TeX' written by Christian Feuersänger, which gives a lot of insights on this in a short and easy-to-read text. – pschulz Feb 10 '17 at 12:18
  • 1
    @HenriMenke thank you for your comment, that is a phantastic Q&A. But it does not cover \gdef and \xdef. – Jan Feb 10 '17 at 12:25
  • 1
    Yes, exactly! Sorry for not providing the link, i'm just so used to using texdoc, so it's a simple texdoc programming for me. – pschulz Feb 10 '17 at 12:29
  • 3
    @Jan \gdef (\xdef) is merely a shorthand for \global\def (\global\edef), so the question about these can be reduced to \def and \edef. – Henri Menke Feb 10 '17 at 12:50
  • 1
    @HenriMenke: Thank you again for that information. Therefore, you are right, my question is a duplicate to your given Link. – Jan Feb 10 '17 at 15:47
17

There are no pros and cons: \def and \edef perform different tasks. With

\def<cs><parameter text>{<replacement text>}

you define <cs> to look for its arguments (if any) and to be replaced by <replacement text>, which is not interpreted in any way at definition time. With

\edef<cs><parameter text>{<replacement text>}

the replacement text is fully expanded at definition time.

For instance, if we have

\def\aaa{aaa}
\def\bbb{x\aaa}
\edef\ccc{y\aaa}
\def\aaa{AAA}

a call like

\bbb \ccc

would produce

xAAAyaaa

because the replacement text of \ccc is what remains after full expansion, so \edef\ccc{y\aaa} is the same as \def\ccc{yaaa}.

Note that the expansion in \edef is done at definition time, so parameter tokens like #1 and so on will be untouched.

A less silly example: if you want that \thissection expands to the value of the section counter at the time the command is defined, you have to say

\edef\thissection{\thesection}

because this “freezes” the value by doing the expansion at definition time. To the contrary, with \def\thissection{\thesection} the macro \thissection would print the current section number.

LaTeX has the variant \protected@edef that avoids some quirks with “robust macros”, so something like \protected@edef\cs{\textbf{a}} works whereas \edef\cs{\textbf{a}} wouldn't (there's plenty of examples on the site).

About \gdef and \xdef there's not much to say: the former is completely equivalent to \global\def and the latter to \global\edef (assuming primitive meaning of \global, of course). LaTeX has \protected@xdef.

  • Thank you @egreg. This is again a very informative and helpful answer. I still have to fiddle about \global. :-) – Jan Feb 10 '17 at 12:36
  • 1
    @Jan -- \global simply means that the definition persists when the current group closes, instead of the pre-group definition being restored. – barbara beeton Feb 10 '17 at 15:01
  • @barbarabeeton thank you. I added an answer to the original question, as I did a search with all four \?defs and did not find an answer. Now it should be found in either way. I am personally glad, I finally learned about that TeX stuff. Next target: what is \long good for? – Jan Feb 11 '17 at 11:22
  • 1
    @Jan -- a separate question about the meaning of \long could be useful; i couldn't find any, although it's not easy to look for. i did find this: Is there a simple way to retroactively add the \long prefix to a macro's definition?, but it presupposes that someone already knows the meaning. actually, the meaning is simple: a \long macro can include a paragraph break, whereas a macro that is not \long will throw an error if it encounters \par or a blank line. – barbara beeton Feb 11 '17 at 16:30

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