2

I'd like to get the meaning of a LaTeX command in fully expanded form in a lua variable. What is the easiest way of getting there? In other words, what should I be replacing the function getmeaning with?

% !TeX program = lualatex
\documentclass{article}
\usepackage{luacode,luatexbase}

\begin{luacode}
function whatis(s)
  s="\\" .. s
  x=getmeaning(s)
  end
\end{luacode}

\newcommand{\whatis}[1]{\luadirect{whatis(\luastring{#1})}} 

\begin{document}
I want the contents of \textbackslash{}foo fully expanded to appear in the Lua variable x.
\whatis{foo}
\end{document}
JPi
  • 13,595
  • 1
    what would be your expectations of "fully expanded" for \def\foo{\def\a{1}\def\b{\a}\def\c{\b}\the\numexpr\a+\b+\c\relax} (to stay simple). What happens if \expandafter\expandafter\expandafter\x\expandafter\string\b is added to contents of \foo? –  Oct 01 '17 at 16:58
  • 3
    "fully expanded" doesn't have a general definition, you can put the \meaning into a Lua string simply by \def\whatis#1{\directlua{s="\expandafter\luaescape\expandafter{\meaning#1}"}}...\whatis\foo – David Carlisle Oct 01 '17 at 17:36
  • Thanks @David. I was hoping for something at the lua end, but perhaps this will do. By fully expanded I meant whatever it is that edef does. – JPi Oct 01 '17 at 20:14
  • 1
    @JPi yes but if you put a general latex command in an \edef you get an error message and no usable document. try \edef\zz{\section{}} given that that gives a fatal error what yould you want \whatis{\section} to do? – David Carlisle Oct 01 '17 at 20:15
  • I am aware of that. Ulrike pointed that out on someone else's question a while back. – JPi Oct 01 '17 at 20:17
  • And basically, I'm mostly looking for ways to get the most out of the lua half of luatex. That requires some learning on my part. – JPi Oct 01 '17 at 20:19
  • Do you know the answer without the requirement of “fully expanded”? – ShreevatsaR Feb 22 '18 at 22:26
  • 1
    ^ Answering above comment: we can use token.get_macro or token.get_meaning – ShreevatsaR Feb 23 '18 at 02:35

1 Answers1

3

You can access the replacement text of a macro with token.get_macro but full expansion (as far as it is defined at all) is something firmly on the tex side of luatex. \directlua already does edef style expansion so that doesn't leave a lot for Lua to do other than save the resulting string in a global variable.

I show both forms here.

enter image description here

\documentclass{article}
\directlua{
function whatis(s)
  x=token.get_macro(s)
  end

function whatisE(s)
  xe=s
  end

}

\def\foo{a \zzz\space b}
\def\zzz{XYZ}

\newcommand{\whatis}[1]{\directlua{whatis("#1")}} 
\newcommand{\whatisE}[1]{\directlua{whatisE("\csname#1\endcsname")}} 

\begin{document}
I want the contents of \textbackslash{}foo to appear in the Lua variable x.
\whatis{foo}

I want the contents of \textbackslash{}foo fully expanded to appear in the Lua variable xe.
\whatisE{foo}

x is \texttt{\directlua{tex.print(-2,x)}}

xe is \texttt{\directlua{tex.print(-2,xe)}}
\end{document}
David Carlisle
  • 757,742