I've read More than one optional argument for newcommand and I chose the answer with xargs
. I would like to pass a second optional argument to the Lua function. But the first of the two commented \drawLUAline
lines leads to the error:
./d.tex:39: Package pgfkeys Error: I do not know the key '/tikz/midway {A}' and
I am going to ignore it. Perhaps you misspelled it.
Of course, I can here just add the second optional part as I did, but I want to know how to do this right for other cases. And for me the syntax with the double brackets for the optional arguments is not clear.
\documentclass[preview]{standalone}
\usepackage{xargs}
\usepackage{luacode}
\usepackage{tikz}
\begin{document}
\begin{luacode*}
function print_line (x, y, optA, optB)
-- optA: yes
if optA~=[[]] then
tex.sprint("\\draw["..optA.."] (0,0) -- ("..x..","..y..")")
end
-- optA: no
if optA==[[]] then
tex.sprint("\\draw (0,0) -- ("..x..","..y..")")
end
-- optB: yes
if optB ~=[[]] then
tex.sprint(""..optB.."")
end
end
\end{luacode*}
\newcommandx{\drawLUAline}[4][1, 4]{%
\directlua{print_line(#2,#3,[[#1]],[[#4]])}
}
\begin{tikzpicture}
\draw (0,0) grid (3,4);
\drawLUAline{3}{1};
\drawLUAline[red]{3}{2};
\drawLUAline[blue]{3}{2.5} node[midway]{A};
%\drawLUAline[green]{3}{3}[node[midway]{A}];
%\drawLUAline{3}{4}[node[midway]{A}];
\end{tikzpicture}
\end{document}
\drawLUAline{3}{1}{}{}
,\drawLUAline{3}{2}{red}{}
, and\drawLUAline{3}{3}{green}{"node[midway]{A}"}
.