Hi,

I want to make a proper definition of a new command to be able to
process it in Lua. I found something on StackOverflow [1] but that
breaks in enumeration _and_ when interaction is enabled and I do not
understand why? Can you give me some help here?

I want to make commands like \foo{bla} and \foo[b]{bla} possible at the
same time. Here is a minimal example (also attached):

```
\startluacode
function userdata.my_command(keywords, text)
        commands.writestatus("test", string.format("text: %s, config: %s", 
text, keywords))
    local cfg = utilities.parsers.settings_to_hash(keywords)

        if cfg['emph'] ~= nil then
                context("\\emph{" .. text .. "}")
        elseif cfg['bold'] ~= nil then
                context("\\bold{" .. text .. "}")
        else
                context(text)
        end
end
\stopluacode

% without that line there is no problem
\setupinteraction[state=start]

\defineenumeration[enu][text=Enu]

\unprotect
\def\mc{\dosingleempty\do_my_command}
\def\do_my_command[#1]#2{%
        \iffirstargument{%
                \ctxlua{userdata.my_command('#1', [==[#2]==])}%
        }\else {%
                \ctxlua{userdata.my_command('', [==[#2]==])}%
        }\fi
}
\protect

\starttext

% all the following works
\mc{foo} \mc[bold]{foo} \mc[emph]{foo}

\startenu[title=Some]
\stopenu

% this breaks
\startenu[title=Some \mc{foo}]
\stopenu

\stoptext
```

The compilation fails with:
```
tex error       > tex error on line 41 in file ./test.tex: Use of 
\do_my_command doesn't match its definition
```

Best,
Gerion

[1] https://tex.stackexchange.com/a/55778
\startluacode
function userdata.my_command(keywords, text)
	commands.writestatus("test", string.format("text: %s, config: %s", text, keywords))
    local cfg = utilities.parsers.settings_to_hash(keywords)

	if cfg['emph'] ~= nil then
		context("\\emph{" .. text .. "}")
	elseif cfg['bold'] ~= nil then
		context("\\bold{" .. text .. "}")
	else
		context(text)
	end
end
\stopluacode

% without that line there is no problem
\setupinteraction[state=start]

\defineenumeration[enu][text=Enu]

\unprotect
\def\mc{\dosingleempty\do_my_command}
\def\do_my_command[#1]#2{%
 	\iffirstargument{%
		\ctxlua{userdata.my_command('#1', [==[#2]==])}%
	}\else {%
		\ctxlua{userdata.my_command('', [==[#2]==])}%
	}\fi
}
\protect

\starttext

% all the following works
\mc{foo} \mc[bold]{foo} \mc[emph]{foo}

\startenu[title=Some]
\stopenu

% this breaks
\startenu[title=Some \mc{foo}]
\stopenu

\stoptext

Attachment: signature.asc
Description: This is a digitally signed message part.

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : ntg-context@ntg.nl / 
https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki     : https://wiki.contextgarden.net
___________________________________________________________________________________

Reply via email to