0

I presume these problems are related.

\documentclass[a4paper]{article}

\begin{document}

\message{\edef\foo{Foo}\foo}
% gives

%! Undefined control sequence.
%l.5 \message{\edef\foo
%                    {Foo}\foo

\newcommand\testing[2][foo]{#1 = #2}
\testing[Foo]{Bar} % works
\testing{bar} % works

\directlua{\testing[Foo]{Bar}}
% or
\directlua{\testing{bar}}
% gives

%! Use of \\testing doesn't match its definition.
%\kernel@ifnextchar ...rved@d =#1\def \reserved@a {
%                                                  #2}\def \reserved@b {#3}\f...
%
%l.16 \directlua{\testing
%                      [Foo]{Bar}}

\end{document}

There are many other versions of these. \defs and \edefs are useful when writing macros. I don't know where optional arguments are officially documented (I only have the TeXbook and Lamport's book and this postdates these) but the instructions I read on-line for optional arguments doesn't say anything about the resulting macros only being available for use in certain sections of code.

Please could someone explain what's going on and tell me where I can find the full gory details: in particular what can and cannot be put inside \directlua and what other TeX/LaTeX commands behave in this way. I am writing quite a lot of macros to go inside \directlua, and don't want to be caught out again!

  • 1
    you can put anything there but it has to expand to Lua code. looking ahead for optional arguments does not work by expansion. – David Carlisle Feb 15 '20 at 20:28
  • OK then maybe I asked the wrong question... where do I find out which LaTeX commands work entirely by expansion? And what indeed is "expansion"? (e.g. are \if's expansion?) – user2444353 Feb 15 '20 at 21:36
  • https://tex.stackexchange.com/questions/66118/advantages-and-disadvantages-of-fully-expandable-macros – David Carlisle Feb 15 '20 at 22:16
  • thank you... that was helpful. I was also reading link which is NOT helpful. It seems to be full of errors, including a quote from a certain DE Knuth from which one can infer that execution of a \def IS expansion. (To be fair, his example wasn't about expansion at all... its just that he called it expansion.) This is part of the problem: if a latex command is defined who's job is simply to convert a sequence of tokens into another sequence of tokens, then we typically call its action expansion, even when it isn't. – user2444353 Feb 15 '20 at 22:49
  • I don't know why you say the second link wasn't helpful, I just looked over the answers and they look Ok to me. I don't know who the "we" is that you refer to but most of the regular people answering on this site are always very careful to only refer to expansion when referring to expandable commands. – David Carlisle Feb 15 '20 at 23:02
  • the quote from DEK is all about expansion, for example. It does not say that \def works by expansion, it describes the expansion of the command defined by \def. – David Carlisle Feb 15 '20 at 23:04
  • It seems the precise answer to the DEK \puzzle is \a\a\a\a\a, but to take this further expansions and \defs are needed. Therefore is seems easy to infer (incorrectly) from the way the puzzle is set that \def is part of "expansion". I am sure I did when I read that in the TeXbook, hence my confusion! – user2444353 Feb 16 '20 at 00:23
  • ...and to say that \newcommand\greeting[1]{Hello #1!} EXPANDS into \def\greeting#1{Hello #1!} (same link) is misleading and typical. You just told me that optional arguments are not implemented in LaTeX using pure expansion! But this is just quibbling - I learnt a lot, thank you! – user2444353 Feb 16 '20 at 00:35

1 Answers1

1

The tokens have to expand to a valid Lua chunk.

The easiest way to see this in action is to use \write which has similar behaviour but writes to the terminal rather than to the Lua interpreter, so you can see what it is doing.

\documentclass{article}


\begin{document}

\def\aaa{\bbb}
\def\bbb{x={1,2,\ccc}}

\def\ccc{3}
\immediate\write20{\aaa}

\def\ccc{4}
\immediate\write20{\aaa}



\immediate\write20{\def\ccc{3}\aaa}



\end{document}

produces a terminal output of

x={1,2,3}
x={1,2,4}
\def 4{3}x={1,2,4}

so as you see the first two forms are Ok and would produce valid Lua.

But in an expansion only context non expandable tokens like \def are inert, and the assignment doesn't happen so \def\ccc{3} does not define \ccc to be 3. \def stays as \def, \ccc happens to expand to 4 here (if it had not been defined it would give an error as you show in your question) and then the {3} consists of unexpandable tokens so writes as itself. The resulting output would not have been valid Lua had you used \directlua instead of \write.

All macros defined via \def are expandable, and TeX primitives such as \def or \if are expandable or not by definition (the TeXBook specifies whether each primitive is expandable or not). (Or the luatex manual for additional luatex primitives of course).

David Carlisle
  • 757,742