Compare contents of macros via \doif etc. alternatives
Hello ConTeXist. Is there any way to evaluate of contents of two macros as I am showing in my minimal example? I know, that problem is in expansion, but I dont know to resolve it. My minimal example consist of piece lua code, because I am solving my problem in mixed codes (TeX & Lua) Thanx for help. Jaroslav Hajtmar \starttext \startluacode interfaces.definecommand ("domymacro", { arguments = { { "option", "string" } }, macro = function (opt_1) if #opt_1>0 then context(opt_1) else context("123") end end }) interfaces.definecommand("mymacro", { macro = function () context.dosingleempty() context["domymacro"]() end }) \stopluacode \def\myothermacro{123} \hairline \mymacro[123] \mymacro \myothermacro \hairline \doifelse{\mymacro}{\mymacro[123]}{equal content}{not equal content} \doifsamestringelse{\mymacro}{\mymacro[123]}{equal content}{not equal content} \doifinstringelse{\mymacro}{\mymacro[123]}{equal content}{not equal content} \doifincsnameelse{\mymacro}{\mymacro[123]}{equal content}{not equal content} \hairline \doifelse{\mymacro}{\myothermacro}{equal content}{not equal content} \doifsamestringelse{\mymacro}{\myothermacro}{equal content}{not equal content} \doifinstringelse{\mymacro}{\myothermacro}{equal content}{not equal content} \doifincsnameelse{\mymacro}{\myothermacro}{equal content}{not equal content} \stoptext
On Fri, 2017-07-21 at 11:25 +0000, Jaroslav Hajtmar wrote:
Something similar has been asked before and it is not possible. The interfaces.definecommand function defines a new macro using \protected\def. To compare as equal in a \doif context, both operands have to _expand_ to the same thing (here 123). Here \mymacro is not expandable and thus only \doifelse{\mymacro}{\mymacro} will ever compare true. Furthermore, scanning for optional arguments (i.e. [...]) is inherently non-expandable. To get something expandable you must at least switch to mandatory arguments (i.e. {...}). Still, using interfaces.definecommand will still not be possible. I believe that scanning arguments is better done on the macro level than in Lua, but that's just my opinion.
Thanks to Henri for the answer. To I admit, I thought it a bit technically that it would not be technically possible. At least I know why I was tearing my teeth while trying to test ...
One more thanx.
Jaroslav Hajtmar
Dne 22.07.17 3:58, ntg-context za uživatele Henri
On 7/22/2017 3:58 AM, Henri wrote:
also, it depends on what you want to achieve ... maybe comparing macros is not needed at all
there is not that much benefit defining these at the lua end if you directly feed back into tex anyway
-- ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------
Thanx Hans for reply.
also, it depends on what you want to achieve ... maybe comparing macros is not needed at all
You have right, that anytime compares are not needed, but for my very needed compares is not working in next eight examples. Problem is in expansions macros with optional argument. For “normal” macros are compares without problem as you see in last four examples….
mymacro
\doifelse{\mymacro}{123}{equal content}{not equal content}
\doifsamestringelse{\mymacro}{123}{equal content}{not equal content}
\doifinstringelse{\mymacro}{123}{equal content}{not equal content}
\doifincsnameelse{\mymacro}{123}{equal content}{not equal content}
\hairline
mymacro[123]
\doifelse{\mymacro[123]}{123}{equal content}{not equal content}
\doifsamestringelse{\mymacro[123]}{123}{equal content}{not equal content}
\doifinstringelse{\mymacro[123]}{123}{equal content}{not equal content}
\doifincsnameelse{\mymacro[123]}{123}{equal content}{not equal content}
\hairline
myothermacro
\doifelse{\myothermacro}{123}{equal content}{not equal content}
\doifsamestringelse{\myothermacro}{123}{equal content}{not equal content}
\doifinstringelse{\myothermacro}{123}{equal content}{not equal content}
\doifincsnameelse{\myothermacro}{123}{equal content}{not equal content}
there is not that much benefit defining these at the lua end if you directly feed back into tex anyway
sure, but I need define some macros automatically from luacode (is part of custom lua library)
Thanx
Jaroslav Hajtmar
22. 7. 2017 v 13:23, Hans Hagen
On 7/23/2017 8:46 AM, Henri Menke wrote:
Hans ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------
participants (4)
-
Hans Hagen
-
Henri
-
Henri Menke
-
Jaroslav Hajtmar