Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 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".

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}
share|improve this answer
1  
one way to typeout the character is (no formatting as I need the backtick in the code) \def\printCatcodeTableStdout{% \forloop[1]{ct}{1}{\value{ct} < 256}{% code_block {\uccode`A=\value{ct} \uppercase{\typeout{== [\thect] ==-A-== {\the\catcode\thect} == \printcatcodeI\thect }}} } % end forloop } –  David Carlisle Jun 23 '12 at 14:39
    
Fantastic, thanks for that @DavidCarlisle - would you mind posting the \uccode`A=87 \uppercase{\typeout{-A-}} trick as answer to output ASCII code as character on terminal in Latex, so I can accept it there? I'll edit this post in the meantime... Many thanks again - cheers! –  sdaau Jun 23 '12 at 14:52
    
I have written a small tool called texref which can give quick information about category codes and character information, and maybe other things too in future. If you think it's useful, perhaps consider including it in your answer. github.com/kieranclancy/texref.git –  codebeard Jul 8 '12 at 16:32
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.