6

Is it possible to define a macro, call it charwithcodepoint, such that

\charwithcodepoint{A}

emits an equivalent of

\texttt{A} (U+0041)

And similar for other codepoints as well. If not, can I do the opposite, codepointwithchar so

\codepointwithchar{U+0041}

emits an equivalent to

\texttt{A} (U+0041)

I would like to do so in order to document a parser. Is there a package that support this?

The engine pdftex and the input file is UTF-8.

0

1 Answer 1

5

New version that takes a single UTF-8 character or a single ASCII letter command sequence such as \#

enter image description here

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\DeclareUnicodeCharacter{1D400}{\textbf{\textrm{A}}}% for demo only

% code mostly from the 2015/10/01 latex release inputenc)

\makeatletter
\def\charwithcodepointb#1{%
\texttt{#1}
(%
U+%
\expandafter\UTFviii@hexnumber\expandafter{%
                     \the\numexpr\expandafter\decode@UTFviii\detokenize{#1}\relax})%
}


\gdef\decode@UTFviii#1\relax{%
  \expandafter\UTFviii@cleanup
    \the\numexpr\dec@de@UTFviii#1\relax)))))\@empty}
\gdef\UTFviii@cleanup#1)#2\@empty{#1}
\def\d@c@de@UTFviii#1{%
\dec@de@UTFviii{#1}}
\gdef\dec@de@UTFviii#1{%
\ifx\relax#1%
\else
  \ifnum`#1>"EF 
    ((((`#1-"F0)%
  \else
    \ifnum`#1>"DF 
      (((`#1-"E0)%
    \else
      \ifnum`#1>"BF 
        ((`#1-"C0)%
      \else
        \ifnum`#1>"7F 
        )*64+(`#1-"80)%
        \else
        +`#1 %
        \fi
      \fi
    \fi
  \fi
  \expandafter\dec@de@UTFviii
\fi}
\gdef\UTFviii@hexnumber#1{%
\ifnum#1<"10 0\fi
\ifnum#1<"100 0\fi
\ifnum#1<"1000 0\fi
\UTFviii@hexnumb@r{#1}}
\gdef\UTFviii@hexnumb@r#1{%
\ifnum#1>15 %
\expandafter\UTFviii@hexnumb@r\expandafter{\the\numexpr(#1-8)/16\relax}%
\fi
\UTFviii@hexdigit{\numexpr#1\ifnum#1>0-((#1-8)/16)*16\fi\relax}%
}
\gdef\UTFviii@hexdigit#1{\ifcase\numexpr#1\relax
  0\or1\or2\or3\or4\or5\or6\or7\or8\or9\or
  A\or B\or C\or D\or E\or F\fi}



\def\charwithcodepoint#1{%
\ifcat\relax\noexpand#1%
\expandafter\expandafter\expandafter\charwithcodepointb\expandafter\@gobble\string#1
\else
\charwithcodepointb{#1}%
\fi}

\makeatother

\begin{document}

\charwithcodepoint{A}

\charwithcodepoint{Á}

\charwithcodepoint{𝐀}


\charwithcodepoint{\A}

\charwithcodepoint{\\}

\charwithcodepoint{\#}


\end{document}
12
  • This might be worth to put into a package. Interestingly \textbackslash is 0x5cd rather than 0x5c
    – user877329
    Sep 16, 2015 at 13:49
  • @user877329 I just copied it from inputenc package as I note in the comment in the code. Sep 16, 2015 at 13:57
  • How can I fix # and `\`, because these are wrong (And my syntax use them).
    – user877329
    Sep 16, 2015 at 13:58
  • @user877329 \textbackslash ?? The command has to be given a single character, you can not give it a command sequence. Sep 16, 2015 at 13:58
  • 1
    @user877329 If you only want the ascii range you can discard 90% of the above code:-) Sep 16, 2015 at 14:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .