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
Prochaine révision Les deux révisions suivantes
2_programmation:macros:definir_un_caracteres_comme_une_macro [2018/06/03 16:16]
samcarter
2_programmation:macros:definir_un_caracteres_comme_une_macro [2018/12/02 23:40]
jejust
Ligne 1: Ligne 1:
---- +====== ​Defining characters as macros ​====== 
-title: ​Defining characters as macros +
-category: programming +
-tags: macros +
-permalink: /​FAQ-activechars +
-date: 2014-06-10 +
----+
  
 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"​. ​ 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
-```+</​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 + 
-\def_{\_+<​code ​latex> 
-```+\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).  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,​
Ligne 28: Ligne 25:
 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''​,  
-`zis no longer interpreted as a letter; the space +''​z'' ​is no longer interpreted as a letter; the space 
-is therefore not necessary&​nbsp;&​mdash;​ `\defzwould 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/​shortvrbpackage with its `\MakeShortVerbcommand.+[[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 `\verbcommand 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
-"[`\verbin command arguments](FAQ-verbwithin)"). +"[[FAQ-verbwithin|''​\verb'' ​in command arguments]]"). 
-An exemplary case is the [`doc`](https://​ctan.org/​pkg/​docpackage, which processes .dtx +An exemplary case is the [[ctanpkg>​doc|doc]] package, which processes .dtx 
-files using the [`shortvrb`](https://​ctan.org/​pkg/​shortvrbpackage to define +files using the [[ctanpkg>​shortvrb|shortvrb]] package to define 
-`|`&​hellip;​`|as a shorthand for +''​|''​...''​|'' ​as a shorthand for 
-`\verb|`&​hellip;​`|`. But `|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
-`dtxfiles 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
-<!-- {% 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 86:
 } }
 \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 (`~`), 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 %} --> + 
-```latex+ 
 +<​code ​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 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 `\globalcommand.+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 
-<!-- {% 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,​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
CC Attribution-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0