12

Background: I use LaTeX to display data that are automatically produced. I cannot manually change the raw data (since they are frequently updated).

But this raw text contains LaTeX's special characters (_, &, and %). This makes compilation fail.

I have found in Handling of special LaTeX characters in text how to tell LaTeX to treat _ and & as "regular" characters. However, \catcode`%=12 does not work...


Question: How to tell LaTeX that the symbol % should be considered as a "regular" character?

Of course, defining this behavior globally would make compilation of many packages fail, so this redefinition should be applicable locally/temporarily (e.g. in an \inputed .tex file).

  • 1
    \catcode`\%=12 should do. – Steven B. Segletes Nov 28 '16 at 18:36
  • 1
    To make it local, enclose it and the affected code between \bgroup / \egroup delimiters. – Steven B. Segletes Nov 28 '16 at 18:38
16

You can make % punctuation character by

\catcode`\%=12
13

In ConTeXt you can use ASCII mode by saying \asciimode. This will disable the special meaning of #$%&^_|~ for the whole document from that point. You can still have comments with double percent signs.

\asciimode
\starttext

#$%&^_|~  %% these are disabled

\ { }     %% these remain enabled

\stoptext

If you only want to enable it for a short piece of text, use \start...\stopasciimode.

\starttext

\startasciimode
  #$%&^_|~  %% these are disabled

  \ { }     %% these remain enabled
\stopasciimode

\stoptext
5

A category code assignment is of the form

\catcode <number> = <number>

(spaces and = are optional). The first <number> is the character we want to assign the category code (so eight bit in legacy engines, up to 0x10FFFF for Unicode engines), the second one is the category code (four bit number).

The number can be expressed in any legal form understood by TeX: decimal, octal, hexadecimal or alphabetic. For instance, the usual category code 4 to & can be assigned by

\catcode 38 = 4    % decimal
\catcode '46 = 4   % octal
\catcode "26 = 4   % hexadecimal
\catcode `& = 4    % alphabetic

In order to change the category code of %, which is preassigned to 14 when INITEX starts off, so embedded in every format that doesn't change the assignment, we have a problem if we want to use the “alphabetic” representation:

\catcode `% = 12

cannot work, because the % character has not yet been assigned a different category code, so it is still a comment character when the code is being read in. You can do it with the explicit number

\catcode 37 = 12

because % is ASCII 37 (hexadecimal 0x25). But it would be much handier to use the character! There is an escape to this cul-de-sac: an alphabetic constant can also be designed by

`\<character>

where the backslash must be a character of category code 0 (as usual). In this case the one character control sequence is not interpreted as a macro, but just as the character it is named with. Hence

\catcode `\% = 12

will work. Similarly, if you want to temporarily change the category code of the backslash to 12 you can do

\catcode `\\ = 12

(the assignment will be done after having input the control sequence \\ in order to interpret it as an alphabetic constant because of the backquote).

Other characters that must be denoted this way are those with category code 5 (end line), 9 (ignored) or 15 (invalid). Also category code 6 characters are better expressed this way, particularly when the category code assignment is in the replacement text of a macro.

Thus, when you have to change category code of a character (possibly) having category code 0 (escape), 5 (end line), 6 (parameter), 9 (ignored), 14 (comment) or 15 (invalid), use either the decimal form or the escaped form.

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.