[Tex/LaTex] Generating a catcode table in Latex (with \typeout to terminal)

catcodesdebuggingterminal-outputtex-core

I often need to look up a catcode table, and quick searches on the web are rarely satisfying.

So I thought, it must be (in the long term) easier to use a Latex function of sorts, to loop through all 8-bit sequences (that is, ASCII characters), and print out:

[ASCII code] - [ASCII character] - [catcode] - [catcode meaning]

A "reverse indexing" would be nice as well – where catcodes are output, and then which ASCII chars belong to them, e.g:

[catcode] - [catcode meaning]: 
    [ASCII code1] - [ASCII character1]
    [ASCII code2] - [ASCII character2]
    ....

Just wanted to note – I'd like such a table to be \typeouted to the terminal (not typeset in a PDF).

In #21397 – How to get the catcode of a token?, a \printcatcode exercise is mentioned, that answers the mapping from ASCII character to a verbose catcode meaning…

So I guess, one would just have to make a loop from 0 to 255 (maybe something similar to approaches in An expandable 'character scanning' command?), and apply all such relevant transformations – unfortunately, I'm not mastering tex core on a level where I can come up with that code off the top of my head. But maybe a function for something like this already exists?

Any suggestions for code that I could use in this context?

Best Answer

Well, here is a partial answer - does everything, (except I don't know how to output ASCII code as character on terminal in Latex (\char apparently typesets, and it's not expandable, so cannot be used in \typeout) EDIT: Fixed thanks to uccode trick by @DavidCarlisle in comments.), except the "reverse lookup".

(EDIT2: See also the texref tool (not the package) in comment by @codebeard)

The output is something like this:

...
ASCII [29]: ==^^]== CATCODE {15}: ignored
ASCII [30]: ==^^^== CATCODE {15}: ignored
ASCII [31]: ==^^_== CATCODE {15}: ignored
ASCII [32]: == == CATCODE {10}: space
ASCII [33]: ==!== CATCODE {12}: otherchar
ASCII [34]: =="== CATCODE {12}: otherchar
ASCII [35]: ==#== CATCODE {6}: parameter
ASCII [36]: ==$== CATCODE {3}: math shift
ASCII [37]: ==%== CATCODE {14}: comment
ASCII [38]: ==&== CATCODE {4}: tab
ASCII [39]: =='== CATCODE {12}: otherchar
ASCII [40]: ==(== CATCODE {12}: otherchar
ASCII [41]: ==)== CATCODE {12}: otherchar
....

.. and the code is here:

\documentclass[12pt]{article}

% http://mathematics.nsetzer.com/latex/latex_for_loop.html
\usepackage{ifthen}
\newcommand{\forloop}[5][1]%
{%
\setcounter{#2}{#3}%
\ifthenelse{#4}%
    {%
    #5%
    \addtocounter{#2}{#1}%
    \forloop[#1]{#2}{\value{#2}}{#4}{#5}%
    }%
% Else
    {%
    }%
}%

\newcommand{\printcatcode}[1]{%
   \ifcase\catcode`#1\relax
      escape\or
      beginning of group\or
      end of group\or
      math shift\or
      tab\or
      end of line\or
      parameter\or
      superscript\or
      subscript\or
      ignored\or
      space\or
      letter\or
      otherchar\or
      active\or
      comment\or
      ignored\fi}

\newcommand{\printcatcodeI}[1]{%
   \ifcase\catcode#1\relax
      escape\or
      beginning of group\or
      end of group\or
      math shift\or
      tab\or
      end of line\or
      parameter\or
      superscript\or
      subscript\or
      ignored\or
      space\or
      letter\or
      otherchar\or
      active\or
      comment\or
      ignored\fi}

\newcounter{ct}

% NOTE: \char *typesets*
% \typeout{\char\thect} doesn't work;
% \char typesets - and doesn't expand!
% to print character from code, use uccode trick
%  (with A as standin, only local changes):
%
\def\printCatcodeTableStdout{%
  \forloop[1]{ct}{0}{\value{ct} < 256}{%
   {% code_block (changes only in local scope)
    \uccode`A=\value{ct} %
    \uppercase{ %
      \typeout{ascii [\thect]: ==A==  catcode {\the\catcode\thect}: \printcatcodeI\thect }
    } % end uppercase
   } % end code_block
  } % end forloop
  %\typeout{=A=} % check if back to normal? yes
}

\begin{document}

\printCatcodeTableStdout

\end{document}
Related Question