[Tex/LaTex] Is it possible to view the latex code after Lua expansion is made

luacodeluatex

When using Lua code inside Latex file, where Lua code sends to Tex Latex code to insert into the document at the location the lua code is made, it will be useful to look at the content of Latex file (internal) after the expansion is made but right before the file is compiled to pdf. This can help in debugging and in many other cases.

Is there an option to do this? A small example will help explain what I mean.

Given this latex file

\documentclass{article}
\usepackage{luacode}
\begin{document}

The following is my table

\begin{luacode}
i=6
j=8
tex.print("\\begin{tabular}{|l|l|}\\hline")
tex.print(i.."&"..j)
tex.print("\\\\ \\hline")
tex.print("\\end{tabular}")
\end{luacode}

\end{document}

Compiling this gives the pdf file:

Mathematica graphics

What I'd like to now see is the Latex file that would resulted after the Lua code expansion, which should be something as follows

\documentclass{article}
\usepackage{luacode}
\begin{document}

The following is my table

\begin{tabular}{|l|l|}\hline
6 & 8 \\\hline
\end{tabular}

\end{document}

I could not find an option to do this so far.

TL 2015


Update

Another example to make clear what is being asked here. I'd like to see not only the latex code that was generated from the lua bits of code, but the full Latex document that would result after all the expansion of all lua code in the original Latex. But this has to be done without modifying the original Latex document itself.

Assuming there is this virtual pipeline:

Latex file -> Lua code expand -> Updated Latex (internal)-> lualatex ->pdf 

I'd like to get a copy of the full latex code/file just after all the lua code expanded. Here is another example. Given this Latex file input:

\documentclass{article}
\usepackage{luacode}
\begin{document}
The following is my table
\begin{luacode*}
i=6
j=8
tex.print("\\begin{tabular}{|l|l|}\\hline")
tex.print(i.."&"..j)
tex.print("\\\\ \\hline")
tex.print("\\end{tabular}")
\end{luacode*}

Our annual budget for the company is \luaexec{tex.print(200*15^3)} dollars as 
shown in this list of items
\begin{luacode*}
 tex.print("\\begin{itemize}")
 for i=1,5 do
   tex.print("\\item "..i^2)
 end
 tex.print("\\end{itemize}")
\end{luacode*}
And so on.
\end{document}

The PDF is

Mathematica graphics

I'd like to get a copy of the following latex file, which has all the Lua code expanded and gone and only the Latex code is left in its place. This file can go to a temporary file, or anywhere, as long as it is the complete Latex file as shown and not bits and pieces. This would be very useful in debugging when there is lots of Lua code in different places in the original Latex file.

\documentclass{article}
\usepackage{luacode}
\begin{document}    
The following is my table

\begin{tabular}{|l|l|}\hline
6 & 8 \\\hline
\end{tabular}

Our annual budget for the company is 675000 dollars as 
shown in this list of items

\begin{itemize}
\item 1
\item 4
\item 9
\item 16
\item 25
\end{itemize}

and so on    
\end{document}

I hope now the question is even more clear 🙂

Best Answer

The \directlua primitive is expandable so provided we formulate the code to use it directly we can show the result:

\documentclass{article}
\begin{document}

The following is my table
\showtokens\expandafter{%
  \directlua{
    i=6
    j=8
    tex.print("\noexpand\\begin{tabular}{|l|l|}\noexpand\\hline")
    tex.print(i.."&"..j)
    tex.print("\noexpand\\\noexpand\\ \noexpand\\hline")
    tex.print("\noexpand\\end{tabular}")
  }%
}
\end{document}

Note that this will not work with the luacode environment, which performs assignments in TeX and so is not expandable. This means that we have to do more work with the TeX-to-Lua step.


Based on comments, an alternative if to write the Lua code to a file would be

\documentclass{article}
\usepackage{luacode}
\begin{document}

The following is my table

\begin{luacode}
local i, j = 6, 8
local f = assert(io.open("\jobname.xxx", "w"))
local function mywrite(text)
  tex.print(text)
  f:write(text .. "\string\n")
end
mywrite("\\begin{tabular}{|l|l|}\\hline")
mywrite(i.."&"..j)
mywrite("\\\\ \\hline")
mywrite("\\end{tabular}")
\end{luacode}

\end{document}

(If used repeatedly it would be sensible to but the Lua code into a separate file, particularly the store-result code.)