Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Consider the following two macro definitions:

\def\setmacro#1{\def\macro{#1}}

\def\setmacro{\def\macro}

Although the two variants of \setmacro have a different \meaning they are equivalent in what they eventually do (if used correctly). In my opinion, the former definition is easier to read while the latter is more flexible and, possibly, more efficient. So which one should be preferred?

share|improve this question
    
When such a decision is being made, it is worth thinking about using \let. It's not always possible to use \let in this case, but if it is, it might be the best compromise between efficiency and error checking (see Martin Scharrer's answer). –  Patrick Häcker Oct 7 '12 at 12:07
add comment

1 Answer

up vote 20 down vote accepted

The second form is indeed more efficient because the argument doesn't have to be read, inserted and read again (while the second read process is quicker), but only the first one is fully "stable".

Note that the second form would still allow a parameter text like \setmacro#1{foo #1 bar} which would expand to \def\macro#1{foo #1 bar}. In most cases you don't want that. Also it requires braces afterwards: \setmacro\foo would lead to \def\macro\foo which would read everything up to the next { as parameter text and then causes problems with error messages which are difficult to understand for an end-user. This case would work with the first definition.

If you define \setmacro as part of a class or package (like \title etc.) I would use the first version. The overhead is not meaningful on modern systems, especially if the set-macro is only used once or twice, but you are avoiding issues if the set-macro is used in a wrong or unexpected way.

share|improve this answer
    
Does your note about avoiding issues hold when \def\setmacro#{\def\macro}? –  morbusg Jun 19 '12 at 8:57
5  
@morbusg: This would still cause an error if a user uses \setmacro\foo. Now you get a better error message, but still it doesn't work like some LaTeX users would expect it. –  Martin Scharrer Jun 19 '12 at 9:00
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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