1

Half of the pain of programming TeX is the difference between expandable and non-expandable commands, the latter usually being a result of an assignment taking place inside the command definition. That makes me wonder why neither Knuth’s TeX nor any later TeX engines add a construction \local\def (or with some other name) that solves this issue once and for all? Then you could do

\def\mycmd{%
    \local\def\tempa{<something>}%
    \local\def\tempb{<something>}%
    \local\def\tempc##1##2{<something>}%
    \tempc{\tempa}{\tempb}%
}

and still have an expandable command. (I know you can use a construction like \group_begin: ... \tl_set:Nn \l__gaussler_temp_tl { ... } \exp_args:NV \group_end: \l__gaussler_temp_tl to go obtain a workaround, but it will not be expandable). In other words, it would work just like a function in a more modern programming language. Using \local outside of a command definition could yield an error. This would solve a great deal of the headaches of programming TeX.

Why has this not been done yet?

Werner
  • 603,163
Gaussler
  • 12,801
  • Comments are not for extended discussion; this conversation has been moved to chat. – Joseph Wright Nov 13 '20 at 13:40
  • I've moved the comments here to chat as they are quite discursive. The question of whether we are talking about local-to-macros or expandable assignments remains open. – Joseph Wright Nov 13 '20 at 13:40
  • @PhelypeOleinik Trust me, I need a lot of expandable commands internally. For instance, I often need to obtain data from parent classes, and this data has to be processed on the spot, using expandable commands. This does occasionally cause issues. Such issues could have been avoided if we had expandable, local definitions. – Gaussler Nov 13 '20 at 13:42
  • \local seems an odd name, eg luatex has expandable immediate definitions but they are not local to a definition (having anything local to a definition would be odd in a macro expansion language) – David Carlisle Nov 13 '20 at 14:46
  • 1
    @DavidCarlisle We are a lot who would prefer if TeX behaved more like a normal programming language, where commands behaved more like functions. – Gaussler Nov 13 '20 at 17:40
  • 3
    @Gaussler Every language has its idioms, , while it's possible to sort of half graft some constructs from another idiom into a language it never really works well. If you want to have a language with functions then you need a different language not adding some individual feature to the existing one. The same features are seen for example in The C pre-processor macro language: C has compiled functions with definitions having function scope. cpp has macro definitions with the replacement text not being a definition scope. TeX is like cpp not like c. – David Carlisle Nov 13 '20 at 17:53
  • Could you give more exact and real example, why you need to have assignments given locally when one macro body is processed? I can't imagine such application. BTW: your example above needs doubled the # characters in the \tempc definition. – wipet Nov 24 '20 at 20:55

1 Answers1

4

luatex has what you are (perhaps) asking for

\documentclass{article}

\begin{document}

{ \edef\zz{ ab \immediateassignment\def\zzz{this} cd \zzz}

zz: [\zz]

zzz: [\zzz]

\meaning\zzz }

\meaning\zzz

\end{document}

Here \immediateassignment\def\zzz is an expandable assignment of \zzz that is therefore allowed in the \edef including using the defined command within the same \edef. It is a local assignment but local to the current group not to the current \edef. It isn't clear if that is what you meant.

so \zzz is used in the \edef and is also defined after the \edef but goes out of scope at the } that ends the current group, so \meaning shows it as undefined again on the final line.

enter image description here

David Carlisle
  • 757,742