Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I can write:

 \def\myarray{{Nrs,1,2,3}}

then I can call lua:

  \luadirect{myfunc(\myarray)}

and access the values in the array/table.

How would one do it for multidimensional ones?

I initially thought to define a multidimensional array in Tex, but I googled and search through all the pdf docs and did not even find the definition of an array. So now I decided my only option would be:

 \def\myarray_a{{name,1,2,3}}
 \def\myarray_b{{name_b,1,4,5}}
 \luadirect{myfunct(\myarra_a,\myarray_b)

That should work fine, but I still would like to know if there is another way.


I wanted to pass a n X m array from lualatex to lua.

I google and found somewhere to define an array in latex you do:

   \def\myarr{{1,2,3}}

that I used before, but I could not figure out how to pass a n x m array. So I asked and then also experimented.

This is a MWE:

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

\begin{luacode*}
 function atest(arrs)
   tex.print('type of parameter received: '..type(arrs)..'; containing: \\par')
   for i,v in ipairs(arrs) do
     tex.print('i='..i..', type='..type(v)..', name='..v.name..', v[1]'..v[1]..'\\par')
     v[1]=v[1]*2;
   end
 end
\end{luacode*}

\begin{document}
\def\flowa{{
    {name="Arr1",1,2,3},
    {name="Arr2",4,5,6}
    }}
\hrulefill

\textbf{\underline{The values:}}\\

\flowa\\

\textbf{\underline{As seen by lua:}}\\

\luadirect{atest(\flowa)}

\hrulefill

\textbf{\underline{Passing by reference (no changes):}}\\

\flowa
\end{document}

This produces:

the result

but it is, as shown, passing by value.

share|improve this question
    
As Caramdir says. But why it does not work is really simple: all three uses of \flowa instantiate an independent anonymous array. If you want to see any kind of persistent effect, you have to store the array in an actual variable. –  Taco Hoekwater Jul 23 '11 at 18:50
    
@theo Your suggestion worked. It just means one has to handle it correctly in lua. I did not know how to pass and handle it correctly in lua. I would have liked to change the the array passed to lua. _If you feel I should revoke the question I can, but the interfaces between lualatex and lua is not very well documented._ –  Louis Jul 23 '11 at 19:10
    
I moved your additional post now to the answer. Please provide further information by updating the answer, not by posting "answer" posts. The question is fine, no need to delete it. –  Martin Scharrer Jul 23 '11 at 20:59
    
@Taco: Could you update your answer so that Louis can accept it. –  Martin Scharrer Jul 23 '11 at 20:59
    
I'm working on questions and assignments that I could change just a few numbers and if I add a flag the new question and its correct answer is provided accurately. I previously used spreadtab and numprint, but the rounding comes in too soon. So with this technique and with my other questions and lua I can now produce questions with different numbers without going through the whole process again. (I will next explore using AEB-pro or other similar js-solutions). –  Louis Jul 23 '11 at 21:37
show 1 more comment

1 Answer

up vote 5 down vote accepted

My initial response was:

\def\myarray{{{name,1,2,3}, {name_b,1,4,5}}}

but that does not actually help you, looking at your edits to the question.

This definition indeed passes a two-dimensional array, but it does so using concrete values (a 'literal'). Each use of \myarray is then completely unrelated from the other uses of\myarray, so not a single change will be permanent.

If you want to pass a reusable two-dimensional array, you have to use a lua variable (code below). This is not something special in the luatex interface, it is standard programming: you cannot pass by reference unless there is something that can actually be (de)referenced.

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

\begin{luacode*}
 function atest(arrs)
   tex.print('type of parameter received: '..type(arrs)..'; containing: \\par')
   for i,v in ipairs(arrs) do
     tex.print('i='..i..', type='..type(v)..', name='..v.name..', v[1]'..v[1]..'\\par')
   end
 end
function achange(arrs)
   for i,v in ipairs(arrs) do
     v[1]=v[1]*2;
   end
end
\end{luacode*}

\begin{document}
\begin{luacode*}
flowa = {{name="Arr1",1,2,3},
         {name="Arr2",4,5,6}}
\end{luacode*}
\hrulefill

\textbf{\underline{The values:}}\\

\luadirect{atest(flowa)}

\textbf{\underline{As seen by lua:}}\\

\luadirect{achange(flowa)}
\luadirect{atest(flowa)}

\hrulefill

\textbf{\underline{Passing by reference (no changes):}}\\

\luadirect{atest(flowa)}
\end{document}
share|improve this answer
    
It might work, in a previous attempt I tried something similar and did not get joy. I will come back. –  Louis Jul 21 '11 at 11:38
    
It works for Lualatex, but I still can't get the passing to lua correct. –  Louis Jul 22 '11 at 20:50
    
@Louis: perhaps you should post a more complete example of what exactly it is that you want to achieve and is not working for you. –  Taco Hoekwater Jul 23 '11 at 6:33
    
TNX. It seems the best option would be to pass the name of the array to lua, else it would not know what to define. –  Louis Jul 24 '11 at 17:30
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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