2

MWE:

\directlua{%
  local abs=math.abs
  function gcd(x, y)
    if y \csstring\~= 0 then
      return gcd(y, x \csstring\% y)% I would like to write "≠" instead of "~=".
    else
      return abs(x)
    end
  end}

\directlua{tex.print(gcd(768,640))}% prints 128

\bye

Is there a way to write instead of ~= (or instead of >= inside directlua{...}? This would improve the readability.

Thanks.

4
  • 4
    anything is possible but for someone who can read Lua (rather than read mathematics) I'm not sure it would help? (just replace directlua by a macro that does some lua pattern replace to replace the uniocde by legal lua syntax before passing to \directlua Commented Mar 5, 2021 at 18:15
  • 3
    What you ask for is preposterous.
    – user226564
    Commented Mar 5, 2021 at 18:44
  • If I run your example I get (./aa035.tex[\directlua]:1: attempt to call a nil value (global 'abs') Commented Mar 5, 2021 at 21:09
  • 1
    @JairoA.delRio booo Commented Mar 5, 2021 at 21:28

1 Answer 1

9

enter image description here


{
\endlinechar=-1
\catcode`\≠\active
\catcode`\%\active
\gdef\zzz{\bgroup
\catcode`\≠\active\edef≠{\string~=}
\catcode`\%\active\edef%{\string%}
\zzzz}
\gdef\zzzz#1{\directlua{#1}\egroup}
}

\zzz{
  local abs=math.abs
  function gcd(x, y)
    if y ≠ 0 then
      return gcd(y, x % y)
    else
      return abs(x)
    end
  end}

\directlua{tex.print(gcd(768,640))}% prints 128

\bye
5
  • 3
    +1 – Wow! But: why???
    – DG'
    Commented Mar 5, 2021 at 22:03
  • 4
    @DG' because I get 45 points closer to passing egreg? Commented Mar 5, 2021 at 22:04
  • Well, fair enough ;)
    – DG'
    Commented Mar 5, 2021 at 22:09
  • I have the impression this will break things somewhere else, but who am I to judge it?
    – user226564
    Commented Mar 5, 2021 at 22:44
  • 1
    @JairoA.delRio not really: the definitions of the symbols are local to the argument of \zzz (or course like any verb-like command you can't use this inside another command, but other than that it is safe enough) Commented Mar 5, 2021 at 22:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .