1

My apologies in advance for what is probably a trivial issue.

I have been trying to debug this MWE for a while now, and despite my best efforts (and an old MWE that still works to go off of), I was still unable to get it to work.

The following code should make a black circle, but it is erroring. Different errors occur depending on the debugs I put, so I don't know which one to copy.

sometimes I tostring() the metapost.getparameterset too. Worked before, and still does on the old MWE. I must be missing something.

\startluacode
local function sse(str)
    if type(str) ~= "string" then
        error("single_string_expression: expected a string, got " .. type(str) .. " (" .. tostring(str) .. ")")
    end
    local chunk, err = load(("return %s"):format(str), "expression", "t", math)
    if not chunk then
        error("Failed to parse expression: " .. tostring(str) .. "\nError: " .. tostring(err))
    end
    local ok, result = pcall(chunk)
    if not ok then
        error("Error evaluating expression: " .. tostring(result))
    end
    return result
end

function mp.test()
    local x = sse(metapost.getparameterset("x"))
    mp.print(("fill fullcircle shifted (%f,0) scaled 1cm ;"):format(x))
end
\stopluacode

\startMPdefinitions
presetparameters "set_test" [x="5"] ;
def test = 
    applyparameters 
    "set_test" 
    "do_set_test" 
enddef ;
vardef do_set_test =
    pushparameters "set_test" ;
    lua.mp.test() ;
    popparameters ;
enddef ;
\stopMPdefinitions

\starttext
\startMPpage
test[] ;
\stopMPpage
\stoptext
6
  • 2
    I’m sorry if I’m missing the point, but in the “Handle triangle segments” section, you’re passing segment.filloptions as the first argument, whereas the format string expects six numbers followed by a string at the end. I don’t rule out that this could cause an error. Commented Oct 31, 2025 at 3:38
  • feel free to roll back the format change if you don't like it - I'm not sure I do. Commented Oct 31, 2025 at 3:43
  • the string at the end won't error, but the first %f should if segment.filloptions is not a number. Commented Oct 31, 2025 at 3:49
  • @kabenyuk yes that is definitely a problem. still doesn't seem to get it running though. Commented Oct 31, 2025 at 4:17
  • I'm pretty sure that the code is supposed to create a circle, not a parallelogram. Please feel free to revert the edit if this is incorrect. Commented Oct 31, 2025 at 6:30

1 Answer 1

2

We can further reduce your MWE to the following:

\startluacode
    function mp.test()
        print("\nA test message.")
        mp.print("draw (0, 0) -- (100, 10);")
    end
\stopluacode

\startMPpage
    def test =
        applyparameters "set_test" "do_set_test"
    enddef ;

    vardef do_set_test =
        lua.mp.test() ;
    enddef ;

    test[] ;
\stopMPpage

There are two problems:

  1. ConTeXt still parses backslashes inside \startluacode, so it sees the macro \nA, which is undefined.

  2. The string test in lua.mp.test() is getting replaced by do_set_test due to the test macro. This seems like a ConTeXt bug to me.

Luckily, the solutions to both are fairly simple:

  1. Place a non-letter after \n so that ConTeXt only sees the macro \n, which will be correctly converted to Lua.

  2. Use a different name for the MetaPost macro and the Lua function.

\startluacode
    function mp.test_()
        print("\n" .. "A test message.")
        mp.print("draw (0, 0) -- (100, 10);")
    end
\stopluacode

\startMPpage
    def test =
        applyparameters "set_test" "do_set_test"
    enddef ;

    vardef do_set_test =
        lua.mp.test_() ;
    enddef ;

    test[] ;
\stopMPpage

output

8
  • 1
    Let me test this out real quick Commented Oct 31, 2025 at 6:34
  • 1
    My main file does not contain these bugs because it uses a separate lua file, and uses unique names, unfortunately, these bugs arose in the creation of the MWE. I mis-posed the question apparently. Commented Oct 31, 2025 at 6:38
  • I'm getting luatex warning > mplib: run script: lua-mf3dtools-implementation.lua:1317: Failed to parse expression: table: 000004d8e5bef960 Error: [string "expression"]:1: malformed number near '000004d8e5bef960' with my current code. Commented Oct 31, 2025 at 6:39
  • @Jasper That looks pretty close to the output of print(load(tostring({}):gsub("x", ""))), so I'm guessing that you're calling tostring on a table somewhere. Commented Oct 31, 2025 at 6:44
  • 1
    (Maybe using vardef instead of def?) Commented Oct 31, 2025 at 9:12

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.