为什么这个 MetaPost 绘图不能在 ConTeXt 中放置多次?

为什么这个 MetaPost 绘图不能在 ConTeXt 中放置多次?

我使用的代码如何有条件地在 ConTeXt 中绘制 MetaPost 图形的各个部分?显示填字游戏。代码运行良好,直到我尝试在书中的后面一页显示另一个填字游戏。

这是一个最小工作示例:

\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

如果我删除第二个\crossword,脚本会编译,并显示我的 2x2 填字游戏。如果我输入\crossword第二个(变量发生变化,因此内容不同),则会出现此错误:

! 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.

我怎样才能在文档中多次显示此 MetaPost 绘图?

答案1

声明

a = -10;
b = 10;

不是作业,而是 MetaPost 在使用时会即时求解的方程a。显然,在文档后面重新陈述相同的方程是多余的,因此会出现错误消息(MetaPost 绘图不在范围内)。解决该问题的一种方法是放弃任何先前的解决方案,然后再陈述方程,即

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

但在我看来,这并不反映你使用ab作为变量的意图。正确的做法是将它们声明为numeric并使用赋值运算符,即

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

x(顺便说一下,和也同样适用y

完整的 MWE 完整性:

\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

相关内容