2

Suppose I have three numerical variables a,b,c and I want to use them in TeX then I write :

  token.set_macro("a",a,'global')
  token.set_macro("b",b,'global') 
  token.set_macro("c",c,'global')

I would like not to have to write "a", "b" and "c" and if possible treat all cases together, something like f(a,b,c).

After some research I found a very heavy solution With debug.getlocal

Is it possible to find another solution?

Finally the question should be: if pi=3.14 how to write tex.print("pi" .."="..tostring(pi)) without duplicating pi

3

2 Answers 2

6

Not quite the form you asked for but it's easier to iterate over a table

\documentclass{article}

\directlua{
t={
 a=11,
 b=22,
 c=33
}
function f (t)
 for k,v in pairs(t) do
  token.set_macro(k,v,'global')
 end
end
f(t)
}

\show\a
\show\b
\show\c


\begin{document}

\end{document}

produces

> \a=macro:
->11.
l.17 \show\a
          
? 
> \b=macro:
->22.
l.18 \show\b
          
? 
> \c=macro:
->33.
l.19 \show\c
          
? 

showing all three macros are set.


Or if you want to use variables, iterate over the names

\documentclass{article}

\directlua{

a=11
b=22
c=33

function f (t)
 for k,v in pairs(t) do
  token.set_macro(v,_G[v],'global')
 end
end
f{"a","b","c"}
}

\show\a
\show\b
\show\c


\begin{document}

\end{document}
4
  • This is not really practical because if I don't write the name of the variable twice, I have to rewrite the value of the variable. Unfortunately a and b for example are the radii of an ellipse ... Commented Apr 12, 2023 at 9:43
  • 1
    @AlainMatthes not sure what you mean, in Lua you can more or less always use table entries rather than global variables if you wish. a global variable is just an entry in the _G table so there isn't really any difference. but otherwise use variables and a function call f("a","b","c"} and get the values from _G Commented Apr 12, 2023 at 9:56
  • @AlainMatthes I added a version using variables Commented Apr 12, 2023 at 10:02
  • Yes, this version is easier to use. It is a mix with the Skillmon one. Commented Apr 12, 2023 at 10:10
5

You could go the other way round: instead of trying to retrieve the variable name as a string, retrieve the variable value from a string.

\documentclass{article}

\directlua{
pi = 3.14
function print_debug(name)
  if _G[name] then
    tex.print(name.."=".._G[name])
  else
    tex.print(name.." not found")
  end
end
}

\begin{document}
\directlua{print_debug("pi")}
\end{document}

enter image description here

4
  • I'd prefer _ENV over _G since most variables shouldn't be global Commented Apr 12, 2023 at 9:17
  • @MaxChernoff yes, but this is a minimal example :) I'd even more prefer a module-specific table, or providing the module-table in which to search as a second argument (with global as default?).
    – Skillmon
    Commented Apr 12, 2023 at 9:19
  • @Skillmon How to proceed in the case of a, b and c? i.e. with a set of variables Commented Apr 12, 2023 at 10:02
  • @Skillmon Finally I have the solution in David' answer. Commented Apr 12, 2023 at 12:00

You must log in to answer this question.

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