2

My curiosity lies on mapping for transliteration. I am working on a project that uses transliteration. I have seen some solved code for this. But I could understand them. For example,

\def\tcmapto#1#2{\expandafter\def\csname tcmapto\number`#1\endcsname{#2}}
\def\tcmapnumto#1#2{\expandafter\def\csname tcmapto#1\endcsname{#2}}
\def\tcremap#1{\ifcsname tcmapto\number`#1\endcsname
                 \csname tcmapto\number`#1\endcsname\else#1\fi}

Question: How these three lines work part by part. Can anyone elaborate with patience. Thank you.

Complete code:

\documentclass{article}
\usepackage{tokcycle}
\def\tcmapto#1#2{\expandafter\def\csname tcmapto\number`#1\endcsname{#2}}
\def\tcmapnumto#1#2{\expandafter\def\csname tcmapto#1\endcsname{#2}}
\def\tcremap#1{\ifcsname tcmapto\number`#1\endcsname
                 \csname tcmapto\number`#1\endcsname\else#1\fi}

\tcmapto{ং}{n}% OR \tcmapnumto{2434}{n} \tcmapto{আ}{Ā}% OR \tcmapnumto{2438}{Ā} \tcmapto{ন}{n}% OR \tcmapnumto{2472}{n} \tcmapto{ব}{b}% OR \tcmapnumto{2476}{b} \tcmapto{ম}{m}% OR \tcmapnumto{2478}{m} \tcmapto{র}{ra}% OR \tcmapnumto{2480}{ra} \tcmapto{ল}{l}% OR \tcmapnumto{2482}{l} \tcmapto{স}{s}% OR \tcmapnumto{2488}{s} \tcmapto{া}{ā}% OR \tcmapnumto{2494}{ā} \tcmapto{ো}{ō}% OR \tcmapnumto{2507}{ō}

\begin{document} \Characterdirective{\addcytoks[x]{\tcremap{#1}}} \tokencyclexpress আমার সোনার বাংলা

আ-মা-র সো-না-র বাং-লা \endtokencyclexpress \end{document}

mmr
  • 2,249
  • 5
  • 22
  • I wrote a generic answer on how to understand code here. Hope it helps. (disclaimer: I wrote that answer) – user202729 Nov 10 '21 at 14:41
  • In this particular case reading only the TeXBook is sufficient. (plus the documentation of that package being used) – user202729 Nov 10 '21 at 14:41
  • The only thing to add to gernot's fine answer, is that the token-cycle proceeds through the input stream token by token. Macros and space tokens are echoed to the output directly, whereas character tokens are each subjected to \tcremap, in turn. Group tokens are retained, while the tokens within the group are also processed individually through the cycle. – Steven B. Segletes Nov 10 '21 at 15:31

1 Answers1

2
  • \def\tcmapto#1#2{\expandafter\def\csname tcmapto\number`#1\endcsname{#2}}

defines \tcmapto in such a way that each call of the form \tcmapto{A}{B} defines a command with the name

\tcpmapto<character code of A>

that expands to B. This way \tcmapto stores the transliteration of A to B in the new macro \tcpmapto<character code of A>. As an example,

\tcmapto{a}{b}

defines the command \tcmapto97 that will expand to b (97 is the character code of a).

  • \def\tcmapnumto#1#2{\expandafter\def\csname tcmapto#1\endcsname{#2}}

does the same, but \tcmapnumto takes the character code itself as the first argument, instead of the character. As an example,

\tcmapnumto{97}{b}

also defines the command \tcmapto97 that will expand to b.

  • \def\tcremap#1{\ifcsname tcmapto\number`#1\endcsname\csname tcmapto\number`#1\endcsname\else#1\fi}

\tcremap{A} checks whether a macro \tcmapto<character code of A> exists and, if it does, executes it. Otherwise it outputs A. The effect is that \tcremap{A} transliterates A to B, if this transliteration has been set up, or outputs A.


The basic building blocks.

  • \csname ... \endcsname constructs a command name from whatever comes inbetween, expanding macros on the way. \csname is useful when defining unusual macro names, like those with digits in it.

  • \def\macroname{expansion} defines the macro \macroname to expand to expansion.

  • \expandafter\def\csname ... \endcsname means: First expand \csname ... \endcsname to get the macro name, and then define it. Without \expandafter, \def would redefine the macro name \csname itself.

  • \ifcsname ... \endcsname <then branch> \else <else branch> \fi: If the characters between \ifcsname and \endcsname are the name of an existing macro, do the <then branch>, otherwise do the <else branch>.

  • \number`<character> expands to the code of <character>

gernot
  • 49,614