[tex-k] a new solution to the Bernshtein trick on page 299 of The METAFONTbook

胡亚捷 (Hu Yajie) 2500418497 at qq.com
Tue Jan 19 06:12:34 CET 2021


Page 299 of The METAFONTbook shows a trick to extend METAFONT's syntax
to support Bernshtein polynomials. Knuth comments that his solution

    seems to work in all cases except with respect to formulas that
    involve `]]' (two consecutive brackets); the latter token, which
    plain METAFONT expands to `] ]', is not expanded when `lookahead'
    reads its text argument, hence the user must remember to insert
    a space between consecutive brackets.

Today I realized that I could further improve my solution so that it
worked not only with `flex' and `show' but also with `]]', by changing 
`[' and `]' from delimiters to macros:

---------------------------------------------------------------------
let [[[ = [; let ]]] = ];
def [ = exitif numeric begingroup for u = enddef;
def ] = , hide(N_ := 0; let v_ = \):
  if incr N_ = 1: def v_ = u enddef
  else: expandafter def expandafter v_ expandafter = v_, u enddef
  fi; endfor endgroup; if N_ < 3: [[[v_]]] else: Bernshtein N_ fi
  enddef;
primarydef t Bernshtein nn = begingroup N_[[[1]]] := 1;
 for n = 1 upto nn - 1: N_[[[n + 1]]] := t * N_[[[n]]];
  for k = n downto 2: N_[[[k]]] := t[[[N_[[[k]]], N_[[[k - 1]]] ]]];
  endfor N_[[[1]]] := (1 - t) * N_[[[1]]]; endfor
 N_ := 0; for u = v_: + N_[[[incr N_]]] * u endfor endgroup enddef;
---------------------------------------------------------------------

The new solution seems to be backward compatible with existing fonts
like cmr10, cmtt10, manfnt, cmman, random, and mfman. Furthermore,

a1=2;
a2=4;
show a[a[1]];

now prints `4' instead of giving an error.

Can you forward this to Knuth? Since he asks "Perhaps there is a simpler
way?" before giving his solution, he might be interested in this.

----

Previous reports:
https://github.com/dine2014/texbook-mfbook-bugs/blob/master/bugs.md



More information about the tex-k mailing list.