5

I know that it is not recommended to use macros like \H0 since it contains non-letters characters.

However, what about the macro names like \2 with only one figure, which are allowed without trouble by LaTeX. Is it a bad practice ?

For example, it is tempting to let \2 be a shortcut for one half, that is :

\newcommand{\2}{\frac{1}{2}}

6
  • 3
    It's not “bad practice” per se; but it would be difficult to remember what these commands mean, I think. – egreg Sep 28 '13 at 21:27
  • One reason it's not good practice -- though it's maybe not sufficiently disastrous to qualify as outright bad practice -- is that you'll get hopelessly confused by your own code if you start doing stuff like \newcommand\1{2}... – Mico Sep 28 '13 at 21:31
  • 8
    whether or not this is a bad practice is a matter of personal opinion. in plain tex, these were left "open" for ad hoc use by authors, and i know of no public latex package that defines them for anything but "temporary" use. (but i could be wrong about that; there are many packages i haven't explored.) i would recommend against creating a package that assigns a one-digit control sequence a "permanent" definition; instead, leave them available for the end user. – barbara beeton Sep 28 '13 at 21:37
  • Am i missing something, or why does nobody complain about the construction \newcommand{\2}{\frac{1}{2}}? AFAIUnderstood the TeX macro processor is not able to read number characters in macro names, assuming they have their default catcode. It should be something like \expandafter\def\csname 2\endcsname{\frac{1}{2}}. – Ruben Sep 28 '13 at 22:15
  • 4
    @Ruben You can't mix letters and numbers in a macro name; but macro names consisting of a unique non letter are legal. – egreg Sep 28 '13 at 22:21
4

whether or not defining control sequences with a single digit is a bad practice is a matter of personal opinion.

in plain tex, these were left "open" for ad hoc use by authors, and i know of no public latex package that defines them for anything but "temporary" use. (but i could be wrong about that; there are many packages i haven't explored.)

i would recommend against creating a package that assigns a one-digit control sequence a "permanent" definition; instead, leave them available for the end user.

3

Not an answer, but here is a tip, given by A.J. Hildebrand, on using one letter macros:

Avoid one letter macros

Many one letter sequences have predefined meanings and thus cause conflicts if you try to use them for your own macros. Most of these one-letter macros are part of the TeX core, and usually not documented in Latex books. Thus, if a macro isn't listed in the index of a Latex book, it doesn't mean that it is not in use. To be certain that a macro isn't in use, you would have to check the original Knuth TeXbook, in addition to a comprehensive Latex reference.

You can largely avoid running into such problems by using at least two letters for a macro name. While there exist a few (non-obvious) two letter sequences that have predefined meanings (e.g., \aa), those are quite rare and the vast majority of two letter sequences are available as macro names. For example, to define \mathbf{v}, use \vv, or \bv (for "bold v"), rather than \v. The latter, \v, is predefined, whereas both \vv and \bv are available.

His website offers many more other useful tips for LaTeX, one can go over them if interested.

3
  • 1
    This advice seems mainly wrong I'm afraid. If you use \newcommand to define the macro and it does not give an error then you know it is not over-writing a tex primitive. – David Carlisle Sep 29 '13 at 0:05
  • @DavidCarlisle: Not if, for some reason, one's forced to use \def. – Francis Sep 29 '13 at 1:48
  • 1
    Since that is claiming to be advice about LaTeX, the advice should be not to use \def for any document commands to avoid over-writing stuff. It is also irrelevant to the question as while plain tex does have some one letter commands, it does not have any that are a single digit, and there are no tex primitives named with a single character, so there should be no need to look in the texbook to discover such commands. – David Carlisle Sep 29 '13 at 9:15

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.