Différences
Ci-dessous, les différences entre deux révisions de la page.
— |
2_programmation:macros:caracteres_non_alphabetiques_dans_les_noms_de_macros [2022/04/29 10:42] (Version actuelle) yannick.tanguy Début de la traduction. |
||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
+ | ====== Comment mettre des caractères autres que des lettres dans les noms de commande ? ====== | ||
+ | Les utilisateurs débutants avec < | ||
+ | <code latex> | ||
+ | \newcommand{\a2main}{À demain !} | ||
+ | </ | ||
+ | |||
+ | En effet, contrairement à d' | ||
+ | |||
+ | ===== Utilisation de “\csname” et “\endcsname” ===== | ||
+ | |||
+ | Voici un exemple de la méthode utilisant les commandes '' | ||
+ | |||
+ | < | ||
+ | \expandafter\newcommand\csname a2main\endcsname{À demain !} | ||
+ | Je vous dis « \csname a2main\endcsname ». | ||
+ | </ | ||
+ | |||
+ | Cette technique a l' | ||
+ | |||
+ | FIXME // | ||
+ | |||
+ | 2. Define a " | ||
+ | |||
+ | <code latex> | ||
+ | \newcommand{\DefineRemark}[2]{% | ||
+ | \expandafter\newcommand\csname rmk-# | ||
+ | } | ||
+ | \newcommand{\Remark}[1]{\csname rmk-# | ||
+ | ... | ||
+ | \DefineRemark{cul8r}{Goodbye!} | ||
+ | ... | ||
+ | \Remark{cul8r} | ||
+ | </ | ||
+ | |||
+ | * **Pro:** Straightforward to use, not too untidy | ||
+ | * **Con:** It's hardly doing what we set out to do (experts will see that you are defining a macro, but others likely won't) | ||
+ | |||
+ | |||
+ | 3. Convince TeX that '' | ||
+ | |||
+ | <code latex> | ||
+ | \catcode`8 = 11 | ||
+ | \newcommand{\cul8r}{Goodbye!} | ||
+ | I said, ``\cul8r'' | ||
+ | </ | ||
+ | |||
+ | * **Pro:** '' | ||
+ | * **Con:** Likely to break other uses of '' | ||
+ | |||
+ | < | ||
+ | ! Missing number, treated as zero. | ||
+ | <to be read again> | ||
+ | 8 | ||
+ | </ | ||
+ | |||
+ | |||
+ | As a general rule, changing category codes is something to use | ||
+ | //in extremis//, after detailed examination of options. | ||
+ | conceivable that such drastic action could be useful for you, but | ||
+ | most ordinary users are well advised not even to try such a technique. | ||
+ | |||
+ | 4. Define a macro '' | ||
+ | |||
+ | < | ||
+ | \def\cul8r{Goodbye!} | ||
+ | I said, ``\cul8r'' | ||
+ | </ | ||
+ | |||
+ | * **Pro:** '' | ||
+ | * **Con #1:** Breaks if '' | ||
+ | |||
+ | < | ||
+ | ! Use of \cul doesn' | ||
+ | <*> \cul9 | ||
+ | 9 | ||
+ | </ | ||
+ | (which would confuse someone who hadn't even realised there //was// a definition of '' | ||
+ | |||
+ | * **Con #2:** Silently redefines existing '' | ||
+ | |||
+ | |||
+ | Technique 3 is in fact commonly used --- in a limited form --- within | ||
+ | most LaTeX packages and within LaTeX itself. | ||
+ | use '' | ||
+ | from the user and thereby prevent naming conflicts. | ||
+ | LaTeX automatically treats '' | ||
+ | processing classes and packages and as a non-letter while processing | ||
+ | the user's document. | ||
+ | internally a non-letter is used for macro names, and the user doesn' | ||
+ | see anything of it, while the status remains " | ||
+ | definitions created within the class or package. | ||
+ | [[FAQ-atsigns|\@ and @ in macro names]] for | ||
+ | more information. | ||
+ | |||
+ | Note that analogous use of technique 3 in this example would give us | ||
+ | |||
+ | <code latex> | ||
+ | \begingroup | ||
+ | \catcode`8 = 11 | ||
+ | \gdef\cul8r{Goodbye!} | ||
+ | \gdef\later{\cul8r} | ||
+ | \endgroup | ||
+ | I said, ``\later'' | ||
+ | </ | ||
+ | which works, but rather defeats the object of the exercise. | ||
+ | ('' | ||
+ | has reverted to normal by the time it's used; note, also, the use of | ||
+ | the primitive command '' | ||
+ | macro that's available outside the group.) | ||
+ | |||
+ | // | ||
+ | '' | ||
+ | that contains only ordinary letters. | ||
+ | roman numerals in place of arabic ones: | ||
+ | |||
+ | <code latex> | ||
+ | \newcommand{\culVIIIr}{Goodbye!} | ||
+ | </ | ||
+ | which rather spoils the intent of the joke implicit in the example '' | ||
+ | |||
+ | ----- | ||
+ | //Source:// [[faquk> | ||
+ | |||
+ | {{htmlmetatags> | ||
+ | metatag-og: | ||
+ | metatag-og: | ||
+ | }} |