# Lua to Tex : content and variable content different?

Here is something bizarre ! (I mean I don't understand). I enter a content, let's say "Something" in the (global) variable context. In line 2 of the following code, as one may anticipate, we have the same printout for the content of context and for the literal Something. However, in line 3, the content of context and the literal Something are recognized different !

\luaexec{context="Something"}
§§\luaexec{tex.print(context)}§§Something§§\\
\ifthenelse{\equal{\luaexec{tex.print(context)}}{Something}}{egal}{different}


Is there a problem of catcode, that makes the two look identical when they are different ? should I prefer luadirect, directlua, etc. instead ?

In fact, I tried with \luadirect instead of \luaexec, and it seems working, sometimes (not very stable, as sometimes, in other contexts, it doesn't work).

\luaexec{context="Something"}
§§\luaexec{tex.print(context)}§§Something§§\\


How shall I assure that the content of the variable context is recognized exactly as the literal content ?

• \luaexec is not expandable. – egreg Dec 26 '15 at 13:46
• @egreg I don't understand : there is no macro in \luaexec to expand. Moreover, I read in the short manual re. LuaCode by Manuel Pégourié-Gonnard that TeX macros are expanded in \luaexec. Did I confused something ? – user1771398 Dec 26 '15 at 13:55
• @user1771398 As @egreg pointed out \luaexec itself is not expandable. But why do you even compare these on the TeX level and not just use \directlua{tex.print(context == "Something")}? – Henri Menke Dec 26 '15 at 13:56
• @user1771398 Sorry, missing tostring, that is to say \directlua{tex.print(tostring(context == "Something"))}. – Henri Menke Dec 26 '15 at 14:03
• I have the same Something to place at different points of macros. I wished to replace all these occurrences by a unique affectation context="Something" and then use \luaexec{tex.print(context)} instead of Something everywhere. I observed that it just did not work. That's the reason why I began to print out the two and then to compare them with ifthenelse to understand why it was not working. – user1771398 Dec 26 '15 at 14:04

The \equal test for \ifthenelse fully expands its arguments (with \protected@edef); so, for instance,

\ifthenelse{\equal{\textbf}{\expandafter\protect\csname textbf \endcsname}}{a}{b}


returns a, because the token lists resulting from these ones, after \protected@edef is applied, are the same.

On the other hand, \luaexec doesn't fully expand to a string, as mentioned in footnote 2 in the manual for luacode.

If I try

\documentclass{article}
\usepackage{ifthen,luacode}

\begin{document}

\luaexec{context="Something"}
\makeatletter\protected@edef\test{\luaexec{tex.print(context)}}
\show\test

\end{document}


I get

> \test=macro:
->\begingroup \escapechar 92 \newlinechar 10 \edef \protect \\{\\}\edef \protect \nobreakspace  {}{~}\let \%=%\let \#=#\endgroup Something.


and it's quite clear this is not the same as Something.

On the other hand, \luadirect is fully expandable and, if I change the second instance of \luaexec in the above code, I get

> \test=macro:
->Something.

• It is rather subtile, being a an implicit of a succession of footnotes ;) but now I understand clearly why the two print the same but are not the same at all. Thanks for the pedagogic example ! – user1771398 Dec 26 '15 at 14:59