Print catcodes as subscripts

Apart from spaces you could do

enter image description here

\documentclass{article}
\usepackage{expl3,xparse}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\ExplSyntaxOn

\NewDocumentCommand\showcatcodes { m }
 {
   \expandafter\tl_map_inline:nn\expandafter{ \detokenize{#1} }
   {
     \tl_to_str:n {##1} \textsubscript{\char_value_catcode:n{`##1}}
   }
 }

\ExplSyntaxOff


\begin{document}

\showcatcodes{a bcde123!$_ ^€\{\} {} } 
\end{document}

spaces you ought to be able to do by using \scantokens to change the catcode of space after making everything else safe with `\detokenize but scantokens is a dangerous beast and it's biting back at present.


You can use l3regex:

\documentclass{article}
\usepackage{expl3,xparse,l3regex}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\ExplSyntaxOn

\NewDocumentCommand\showcatcodes { v }
 {
  \regex_extract_all:nnN { . } { #1 } \l_tmpa_seq
  \seq_map_inline:Nn \l_tmpa_seq
   { \ulrike_value_catcode:x { \tl_to_str:n {##1} } }
 }
\cs_new_protected:Nn \ulrike_value_catcode:n
 {
  #1\textsubscript{\char_value_catcode:n { `#1 }}
 }
\cs_generate_variant:Nn \ulrike_value_catcode:n { x }
\ExplSyntaxOff


\begin{document}
\showcatcodes{abcde123!$_ ^€\{\}}
\end{document}

enter image description here

This is the output with XeLaTeX (after removing inputenc and fontenc):

enter image description here

Final variant with \textvisiblespace and monospaced fonts for the characters:

\documentclass{article}
\usepackage{expl3,xparse,l3regex}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\ExplSyntaxOn

\NewDocumentCommand\showcatcodes { v }
 {
  \group_begin:
  \ttfamily
  \regex_extract_all:nnN { . } { #1 } \l_tmpa_seq
  \seq_map_inline:Nn \l_tmpa_seq
   { \ulrike_value_catcode:x { \tl_to_str:n {##1} } }
  \group_end:
 }
\cs_new_protected:Nn \ulrike_value_catcode:n
 {
  \tl_if_blank:nTF { #1 } { \textvisiblespace } { #1 }
  \textsubscript{\normalfont\char_value_catcode:n { `#1 }}
 }
\cs_generate_variant:Nn \ulrike_value_catcode:n { x }
\ExplSyntaxOff


\begin{document}
\showcatcodes{abcde123!$_ ^€\{\}}
\end{document}

enter image description here

Tags:

Expl3

Catcodes