0

The next code gives the error: Missing $ inserted. This is because one of the keys is c_a. How to modify the loop to use c_a?

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{luacode}

\begin{document}
\parindent = 0pt
\begin{luacode}
tp=tex.print
z = {}

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

z.a = {5,2}
z.b = {3,-2}
z.c_a = {3,2}
z[ap] = {99,0}
z.bp = {66,55}

tex.print("The stored coordinates are : "..'\\\\')
-- z.c_a = nil
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}
4
  • I left them here but as commented in your last question it's confusing to use variable names matching your keys. Here you define bp but do not use it,whereas the similar ap variable is used to access the key a' Mar 6, 2023 at 11:28
  • @DavidCarlisle I agree it's confusing but this defines the points given to start my geometrical figures. For the user z["a'"],z["b'"] = o:set_rotation (math.pi/2,a,b)is simpler than z["a'"],z["b'"] = z.o:set_rotation (math.pi/2,z.a,z.b). For the moment I have not encountered any problem but ... However, the user is not obliged to do this and I do not use it in the main code! Mar 6, 2023 at 12:38
  • I'd use keys ap and bp so you can use . in all cases z.ap and z.bp using a key a' for a' and a key bp for b' is more confusing than you need, did you intend to treat these differently or is this just left from experiments for the last question? Mar 6, 2023 at 12:44
  • It was an experiment. I have to use the form z[ap] and z[bp] if I want to get a' and b' in my TikZ code. Mar 6, 2023 at 12:58

1 Answer 1

4

Not really related to Lua, you need \verb|c_a| or similar for the underscore

enter image description here

\documentclass{article}
\usepackage{luacode}

\begin{document}
\parindent = 0pt
\begin{luacode}
tp=tex.print
z = {}

ap = "a'"
bp = "b'"  -- not used

z.a = {5,2}
z.b = {3,-2}
z.c_a = {3,2}
z[ap] = {99,0}
z.bp = {66,55}

tex.print("The stored coordinates are : "..'\\\\')
-- z.c_a = nil
for k,v in pairs(z) do
   tp("\\verb|" .. k .."|") tp(" represents :") tp("(" .. v[1]..","..v[2]..")")
   tex.print('\\\\')
end     
\end{luacode}
\end{document}

You must log in to answer this question.

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