TeX Macros – How to Place Explicit Character Tokens Before or Behind Tokens Forming Arbitrary Macro Arguments

bracesmacrostex-core

Assume tokens
\macro{1612512}2{1612612}2{1⟨arbitrary non-outer brace balanced tokens⟩}2;

  • the 1st argument of \macro holds a TeX-⟨number⟩-quantity in the range of possible codepoints of the TeX engine's internal character encoding scheme which denotes the character code of the explicit category 1 character token to create;
  • the 2nd argument of \macro holds a TeX-⟨number⟩-quantity in the range of possible codepoints of the TeX engine's internal character encoding scheme which denotes the character code of the explicit category 2 character token to create;
  • the 3rd argument of \macro holds an arbitrary amount of non-outer brace balanced tokens that are to be placed between the explicit category 1 character token and the explicit category 2 character token.

How to define an expandable macro \macro in Knuth's TeX—\expanded not available, where expansion at some stage delivers
A1⟨arbitrary non-outer brace balanced tokens⟩B2 ?

How to define an expandable macro \macro for an (elder) utf-8-engine with \expanded not being available, where expansion at some stage delivers
A1⟨arbitrary non-outer brace balanced tokens⟩B2 ?


With \expanded/expl3 available you can do s.th. along

\ExplSyntaxOn
\cs_new:Npn \macro #1#2#3 
  {
    \tex_expanded:D { \char_generate:nn {#1}{1}
                      \exp_not:n {#3}
                      \char_generate:nn {#2}{2}
                    }
  }
\ExplSyntaxOff

If expandability was not an issue, one could do s.th. along

\ExplSyntaxOn
\newtoks\scratch
\cs_new:Npn \macro #1#2#3 
  {
    \scratch={#3}
    \exp_after:wN \def
    \exp_after:wN \test 
    \exp_after:wN {
     \exp:w
     \exp_after:wN \exp_after:wN \exp_after:wN
     \exp_after:wN
     \exp_after:wN \exp_after:wN \exp_after:wN
     \exp_end: 
     \exp_after:wN \exp_after:wN \exp_after:wN
     \exp_after:wN
     \char_generate:nn {#1}{1}
     \the \exp_after:wN \exp_after:wN \exp_after:wN \scratch \char_generate:nn {#2}{2}
    }
  }
\ExplSyntaxOff

It is academical interest. I try to learn how TeX works in order to gain a good understanding of what you can do and what you can't. And how. I think that approach is better than to start by trying to write documents and getting stuck all the time while approaching the deadline.

I know expl3 is LaTeX, but the only real expl3-thing I use is \char_generate:nn and according to source3.pdf that is fully functional with traditional LaTeX (8 bit engines) also, which implies that a stripped down variant only delivering explicit character tokens of category 1/2 can also be implemented using plain tex.
E.g., one could define a macro for each code point which with two expansion-steps, \romannumeral-expansion and brace-hacking delivers the character token in question.

Best Answer

There is no reason to do this but if you define 256*256 helper functions you could do

\def\macro#1#2{%
  \csname x\number#1x\number#2\endcsname
  }


  % then lots of definitions like

  {\catcode65=1\relax \catcode66=2\relax
  \expandafter
  \gdef\csname x65x66\endcsname#1{A#1B}
}


\immediate\write20
\expandafter\expandafter\expandafter
\expandafter\expandafter\expandafter
\expandafter
{%
  \macro{65}{66}{some \relax \alpha tokens}%
}

\bye
Related Question