1

I use a table z={}: the keys are names of points and the values are in my example coordinates.

Example : z.a = {0,1} I prefer this notation to the one z[a]. I try to keep the syntax as simple as possible knowing that in the end the keys will become coordinate names in TikZ.

So I tested z.O_0 = {0,0} no problem for the moment except a little later.

Then with Tikz it's possible to use a'. After reading the documentation, I realized that I could not use the sugar syntax with ., also I tested z["a'"]. This is correct but painful to write. New test ap = "a'" and as I expected z[ap] is accepted but surprise z.ap too.

-- First question: Why is z.ap accepted? ap has type "string" but here it is a variable ?

Now to simplify the syntax again I tried a = z.a b=z.b ap = z[ap] c_a = z.c_a and bp = z.bp with success. This seems logical because it points to the same table.

-- Second question: I would like to use one function like iso to get all equalities at once.

I tried something like:

for k,v in pairs(z) do
   k = ...
end

but I haven't found a solution.

Last problem when I wanted to test the storage of the coordinates. I didn't see this problem before because the code was injected into TikZ code but in this case you can use _ in the coordinate or node names. When simplifying the code to ask my question I saw that I had to use $$ to display something like c_a but now I have a problem to use $$ in tex.print.

I used z.c_a=nil to get it out of the loop

-- Last question: how to print all the keys in the for loop. I can't use \\_ as I did before.

\documentclass{article}
\usepackage{luacode}

\begin{document}
\parindent = 0pt
\begin{luacode}
tp=tex.print
z = {}
z.a = {5,2}
z.b = {3,-2}
z.c_a = {3,2}

ap = "a'"
bp = "b'"

tex.print("type of ap is :".. type(ap)) -- > string
tex.print('\\\\')

z[ap] = {99,0}   -- z."a'" = {1,1} -- > error it's ok
z.bp = {66,55} -- 
tex.print("x coordinate for z[ap] is ".. z[ap][1])
tex.print('\\\\')
tex.print("x coordinate for z.bp= \\{66,0\\} is ".. "z.bp[1] = " ..z.bp[1])
tex.print('\\\\')


bp = z.bp --  but  "a'" = z["a'"] is not possible
--tex.print(" ap works like :" .. z[ap][1])
tex.print('\\\\')

tp("Sugar syntax : a=z.a b=z.b ap =z[ap] bp=z.bp")
a=z.a
b=z.b
bp = z.bp
ap = z[ap]
c_a = z.c_a

tex.print('\\\\')
tp("Test with sugar syntax :"..'\\\\')
tp("y coordinate for a[2] is ".. "a[2]= "..a[2]) 
tp('\\\\')
tp("y coordinate for ap[2] is ".. "ap[2]= "..ap[2])
tp('\\\\')
tp("y coordinate with bp[2]= "..bp[2]) 
tp('\\\\')
tp("y coordinate with c\\_a[1]= "..c_a[1]) 
tp('\\\\')

tex.print("The stored coordinates are : "..'\\\\')
z.c_a = nil. -- when `k=c_a` $$ are required but `$k$`does not work
for k,v in pairs(z) do
   tp(k) tp(tostring(" represents :")) tp(tostring("(" .. v[1]..","..v[2]..")"))
  tex.print('\\\\')
end     
\end{luacode}
\end{document}

enter image description here

1 Answer 1

3

The site works best with one question per question:-) But to pick out what I think is the main confusion;

bp = z.bp

There there is no relation between the bp. The first is the variable bp, but z.bp is syntactic sugar for z["bp"] so accesses the table entry with key bp.

"a'" = z["a'"] is not possible

You can not assign anything to a string literal, so it would not matter what you put after the =

I saw that I had to use $$ to display something like c_a

Only if you want to display that as a math subscript. Here you just want c_a as a variable name, so use \texttt{\detokenize{c_a}} or similar.

Second question: I would like to use one function like iso to get all equalities at once.

I couldn't see what you wanted here.

1
  • I suspected the first answer but wanted to be sure. I'm going to separate the questions to be more precise and to follow the instructions. The code is common so I wanted to avoid repeating everything Commented Mar 6, 2023 at 10:43

You must log in to answer this question.

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