5
  1. How can I view the catcode of a character given by its ASCII number?

  2. Is there a way to list all the characters (represented by their ASCII numbers) that are associated with a given catcode? For instance, how to list all the end-of-line characters, i.e. all the characters with catcode 5?

10
  1. You can use \showthe\catcode32 and TeX will stop at that point and show you the category code of a space (10). You can also use \the\catcode32 which expands to the category code. Note that in both cases, I included a space after the number. The space causes TeX to stop reading the number.

  2. You could iterate through all 256 characters and output the ones that match the category code you want.

Here's some code that will typeset all of the current category codes.

\documentclass{article}
\newcount\charcount
\begin{document}
\charcount=0
\loop\ifnum\charcount<256
    Character \number\charcount \space has category code
    \number\catcode\charcount .\endgraf
    \advance\charcount by 1
\repeat
\end{document}
  • 2
    @EvanAad TeX90 is 8-bit: this is also true for pdfTeX (which is the common engine is use today). If you are using LuaTeX or XeTeX then the full Unicode range is available. (u)pTeX (for CJK use) is a bit of an oddity ... – Joseph Wright Jun 20 '17 at 16:56
  • 1
    That's true of TeX, eTeX, and pdfTeX. I'm really not sure what XeTeX or LuaTeX do. I've almost never used them. – TH. Jun 20 '17 at 16:57
  • 1
    @EvanAad Yes (to both questions). – ShreevatsaR Jun 20 '17 at 17:00
  • 1
    @EvanAad Of course: they are TeX derivatives, and in XeTeX's case back-compatible other than where Unicode compatibility requires a change. LuaTeX isn't back-compatible but is still 'TeX like', certainly to the point of being able to set \catcode. See load-unicode-data.tex for how catcodes are set for both XeTeX and LuaTeX for the entire Unicode range, starting from the source Unicode data files. – Joseph Wright Jun 20 '17 at 17:00
  • 2
    @EvanAad Unchanged bar the need for a rather longer loop for part (2) ... – Joseph Wright Jun 20 '17 at 17:02
7

An additional note on the XeTeX/LuaTeX side of things: unlike in the TeX/pdfTeX engines where a "character" can have a value from 0 to 255 (inclusive), in both XeTeX and LuaTeX a character can have a value from 0 to 1114111 (inclusive). This latter number (10FFFF in hexadecimal) is the number of code points in Unicode (although only about a tenth of them have actually been assigned meanings so far).

If you'd like to list only all characters with category code 5 (say), you can do that with a loop as suggested by TH.:

\documentclass{article}
\begin{document}
\newcount\charcount
\charcount=0
\loop\ifnum\charcount<1114112 % Change to 256 if not using XeTeX/LuaTeX
  \ifnum\catcode\charcount=5
    Character \number\charcount \ has category code \number\catcode\charcount .

  \fi
  \advance\charcount by 1
\repeat
\end{document}

(By default the only end-of-line character is ASCII 13 (aka U+000D CARRIAGE RETURN (CR) in Unicode), even in XeTeX/LuaTeX.)

With LuaTeX, one additionally has the option of doing all this looping and checking in Lua code instead of TeX macros:

\documentclass{article}
\begin{document}
\directlua{
  for i = 0, 1114111 do
    if tex.getcatcode(i) == 5 then
      tex.print("Character " .. i .. " has category code " .. tex.getcatcode(i) .. ".")
    end
  end
}
\end{document}

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.