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 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 at 21:36
  • 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 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 at 23:02
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).

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.