0

Assuming that the following assignments a = z.a etc. are correct, is it possible to use a function iso to obtain them. I thought of the name "iso" because in my main code a is a point, z.a is an affix that represents the point.

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{tkz-euclide}
\usepackage{tkz-elements}

\begin{document}
\parindent = 0pt
\begin{elements}
z.a   = {5,2}
z.b   = {3,-2}
z.c   = {4,0}

-- instead of a = z.a b = z.b c = z.c
__ is it possible to do iso() with
-- function iso ()
--      for i,k in pairs(z) do
--          ????
--       end
-- end
\end{elements}

\end{document}
5
  • 1
  • as @user202729 just showed, it's sort of possible, but very weird and very fragile, what do you want to happen for keys with names that can't be used as variables, a' ? skip, give error, generate a variable you can not access? It will make the Lua very hard for any human to read as in all normal cases you can read the code statically to see where a variable is defined. It's possible to dynamcally alter the Lua symbol table, but not recommended for any normal use. Mar 6, 2023 at 14:32
  • @user202729 Too complicated and risky for a beginner like me with lua. I'm going to abandon this idea. Mar 6, 2023 at 14:55
  • @DavidCarlisle I am not going to do anything special. My geometry figures are based on points given at the beginning. Only three or four points are involved. No a', then these are points that are used to construct other points with tools like the ones I have in tkz-euclide. I already have a hundred examples and as many lua functions et methods that allow me to recreate with lua all my old examples. I will test again to decide if I use this possibility (but by hand and without macro). Mar 6, 2023 at 15:02
  • 1
    actually it's maybe not as bad as my orginal comment implied (I posted an answer) but it still seems scary, you can generate variables you can not access as variables, and can easily over-write anything Mar 6, 2023 at 15:06

1 Answer 1

1

You can access the global symbol table _G but I wouldn't recommend it here

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{luacode}
\begin{document}
\parindent = 0pt
\begin{luacode}
z={}
z.a   = {5,2}
z.b   = {3,-2}
z.c   = {4,0}
z["a'"] ={2,4}

-- instead of a = z.a b = z.b c = z.c



function iso ()
for i,k in pairs(z) do
 _G[i]=k
 end
end

iso()
\end{luacode}


% look at variable b

(\directlua{tex.print(b[1])},\directlua{tex.print(b[2])})


\end{document}
4
  • I saw _G from time to time in the documentations but I haven't studied it yet. If I understand correctly we have access to all global variables directly. Here the z table is global, can it be deleted by another program? Mar 6, 2023 at 15:24
  • @AlainMatthes yes (but z being global is not used here, it makes a b c and a' global variables Mar 6, 2023 at 15:25
  • New problem !! is it possible to protect for example z? Mar 6, 2023 at 16:01
  • 1
    @AlainMatthes no, a lua module is supposed to not use too much global namespace, if you only define tkz global variable as a table and all functions/variables are members of that module, it should not clash with others, but if you run the above and define a,b,c globally it is bound to clash Mar 6, 2023 at 16:10

You must log in to answer this question.

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