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 have the following MWE which prints the catcode of every character in the list.

The input method is:

\MyList{`+,{`,},`\%,`a}

but I want to simplify as follows:

\MyList{+,{,},\%,a}

Here the MWE:

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

\cs_new:Npn \catcode_print:n #1 
 {
  The~catcode~of~\tl_to_str:n {#1}~is~\char_value_catcode:n {#1} \par
 }
\NewDocumentCommand { \MyList }
 { > { \SplitList { , } } m }
 { \ProcessList {#1} { \catcode_print:n } }
\ExplSyntaxOff
\begin{document}
 \MyList{`+,{`,},`\%,`a}
% \MyList{+,{,},\%,a}
\end{document}

EDIT and PS: I want to inverse the number to get the corresponding character: For example: \tex_number:D A prints 65. But I want to insert 65 and with the result A

share|improve this question
3  
Use \char_value_catcode:n { `#1 } instead of \char_value_catcode:n { #1 }. –  Bruno Le Floch Feb 9 '12 at 18:36
    
@BrunoLeFloch: This was to simple ;-). Do you know a way how to inverse \tex_number:D A`. This will print 65. But I want to insert 65 and want to get A. (LaTeX3 solution isn't deprecated ;-) ). Please provide an answer. –  Marco Daniel Feb 9 '12 at 18:40
add comment

1 Answer

up vote 8 down vote accepted

Not expandably, of course:

\def\achar#1{\begingroup\lccode`!=#1\lowercase{\endgroup!}}

\achar{65}
\achar{`A}
\achar{"41}

will give

A A A

But of course

\char<number>

does the same. The \achar technique can be useful if you want to define a macro to expand to the character given by number (with category code 12):

\def\achar#1#2{\begingroup\lccode`!=#2\lowercase{\endgroup\def#1{!}}}

\achar\firstA{65}
\achar\secondA{`A}
\achar\thirdA{"41}
\show\firstA \show\secondA \show\thirdA

What does \achar do? It starts a group where the lowercase correspondent of the exclamation character is set to character #2 (which is supposed to be a number, in any of the formats TeX allows for specifying an integer constant). This correspondance is maintained by an array (a 256 component vector, in 8 bit TeX, the whole 0x110000 of Unicode for XeTeX and LuaTeX, from 0x0000 to 0x10FFFF): the n-th entry specifies the lowercase correspondent for character number n.

When we say \lowercase{<tokens>}, TeX performs the substitution of each character token with its lowercase correspondent and then inserts back in the input stream the token list so obtained, to be read again. In our case \endgroup, \def and the first argument to \achar are not touched, as they are not character tokens, so only ! is converted, exactly to the character having the number we specified in the second argument. Notice that TeX still hasn't executed \endgroup: it does only when the token list is put back. So the group is closed, the \lccode assignment is forgotten and the \def is executed.

With \achar\firstA{65}, for example, the replacement text for \firstA is A with category code 12, because ! has category code 12 and \lowercase doesn't change this attribute.

The LaTeX3 code is just the same:

\NewDocumentCommand { \achar } { m m }
  {
    \group_begin:
    \char_set_lccode:nn { `! } { #2 }
    \tl_to_lowercase:n { \group_end: \cs_new:Npn #1 { ! } }
  }
share|improve this answer
    
GREAT! (as always ;-) ) Do you know the LaTeX3 source too? –  Marco Daniel Feb 9 '12 at 19:03
    
Incidentally, the experimental module l3str introduces for internal purposes 256 constant token lists holding each of the bytes: for character codes less than 256, you can access the corresponding character expandably. –  Bruno Le Floch Feb 9 '12 at 22:20
    
Will you add also the missing 1113856 for Unicode? :) –  egreg Feb 9 '12 at 22:25
    
@egreg better a late reply than none: no, I won't add ones for Unicode. The 256 bytes are useful for all engines when converting from an encoding to another (e.g., UTF-16 for some pdf strings). –  Bruno Le Floch Dec 30 '12 at 0:31
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.