3

I want to draw a rectangle with \luacode. For rectangle points, the coordinates values will be taken from code.

I try the following code. For rectangle, points are defined as '(0,0) and (1,1). But i want to pass the xmin, xmax, ymin, ymax values into the \tikzpicture code. When i changed the related lines as tp("\\draw [ultra thick] (xmin,ymin) rectangle (xmax,ymax);"), the error gives that

Package PGF Math Error: Unknown function `xmin' (in 'xmin').

How to pass the values? Any suggestions are welcome.

\documentclass{article}
\usepackage[utf8x]{inputenc}
\usepackage{luacode}
\usepackage{amsmath,tikz}

    \begin{document}
    \luaexec{
    tp = tex.print
    local xmin = 0
    local xmax = 1
    local ymin = 0
    local ymax = 1
    tp(xmin.." "..xmax.." "..ymin.." "..ymax)
    tp("\\\\")
    tp("\\begin{center}")
    tp("\\begin{tikzpicture}")
    tp("\\draw [ultra thick] (0,0) rectangle (1,1);")
    tp("\\end{tikzpicture}")
    tp("\\end{center}")
    }

    \end{document}
5

There is a different behavior between \luadirect & \luaexec commands and luacode & luacode* environments, please see page 2 in the luacode manual. This is your example taking an advantage of knowing that.

%! lualatex mal-lua-to-tikz.tex
\documentclass{article}
%\usepackage[utf8x]{inputenc}
%\usepackage{amsmath}
\pagestyle{empty}
\usepackage{luacode}
\usepackage{tikz}
\begin{document}
\begin{luacode*}
tp = tex.print
local xmin = 0.5
local xmax = 3.5
local ymin = 0.5
local ymax = 1.5
local printme="The xmin, xmax, ymin and ymax are: "..xmin..", "..xmax..", "..ymin..", and "..ymax.."."
print("\n"..printme)
tp(printme)
tp("\\par\\medskip")
--tp("\\begin{center}")
tp("\\begin{tikzpicture}")
--tp("\\draw [ultra thick] (0,0) rectangle (1,1);")% An old version...
tp("\\draw [ultra thick, draw] ("..xmin..","..ymin..") rectangle ("..xmax..","..ymax..");")
tp("\\end{tikzpicture}")
--tp("\\end{center}")
\end{luacode*}
\end{document}

mwe

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.