1

I am fond of the TeX macro language. I would like to take from TeX only the macro capabilities, that is, the expansion process, the \catcode, \def, \let, \expandafter etc., but I'm not interested in the typesetting output. In other words, I don't need the \hbox, the output routine, the glues, the pdf output etc. I need only text characters at the end of the process, which would be output in a text file.

To put flesh on bones, let me give an example:

   \def\foo{hello world\ }

   \let\foob=\foo

   \expandafter\def\csname foo3\endcsname {hello everybody}

   \foo \foob \csname foo3\endcsname

   \end

The above program would output "hello world hello world hello everybody" in a text file.

What is the simplest way to obtain the desired result? I guess one has to cut Knuth's program just after it has totally expanded the tokens, and before it transforms them to typeset output.

7
  • it's tricky to extract from tex as it does not work in the order indicated in your final paragraph, expansion and typesetting are closely interleaved in tex's main loop. it is possible of course, latexml for example does this in perl. Commented Feb 12, 2023 at 23:02
  • in tex \let\foo2=\foo would assign \foo to be 2 and then typeset =2 is that what you want here, also you define a command with name foo3 but never call it. \foo3 is the same as \foo 3 Commented Feb 12, 2023 at 23:05
  • if I fix ! Undefined control sequence \encsname your test gives this output in plain tex Commented Feb 12, 2023 at 23:09
  • I added an example that I think you intended, it typesets the text you stated if used with plain tex. Commented Feb 12, 2023 at 23:18
  • You can use \openout and \write to send expanded macros in plan text (ASCII). Commented Feb 13, 2023 at 4:20

1 Answer 1

6

Depending on your use case you may find it easier to use luatex and not use its typesetting than to really extract the expansion.

This modified version of your test produces no pdf but a text file (\jobname .txt) that contains

hello world hello world hello everybody

if used with luatex



\immediate\openout1\jobname.txt
\immediate\write1{
\immediateassigned{
   \def\ { }
   \def\foo{hello world\ }
   \let\foob=\foo
   \expandafter\def\csname foo3\endcsname {hello everybody}
}
   \foo \foob \csname foo3\endcsname
}

   \end
2
  • Thank you for the answer. I don't know Luatex. but I think that if used in this way, it is possible to obtain the same result with ordinary TeX. Actually, I did program a code that input the lines of code one after one, and with a double input mechanism, allows totally expandable macro to be used inside the code. So, I have exhausted this kind of solution. The problem is that I want a real typesetting interface where the user can interact with the coding as easily and as powerfully as is possible with TeX.
    – MikeTeX
    Commented Feb 13, 2023 at 6:26
  • Also, I found your clue about Latexml interesting. I will see what I can do there.
    – MikeTeX
    Commented Feb 13, 2023 at 6:28

You must log in to answer this question.

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