MetaObj and Labels

From Wiki
Jump to navigation Jump to search

MetaObj is a system for high-level object-oriented drawing based on MetaPost (from the manual).

When using text labels in embedded MetaPost code within ConTeXt, the whole MetaPost code is processed in two runs. This causes problems with MetaObj, because it doesn't expect this. See this mailing list thread for details. The following example (from the manual, modified by Aditya Mahajan) shows how to avoid those problems.

\startMPinclusions[+]
  input metaobj;
  boolean flag; flag := false; % flag is false in the first run
\stopMPinclusions

\startMPdefinitions
  vardef newSegment@# =
    if not flag:
      assignObj(@#, "Segment"); % this should be called only once
      ObjPoint a, b;
      ObjCode "@#b - @#a = (1cm, 2cm)";
    fi;
  enddef;

  def drawSegment(suffix n) =
    draw n.a -- n.b;
    draw btex E etex shifted n.a;
  enddef;
\stopMPdefinitions

\starttext
\startMPpage
  newSegment.s;
  s.a = origin;
  drawObj(s);

  newSegment.t;
  if not flag:
    rotateObj(t, 180); % we don't want the object rotated twice
  fi;
  t.a = s.a + (4cm, 1cm);
  drawObj(t);

  flag := true; % flag will be true after the first run
\stopMPpage
\stoptext

Just use "if not flag" constructions around anything that might cause trouble. Sometimes even the definition of relations (bindings) between objects will require precautions.