What I am trying to accomplish is the calculation of a length in cm, which I need for a TikZ drawing. The calculation itself is not trivial (cubic root, which I turned into math.log and math.exp), thus I have decided to use Lua, which will also allow me to define the number of decimal points through string.format. Out of this calculation, I would like to return a LaTeX length definition in cm.
Now I have finally arrived at a half-baked solution, that seems to set the length but for some reason the length dbe is still as zero. As per MWE, I am printing the command to the Log as well, see print(tmp) for debugging and if I Ctrl+C/Ctrl+V this output directly, the length definition works. Where am I going wrong with printing/returning the commands back to LaTeX?
\documentclass{article}
\usepackage{luacode}
\usepackage{tikz}
\usetikzlibrary{fadings,shapes,calc}
% Define a variable as a length
\newcommand{\nvar}[2]{%
\newlength{#1}
\setlength{#1}{#2}
}
% Define a few constants for transmission line drawing
\nvar{\rx}{0.33333cm}
\nvar{\diameter}{0.6cm}
\nvar{\db}{2cm}
% calculate some other dimensions (cubic root using ln and e)
\begin{luacode}
rx=0.33333
db=2
dbe= math.exp(math.log(rx*db*db)/3)
tmp = "\\newlength{\\dbe}\\setlength{\\dbe}{" .. string.format("%.2f", dbe) .. "cm}"
print(tmp)
tex.print(tmp)
\end{luacode}
\begin{document}
The \LaTeX lengths:\\
\the\rx\\
\the\diameter\\
\the\db \\
\the\dbe
\begin{tikzpicture}
\colorlet{left}{black}
\tikzset{line core/.style={draw=red,ultra thick, circle,fill=orange,minimum height=\dbe,anchor=center},%
line conductor/.style={shading = axis,circle, left color=left, right color=left!30!white,shading angle=180, anchor=center, minimum height=\dbe}
}
% looping through available phases
\foreach \phase/\centreX/\centreY in{1/0/0,2/4/0,3/2/3}
{
\coordinate (centre\phase) at (\centreX,\centreY);
\node [line core] at (centre\phase) {};
% drawing a phase transmission line - need to add at least three or four conductors
\foreach \angle in {90,30,150}
{
\node [line conductor] at ($ (centre\phase) + (\angle:\diameter)$) {};
\node [line conductor] at ($ (centre\phase) + (-\angle:\diameter)$) {};
}
}
\end{tikzpicture}
\end{document}
The output I get through print(tmp) in the log is:
\newlength{\dbe}\setlength{\dbe}{1.10cm}
edit: using MacTeX/TeXLive 2015 with all the latest packages through tlmgr
