I want to write some Lua code to calculate the total impedance of an electrical circuit. Here is my code:
\documentclass{article}
\usepackage{amsmath}
\usepackage{luacode} % for 'luacode' environment
\begin{luacode}
function calculate_impedance(R, L, C, f)
local omega = 2 * math.pi * f
local Z_R = R
local Z_L = 1i * omega * L
local Z_C = -1i / (omega * C)
local Z_total = Z_R + Z_L + Z_C
return Z_total
end
\end{luacode}
\begin{document}
\section{Introduction}
This document calculates the impedance of an electrical circuit with resistive, inductive, and capacitive components.
\section{Parameters}
\begin{itemize}
\item Resistance $ R = 100 \, \Omega $
\item Inductance $ L = 0.1 \, H $
\item Capacitance $ C = 10 \, \mu F $
\item Frequency $ f = 50 \, Hz $
\end{itemize}
\section{Calculation}
\begin{luacode}
R = 100 -- Resistance in ohms
L = 0.1 -- Inductance in henries
C = 10e-6 -- Capacitance in farads
f = 50 -- Frequency in hertz
Z = calculate_impedance(R, L, C, f)
\end{luacode}
The total impedance $ Z $ of the circuit is:
\begin{equation}
Z = \luacode{tex.print(string.format("%.2f + %.2fi", Z:real(), Z:imag()))}
\end{equation}
\section{Conclusion}
The impedance of the circuit is calculated successfully.
\end{document}
Compiler gives the following error:
I suspect you have forgotten a `}', causing me to read past where you wanted me to stop.
What is the wrong with my code?



1ias inlocal Z_L = 1i * omega * Lis invalid Lua code. Variables cannot start with numbers.iis the imaginary unit. I don't know how to manage complex numbers withlua, but writing1iis not the good way for that.