Right after \NeedsTeXFormat{LaTeX2e} in v1.0i of the xcolor package, there are many fun lines of category codes. Why are these here? What are they doing?

Snippet from texmf-dist/tex/latex/xcolor/xcolor.sty

Version 2016, v.1.0i

\expandafter\edef\csname XC@catcodes\endcsname
 {\catcode33 \the\catcode33 % !
  \catcode34 \the\catcode34 % "
  \catcode39 \the\catcode39 % '
  \catcode42 \the\catcode42 % *
  \catcode44 \the\catcode44 % ,
  \catcode45 \the\catcode45 % -
  \catcode46 \the\catcode46 % .
  \catcode47 \the\catcode47 % /
  \catcode58 \the\catcode58 % :
  \catcode59 \the\catcode59 % ;
  \catcode60 \the\catcode60 % <
  \catcode61 \the\catcode61 % =
  \catcode62 \the\catcode62 % >
  \catcode63 \the\catcode63 % ?
  \catcode64 \the\catcode64 % @
  \catcode94 \the\catcode94 % ^
  \catcode96 \the\catcode96 % `
\catcode64 11 %
\@makeother\! \@makeother\" \@makeother\' \@makeother\* \@makeother\,
\@makeother\- \@makeother\. \@makeother\/ \@makeother\: \@makeother\;
\@makeother\< \@makeother\= \@makeother\> \@makeother\? \@makeother\`
\catcode94 7 %
\catcode`\^^A=14 %

Later also

\catcode`\!=13 \catcode`\:=13 \catcode`\-=13 \catcode`\+=13
\catcode`\;=13 \catcode`\/=13 \catcode`\"=13 \catcode`\>=13
  \ifnum\catcode`\!=13 \edef!{\string!}\fi
  \ifnum\catcode`\:=13 \edef:{\string:}\fi
  \ifnum\catcode`\-=13 \edef-{\string-}\fi
  \ifnum\catcode`\+=13 \edef+{\string+}\fi
  \ifnum\catcode`\;=13 \edef;{\string;}\fi
  \ifnum\catcode`\"=13 \edef"{\string"}\fi
  \ifnum\catcode`\>=13 \edef>{\string>}\fi
  \ifnum\catcode`\/=13 \edef/{\string/}\fi
  \ifnum\catcode`\:=13 \edef:{\string:}\fi

The \XC@catcodes macro is an array used to save some category codes at the point of execution of the macro, in order to be able to restore them.

So, under standard settings, the replacement text will begin with

\catcode33 12

because the standard category code of ! (ASCII 33) is 12. However, if babel-french is in force, we'd get \catcode33 13.

The last line is worthy a mention. The doc package, used to print documentation contained in dtx files makes ^^A (ASCII 1) into a comment character, so the more complicated


is used. However it would be much easier to do \catcode 1 \the\catcode1 like for the previous entries.

Actually, as you can see, the package does change the category codes in order to have character tokens with the desired category in replacement text of macros.

What does \XC@edef do? It is the package's version of \edef, with some twists.

Remember babel-french mentioned above? Well ! is used in the syntax of color specifications. So \color{red!50} must work independently of the category code of !.

This is solved by doing \XC@edef on the argument; the macro opens a group and redefines the active ! to produce \string! so, when the \edef is eventually performed, the replacement text will contain a ! with category code 12 to be passed on for parsing the color specification.

  • Just out of curiosity, why does babel-french set ! to category code 13? – Jonathan Komar Oct 20 '16 at 17:10
  • @macmadness86 Because it wants that ! acts by removing a possible space before it, adding a fixed space and an exclamation mark, according to the typographic rules of French. – egreg Oct 20 '16 at 17:14

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.