3

I used the code at How to conditionally draw parts of a MetaPost drawing in ConTeXt? to display a crossword puzzle. The code works great, until I try to display another crossword puzzle, at a later page in my book.

Here is a minimum working example:

\setvalue{1}{x}
\setvalue{2}{y}
\setvalue{1s}{1}
\setvalue{2s}{2}

\define\crossword{%
    \startMPcode
        string cell[];
        cell1 := "\getvalue{1}";
        cell2 := "\getvalue{2}";

        string super[];
        super1 := "\getvalue{1s}";
        super2 := "\getvalue{2s}";

        a = -10;
        b = 10;

        path box;
        box = unitsquare shifted -(1/2, 1/2) scaled 30;

        numeric x, y;
        y = 15;
        x = 15;
        for i=1 upto 2:
            if cell[i] <> "": 
                label(super[i], (x+a, y+b));
                label(cell[i], (x, y));
                draw box shifted (x, y);
            fi
            x := x + 30;
        endfor

        numeric x, y;
        y = -15;
        x = 15;
        for i=1 upto 2:
            if cell[i] <> "": 
                label(super[i], (x+a, y+b));
                label(cell[i], (x, y));
                draw box shifted (x, y);
            fi
            x := x + 30;
        endfor
    \stopMPcode

}

\starttext
    \crossword
    \crossword
\stoptext

If I delete the second \crossword, the script compiles, displaying my 2x2 crossword puzzle. If I put \crossword in a second time (the variables get changed so the content is different), then I get this error:

! Redundant equation.
<to be read again>
;
<*> ...r[]; super1 := "1"; super2 := "2"; a = -10;
b = 10; path box; box = u...
I already knew that this equation was true.
But perhaps no harm has been done; let's continue.

! Redundant equation.
<to be read again>
;
<*> ...er1 := "1"; super2 := "2"; a = -10; b = 10;
path box; box = unitsquar...
I already knew that this equation was true.
But perhaps no harm has been done; let's continue.

How can I display this MetaPost drawing more than once in the document?

4

The statements

a = -10;
b = 10;

are not assignments but equations which MetaPost will solve on-the-fly when you use a. Obviously, restating the same equation later in the document is redundant, hence the error message (MetaPost drawings are not scoped). One remedy to the problem would be to discard any previous solution before stating the equation, i.e.

save a, b;
a = -10;
b = 10;

but in my opinion this does not reflect your intention of using a and b as variables. The proper way to do it would be to declare them as numeric and use the assignment operator, i.e.

numeric a, b;
a := -10;
b := 10;

(the same applies for x and y, BTW)

Full MWE for completeness:

\setvalue{1}{x}
\setvalue{2}{y}
\setvalue{1s}{1}
\setvalue{2s}{2}

\define\crossword{%
    \startMPcode
        string cell[];
        cell1 := "\getvalue{1}";
        cell2 := "\getvalue{2}";

        string super[];
        super1 := "\getvalue{1s}";
        super2 := "\getvalue{2s}";

        numeric a, b;
        a := -10;
        b := 10;

        path box;
        box = unitsquare shifted -(1/2, 1/2) scaled 30;

        numeric x, y;
        y := 15;
        x := 15;
        for i=1 upto 2:
            if cell[i] <> "": 
                label(super[i], (x+a, y+b));
                label(cell[i], (x, y));
                draw box shifted (x, y);
            fi
            x := x + 30;
        endfor

        y := -15;
        x := 15;
        for i=1 upto 2:
            if cell[i] <> "": 
                label(super[i], (x+a, y+b));
                label(cell[i], (x, y));
                draw box shifted (x, y);
            fi
            x := x + 30;
        endfor
    \stopMPcode

}

\starttext
    \crossword
    \crossword
\stoptext

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.