16

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 :)

1
  • @JosephWright I do not know to change the example about to use \directlua and keep everything else the same. I get errors when I replaced luacode with \directlua{...}. If you could show this is done and how to view the generated Latex that will help. I do not want to have external lua files. I want the lua code to be inside the Latex file itself as shown. I am still learning lua and find all these different environments very confusing so far. Thanks
    – Nasser
    Commented Jul 4, 2015 at 22:22

2 Answers 2

12

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.)

8
  • Thanks. fyi, there seems to be some typo somewhere? I get this error on TL 15 lualatex foo1.tex This is LuaTeX, Version beta-0.80.0 (TeX Live 2015) (rev 5238) restricted \write18 enabled. (./foo1.tex LaTeX2e <2015/01/01> Babel <3.9l> and hyphenation patterns for 79 languages loaded. (/usr/local/texlive/2015/texmf-dist/tex/latex/base/article.cls Document Class: article 2014/09/29 v1.4h Standard LaTeX document class (/usr/local/texlive/2015/texmf-dist/tex/latex/base/size10.clo)) (./foo1.aux) > \begin {tabular}{|l|l|}\hline 6&8 \\ \hline \end {tabular}. l.14 } ? x
    – Nasser
    Commented Jul 4, 2015 at 22:31
  • does the above code compile ok for you? Here is screen shot showing the error I get. I copied the code as is. !Mathematica graphics fyi, I am using TL 2015 on Linux mint.
    – Nasser
    Commented Jul 5, 2015 at 0:17
  • @Nasser That's the expected output: you said you wanted to see the code so I expanded the Lua and then used \showtokens in the same way one would with any TeX you wanted to look at.
    – Joseph Wright
    Commented Jul 5, 2015 at 6:41
  • @Nasser I wonder if you want us to print the TeX code into the LaTeX document in a verbatim fashion?
    – Joseph Wright
    Commented Jul 5, 2015 at 6:52
  • Oh, I did not know the message on the screen I was getting was by design. I knew very little about these Latex tricks. But this method is not really practical for me. First of all, I do not want to write "\noexpand" everywhere, this makes the code hard to read and write. All what I was thinking of, is that if the final latex file, after all expansion, could be written to some other file, say foo.lux or something (we need new extension :) and then a user can look at the .lux file to see the full Latex code which was actually compiled to generated the pdf after all the expansion.
    – Nasser
    Commented Jul 5, 2015 at 17:10
11

Instead of using tex.print you can use print. Then the argument is written to the standard output (monitor) and you can control it:

\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}

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

and in the standard output (not the logfile) you'll see:

[...]
(./demo.aux)\begin{tabular}{|l|l|}\hline
6&8
\\ \hline
\end{tabular}
[...]

You can also define your own Lua Print function which does the TeX and log print:

\documentclass{article}
\usepackage{luacode}
\begin{luacode}
function Print (string)
  tex.print(string)   
  print(string)   
end
\end{luacode}

\begin{document}

The following is my table

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

\end{document}
3
  • 1
    Which means one can introduce a new lua command which writes to both.
    – JPi
    Commented Jul 5, 2015 at 7:26
  • Thanks. This does make it little hard to see the bits of the code on the screen, and if I understand you method, it means one has to duplicate all the lua code inside the Latex file? One copy to use print and one copy to use tex.print? it would be nice if luatex would add support to generate the full expended latex to some external file as I said in my above comment to the other answer. Thanks for your answer.
    – Nasser
    Commented Jul 5, 2015 at 17:13
  • See my edites answer
    – user2478
    Commented Jul 5, 2015 at 20:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .