Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

The following code creates an array of boxes to represent an array.

\startMPinclusions
 input boxes ;
\stopMPinclusions

\starttext
\startluacode
function createGraphic(k,c,indH)
    local i=1
    local j=65
    context.startuseMPgraphic(name)
    context("boxjoin(a.se=b.sw; a.ne=b.nw);")
    while k[i] do
        bname=string.char(j)
        context("boxit."..bname.."(btex "..k[i].." etex)("..c[i]..");drawboxed("..bname..");")
        i=i+1
        j=j+1
    end
     context.stopuseMPgraphic()
    context.useMPgraphic(name)
end

a={51,31,4,22,23,45,23,43,54,22,11,34}
colors={"blue","blue","blue","blue","blue","blue","blue","blue","blue","white","white","white"}
createGraphic(a,colors)

The output is

Now I want to change the colors dynamically and call the createGraphic() in a loop. So I wrote the following function to change the colors array.

function refreshColors(yellowEndIndex,redIndex,blueIndex)
        print(yellowEndIndex..","..redIndex..","..blueIndex)
        local ccnt=1
        while a[ccnt] do
                if ccnt < yellowEndIndex then
                    colors[ccnt] = "yellow"
                elseif ccnt == redIndex then
                    colors[ccnt] = "red"
                elseif ccnt == blueIndex then
                    colors[ccnt] = "blue"
                else
                    colors[ccnt] = "white"
                end
                ccnt= ccnt+1
        end
end
refreshColors(2,4,6)
createGraphic(a,colors)

Now, the output is

Also when I call refreshColors() in loop and createGraphic() outside there is no error.

for i=1,10,1 do
    refreshColors(i,0,0)
end
createGraphic(a,colors)

Now the output is

However, when I call createGraphic() inside loop, error occurs.

for i=1,10,1 do
    refreshColors(i,0,0)
 createGraphic(a,colors)
end

The error message thrown by Texworks is

error on line 33 in file try.tex: terminal: ! Inconsistent equation (off by -1).

Line 33 is

colors[ccnt] = "red"

Here is the link to the boxes.mp used in the program.

I want to call the createGraphic() inside the loop. Why the error occurs??

share|improve this question
    
Welcome to TeX.sx!. You can refer to tex.stackexchange.com/editing-help for the markdown used in this site (for including links, etc). Unfortunately, you can only add displayed pictures once you have accumulated enough reputation. But higher rep users can include the pictures for you. Also, you can use backticks ` to mark your inline code as I did in my edit. –  hpesoj626 Feb 12 '13 at 4:37
    
The code is incomplete (at least \stopluacode, etc., is missing) and I haven't managed to recreate code using createGraphic() that runs without error. Could you post a proper MWE? I note that you are passing the uninitialised value name to startuseMPgraphic - this is a nil value where it should be a string, though I doubt that is the problem. –  Charles Stewart Feb 12 '13 at 12:08
add comment

1 Answer 1

up vote 4 down vote accepted

The problem must be with boxes.mp In the macro boxit, replace

@#col = cellColor;

with

@#col := cellColor;

This might make it work..

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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