Différences

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

Lien vers cette vue comparative

Les deux révisions précédentes Révision précédente
Prochaine révision
Révision précédente
2_programmation:macros:definir_un_caracteres_comme_une_macro [2018/05/24 10:38]
joseph.wright
2_programmation:macros:definir_un_caracteres_comme_une_macro [2021/10/24 23:05] (Version actuelle)
jejust Mise à jour des liens internes. Mots-clefs OpenGraph.
Ligne 1: Ligne 1:
---- +====== Defining characters as macros ​======
-section: Macro programming +
-subsection: ''​Generic'' ​macros ​and techniques +
-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 ''​~''​ as a "non-breakable space"
-`&#​x7e;​` as a ''​non-breakable space''​ A +character is made definable, or "active", by setting its 
-character is made definable, or ''​active''​, by setting its +//​[[2_programmation:​syntaxe:​catcodes:​start|category code]]// ​(catcode) to be ''​\active'' ​(13): 
-_category code_ (catcode) to be `\active(13): +<​code ​latex>
-```latex+
 \catcode`\_=\active \catcode`\_=\active
-```+</​code>​ 
 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 +<​code ​latex> 
-\def_{\_+\def//{\//
-```+</​code>​
 which could be characterised as an over-simple answer to  which could be characterised as an over-simple answer to 
-[using underscores](FAQ-underscore.md).  ​However, you must be +[[3_composition:​texte:​symboles:​caracteres:​tiret|using underscores]]. 
-wary: whereas people expect an active tilde, other active characters +However, you must be wary: whereas people expect an active tilde, other 
-may be unexpected and interact badly with other macros. ​ Furthermore,​ +active characters may be unexpected and interact badly with other macros. 
-by defining an active character, you preclude the character'​s use for +Furthermore,​ by defining an active character, you preclude the character'​s use 
-other purposes, and there are few characters ​''​free'' ​to be subverted+for other purposes, and there are few characters ​"free" ​to be subverted
 in this way. in this way.
  
-To define the character ​`zas 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}%
-``` +</​code>​ 
-and each subsequent ​`zin 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 
-`zis no longer interpreted as a letter; the space +is therefore not necessary ​--- ''​\defz'' ​would do; we choose to
-is therefore not necessary&​nbsp;&​mdash;​ `\defzwould 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 `\MakeShortVerbcommand.+[[ctanpkg>​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. 
-duration of a document. ​ If catcodes are changed for particular +If catcodes are changed for particular purposes (the ''​\verb'' ​command does this), then 
-purposes (the `\verbcommand does this), then the altered +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
-''​[`\verbin command arguments](FAQ-verbwithin.md)''​). +"[[3_composition:​texte:​paragraphes:​pourquoi_verbatim_ne_fonctionne_pas_toujours|“\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]package, which processes ​''​.dtx''​ 
-files using the [`shortvrb`](https://​ctan.org/​pkg/​shortvrb) ​package to define +files using the [[ctanpkg>​shortvrb]package to define ​''​|''​...''​|'' ​as a shorthand 
-`&#​x7c;&​hellip;&#​x7c;​` ​as a shorthand ​for +for ''​\verb|''​...''​|''​. But ''​|'' ​is also used in the preambles of tabular environments,​ 
-`\verb``&#​x7c;&​hellip;&#​x7c;​`. But `&#​x7c;​` ​is +so that tables in ''​.dtx'' ​files can only have vertical line separation between
-also used in the preambles of tabular environments,​ so that tables in +
-`dtxfiles 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.md)''​).+(see "[[2_programmation:​macros:​caracteres_actifs_dans_les_arguments_d_une_macro|Active characters in command arguments]]").
 For example, the definition For example, the definition
-<!-- {% raw %} --> + 
-```latex+<​code ​latex>
 \def\mistake{% \def\mistake{%
 \catcode`_=\active \catcode`_=\active
 \def_{\textunderscore\-}% \def_{\textunderscore\-}%
 } }
-``` +</code> 
-<!-- {% endraw %} --+ 
-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:
-<!-- {% raw %} --> + 
-```latex+<​code ​latex>
 \begingroup \begingroup
 \catcode`_=\active \catcode`_=\active
Ligne 86: Ligne 77:
 } }
 \endgroup \endgroup
-``` +</code> 
-<!-- {% endraw %} --+ 
-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 `\lowercaseinto patching up a definition without ever+can fool ''​\lowercase'' ​into patching up a definition without ever
 explicitly changing a catcode: explicitly changing a catcode:
-<!-- {% raw %} --> +<code latex>
-```latex+
 \begingroup \begingroup
-  \lccode`\~=`\_+  \lccode''​\~=''​\_
   \lowercase{\endgroup   \lowercase{\endgroup
     \def~{\textunderscore\-}%     \def~{\textunderscore\-}%
   }%   }%
-``` +</code> 
-<!-- {% endraw %} --+ 
-The two definitions have the same overall effect (the character is +The two definitions have the same overall effect (the character is defined 
-defined as a command, but the character does not remain active), +as a command, but the character does not remain active), except that 
-except that the first defines a `\globalcommand.+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 
-<!-- {% raw %} --> + 
-```latex+<​code ​latex>
 \begingroup \begingroup
-  \lccode`~=`x+  \lccode''​~=''​x
   \lowercase{\endgroup   \lowercase{\endgroup
     \def~{\times}%     \def~{\times}%
   }%   }%
 \mathcode`x="​8000 \mathcode`x="​8000
-``` +</code> 
-<!-- {% endraw %} -->+
 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,​programmation en LaTeX,​définir des macros,​définir des commandes,​catcodes,​caractères actifs,​commande à un seul caractère,​macro à un seul caractère)
 +metatag-og:​title=(Defining characters as macros)
 +metatag-og:​site_name=(FAQ LaTeX francophone)
 +}}
  
2_programmation/macros/definir_un_caracteres_comme_une_macro.1527151099.txt.gz · Dernière modification: 2018/05/24 10:38 par joseph.wright
CC Attribution-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0