# Différences

Ci-dessous, les différences entre deux révisions de la page.

 2_programmation:macros:definir_un_caracteres_comme_une_macro [2018/05/25 21:31]joseph.wright 2_programmation:macros:definir_un_caracteres_comme_une_macro [2018/12/02 23:40]jejust Les deux révisions précédentes Révision précédente 2021/10/24 23:05 jejust Mise à jour des liens internes. Mots-clefs OpenGraph.2018/12/04 00:25 jejust 2018/12/02 23:40 jejust 2018/06/03 16:21 joseph.wright 2018/06/03 16:16 samcarter 2018/06/03 15:33 d.p.carlisle 2018/06/03 15:31 samcarter 2018/06/03 15:16 samcarter 2018/06/03 15:15 joseph.wright 2018/06/03 15:15 d.p.carlisle 2018/06/03 15:11 samcarter 2018/06/03 15:03 joseph.wright 2018/06/03 15:02 samcarter 2018/06/03 14:57 joseph.wright 2018/06/03 11:58 d.p.carlisle 2018/06/02 17:34 joseph.wright 2018/06/02 16:44 d.p.carlisle 2018/05/31 15:28 d.p.carlisle 2018/05/28 21:29 d.p.carlisle 2018/05/25 23:17 joseph.wright 2018/05/25 21:31 joseph.wright 2018/05/25 10:57 joseph.wright 2018/05/25 10:55 joseph.wright 2018/05/25 00:02 joseph.wright 2018/05/25 00:02 joseph.wright 2018/05/24 19:06 joseph.wright 2018/05/24 18:57 joseph.wright 2018/05/24 10:38 joseph.wright 2018/05/24 10:38 joseph.wright 2018/05/24 10:29 joseph.wright 2018/05/23 23:25 joseph.wright 2018/05/23 23:19 joseph.wright 2018/05/23 22:25 joseph.wright 2018/05/23 18:46 joseph.wright 2018/05/23 15:55 d.p.carlisle 2018/05/22 21:39 joseph.wright 2018/05/22 13:40 joseph.wright 2018/05/22 13:34 joseph.wright créée Prochaine révision Révision précédente 2021/10/24 23:05 jejust Mise à jour des liens internes. Mots-clefs OpenGraph.2018/12/04 00:25 jejust 2018/12/02 23:40 jejust 2018/06/03 16:21 joseph.wright 2018/06/03 16:16 samcarter 2018/06/03 15:33 d.p.carlisle 2018/06/03 15:31 samcarter 2018/06/03 15:16 samcarter 2018/06/03 15:15 joseph.wright 2018/06/03 15:15 d.p.carlisle 2018/06/03 15:11 samcarter 2018/06/03 15:03 joseph.wright 2018/06/03 15:02 samcarter 2018/06/03 14:57 joseph.wright 2018/06/03 11:58 d.p.carlisle 2018/06/02 17:34 joseph.wright 2018/06/02 16:44 d.p.carlisle 2018/05/31 15:28 d.p.carlisle 2018/05/28 21:29 d.p.carlisle 2018/05/25 23:17 joseph.wright 2018/05/25 21:31 joseph.wright 2018/05/25 10:57 joseph.wright 2018/05/25 10:55 joseph.wright 2018/05/25 00:02 joseph.wright 2018/05/25 00:02 joseph.wright 2018/05/24 19:06 joseph.wright 2018/05/24 18:57 joseph.wright 2018/05/24 10:38 joseph.wright 2018/05/24 10:38 joseph.wright 2018/05/24 10:29 joseph.wright 2018/05/23 23:25 joseph.wright 2018/05/23 23:19 joseph.wright 2018/05/23 22:25 joseph.wright 2018/05/23 18:46 joseph.wright 2018/05/23 15:55 d.p.carlisle 2018/05/22 21:39 joseph.wright 2018/05/22 13:40 joseph.wright 2018/05/22 13:34 joseph.wright créée Prochaine révision Les deux révisions suivantes Ligne 1: Ligne 1: - --- + ====== Defining characters as macros ​====== - category: programming + - tags: macros + - permalink: /​FAQ-activechars + - date: 2014-06-10 + - --- + - # Defining characters as macros Single characters can act as macros (defined commands), and both Single characters can act as macros (defined commands), and both Plain TeX and LaTeX define the character Plain TeX and LaTeX define the character - &#​x7e;​ as a ''​non-breakable space''​.  A + ''​~''​ as a "non-breakable space".  A - character is made definable, or ''​active''​, by setting its + character is made definable, or "active", by setting its - _category code_ (catcode) to be \active (13): + //category code// ​(catcode) to be ''​\active'' ​(13): - latex + + <​code ​latex> \catcode\_=\active \catcode\_=\active -  + ​ Any character could, in principle, be activated this way and defined Any character could, in principle, be activated this way and defined as a macro: as a macro: - latex + - \def_{\_} + <​code ​latex> -  + \def//{\//} + ​ which could be characterised as an over-simple answer to which could be characterised as an over-simple answer to - [using underscores](/​FAQ-underscore).  However, you must be + [[FAQ-underscore|using underscores]].  However, you must be wary: whereas people expect an active tilde, other active characters wary: whereas people expect an active tilde, other active characters may be unexpected and interact badly with other macros. ​ Furthermore,​ may be unexpected and interact badly with other macros. ​ Furthermore,​ by defining an active character, you preclude the character'​s use for by defining an active character, you preclude the character'​s use for - other purposes, and there are few characters ​''​free'' ​to be subverted + other purposes, and there are few characters ​"free" ​to be subverted in this way. in this way. - To define the character ​z as a command, one would say something + To define the character ​''​z'' ​as a command, one would say something like: like: - latex + + <​code ​latex> \catcode\z=\active \catcode\z=\active \def z{Yawn, I'm tired}% \def z{Yawn, I'm tired}% -  + ​ - and each subsequent ​z in the text would become a + and each subsequent ​''​z'' ​in the text would become a yawn. This would be an astoundingly bad idea for most documents, but yawn. This would be an astoundingly bad idea for most documents, but might have special applications. ​ (Note that, in might have special applications. ​ (Note that, in - \def  z, + ''​\def  z''​, - z is no longer interpreted as a letter; the space + ''​z'' ​is no longer interpreted as a letter; the space - is therefore not necessary&​nbsp;&​mdash;​ \defz would do; we choose to + is therefore not necessary ​--- ''​\defz'' ​would do; we choose to retain the space, for what little clarity we can manage.) retain the space, for what little clarity we can manage.) Some LaTeX packages facilitate such definitions. For example, the Some LaTeX packages facilitate such definitions. For example, the - [shortvrb](https://​ctan.org/​pkg/​shortvrb) package with its \MakeShortVerb command. + [[ctanpkg>​shortvrb|shortvrb]] package with its ''​\MakeShortVerb'' ​command. TeX uses category codes to interpret characters as they are read TeX uses category codes to interpret characters as they are read from the input. from the input. - _Changing ​a catcode value will not affect characters that have already been read_. + //​Changing ​a catcode value will not affect characters that have already been read//. Therefore, it is best if characters have fixed category codes for the Therefore, it is best if characters have fixed category codes for the duration of a document. ​ If catcodes are changed for particular duration of a document. ​ If catcodes are changed for particular - purposes (the \verb command does this), then the altered + purposes (the ''​\verb'' ​command does this), then the altered characters will not be interpreted properly when they  appear in the characters will not be interpreted properly when they  appear in the argument to another command (as, for example, in argument to another command (as, for example, in - ''​[\verb in command arguments](/​FAQ-verbwithin)''​). + "​[[FAQ-verbwithin|''​\verb'' ​in command arguments]]"). - An exemplary case is the [doc](https://​ctan.org/​pkg/​doc) package, which processes .dtx + An exemplary case is the [[ctanpkg>​doc|doc]] package, which processes .dtx - files using the [shortvrb](https://​ctan.org/​pkg/​shortvrb) package to define + files using the [[ctanpkg>​shortvrb|shortvrb]] package to define - &#​x7c;&​hellip;&#​x7c;​ ​as a shorthand for + ''​|''​...''​|'' ​as a shorthand for - \verb&#​x7c;&​hellip;&#​x7c;​. But &#​x7c;​ ​is + ''​\verb|''​...''​|''​. But ''​|'' ​is also used in the preambles of tabular environments,​ so that tables in also used in the preambles of tabular environments,​ so that tables in - dtx files can only have vertical line separation between + ''​dtx'' ​files can only have vertical line separation between columns by employing special measures of some sort. columns by employing special measures of some sort. Another consequence is that catcode assignments made Another consequence is that catcode assignments made in macros often don't work as expected ​ in macros often don't work as expected ​ - (see ''​[Active characters in command arguments](/​FAQ-actinarg)''​). + (see "[[FAQ-actinarg|Active characters in command arguments]]"). For example, the definition For example, the definition - + - latex + + <​code ​latex> \def\mistake{% \def\mistake{% \catcode_=\active \catcode_=\active \def_{\textunderscore\-}% \def_{\textunderscore\-}% } } -  + - + - does not work because it attempts to define an ordinary ​_ character: + does not work because it attempts to define an ordinary ​''​_'' ​character: When the macro is used, the category change does not apply to the When the macro is used, the category change does not apply to the underscore character already in the macro definition. ​ Instead, one may underscore character already in the macro definition. ​ Instead, one may use: use: - + - latex + + <​code ​latex> \begingroup \begingroup \catcode_=\active \catcode_=\active Ligne 87: Ligne 86: } } \endgroup \endgroup -  + - + - The alternative (''​tricksy''​) way of creating such an isolated + The alternative ("tricksy") way of creating such an isolated - definition depends on the curious properties of \lowercase, which + definition depends on the curious properties of ''​\lowercase''​, which changes characters without altering their catcodes. ​ Since there is changes characters without altering their catcodes. ​ Since there is - always ​_one_ active character (&#​x7e;​), we + always ​//​one// ​active character (''​~''​), we - can fool \lowercase into patching up a definition without ever + can fool ''​\lowercase'' ​into patching up a definition without ever explicitly changing a catcode: explicitly changing a catcode: - + - latex + + <​code ​latex> \begingroup \begingroup - \lccode\~=\_ + \lccode''​\~=''​\_ \lowercase{\endgroup \lowercase{\endgroup \def~{\textunderscore\-}% \def~{\textunderscore\-}% }% }% -  + - + The two definitions have the same overall effect (the character is The two definitions have the same overall effect (the character is defined as a command, but the character does not remain active), defined as a command, but the character does not remain active), - except that the first defines a \global command. + except that the first defines a ''​\global'' ​command. For active characters to be used only in maths mode, it is much better For active characters to be used only in maths mode, it is much better to leave the character having its ordinary catcode, but assign it a to leave the character having its ordinary catcode, but assign it a - special active ​_maths code_, as with + special active ​//maths code//, as with - + - latex + + <​code ​latex> \begingroup \begingroup - \lccode~=x + \lccode''​~=''​x \lowercase{\endgroup \lowercase{\endgroup \def~{\times}% \def~{\times}% }% }% \mathcodex="​8000 \mathcodex="​8000 -  + - + The special character does not need to be redefined whenever it is The special character does not need to be redefined whenever it is - made active&​nbsp;&​mdash; ​the definition of the command persists even if the + made active ​--- the definition of the command persists even if the character'​s catcode reverts to its original value; the definition character'​s catcode reverts to its original value; the definition becomes accessible again if the character once again becomes active. becomes accessible again if the character once again becomes active. + + --- + + //Source:// [[faquk>​FAQ-activechars|Defining characters as macros]] + + {{htmlmetatags>​metatag-keywords=(LaTeX,​programming) + metatag-og:​title=(Defining characters as macros) + metatag-og:​site_name=(FAQ LaTeX francophone) + }}
2_programmation/macros/definir_un_caracteres_comme_une_macro.txt · Dernière modification: 2021/10/24 23:05 par jejust