Dear all, how can I check whether a macro I defined is actually expanded into what I intended? For example, if I define \define[2]\Test{#2, {\it #1}} is there a (Lua?) function like check(\Test{Hello}{world}, string) that would return true for string = "world, {\it Hello}" and false otherwise? If the macro is a bit more complicated than this I usually get caught by a mistake rather quickly ... so a quick check would be very handy for me. Many thanks for your help, Oliver
Am 04.11.2009 um 11:52 schrieb Oliver Buerschaper:
Dear all,
how can I check whether a macro I defined is actually expanded into what I intended? For example, if I define
\define[2]\Test{#2, {\it #1}}
is there a (Lua?) function like
check(\Test{Hello}{world}, string)
that would return true for string = "world, {\it Hello}" and false otherwise? If the macro is a bit more complicated than this I usually get caught by a mistake rather quickly ... so a quick check would be very handy for me.
You can check the content in ConTeXt itself. \define[2]\Test {\doifelse{#1}{world}{\writedirect{Test \#1: true}}{\writedirect {Test \#1: false}}% \doifelse{#2}{Hello}{\writedirect{Test \#2: true}}{\writedirect {Test \#2: false}}% #2, {\it#1}} Wolfgang
how can I check whether a macro I defined is actually expanded into what I intended? For example, if I define
\define[2]\Test{#2, {\it #1}}
is there a (Lua?) function like
check(\Test{Hello}{world}, string)
that would return true for string = "world, {\it Hello}" and false otherwise? If the macro is a bit more complicated than this I usually get caught by a mistake rather quickly ... so a quick check would be very handy for me.
You can check the content in ConTeXt itself.
\define[2]\Test {\doifelse{#1}{world}{\writedirect{Test \#1: true}} {\writedirect{Test \#1: false}}% \doifelse{#2}{Hello}{\writedirect{Test \#2: true}} {\writedirect{Test \#2: false}}% #2, {\it#1}}
This is perfect if the arguments of \Test are fixed once and for all. But actually I'd like to get something different ... (perhaps I shouldn't have called the macro \Test but rather \MyMysteriousMacro :-). Suppose I know what the output of this macro (after expansion) should yield for *various* inputs. Now I just want to check whether the macro really does what I expect it to do. For example: bool1 = check(\MyMysteriousMacro{Hello}{world}, "world, {\it Hello}") bool2 = check(\MyMysteriousMacro{Wolfgang}{How are you}, "How are you, {\it Wolfgang}") etc. Usually the macro would typeset something but here I'd just like this comparison with the supplied string (without anything ending up on paper). How would I do that? Many thanks, Oliver
Am 04.11.2009 um 15:10 schrieb Oliver Buerschaper:
how can I check whether a macro I defined is actually expanded into what I intended? For example, if I define
\define[2]\Test{#2, {\it #1}}
is there a (Lua?) function like
check(\Test{Hello}{world}, string)
that would return true for string = "world, {\it Hello}" and false otherwise? If the macro is a bit more complicated than this I usually get caught by a mistake rather quickly ... so a quick check would be very handy for me.
You can check the content in ConTeXt itself.
\define[2]\Test {\doifelse{#1}{world}{\writedirect{Test \#1: true}}{\writedirect {Test \#1: false}}% \doifelse{#2}{Hello}{\writedirect{Test \#2: true}}{\writedirect {Test \#2: false}}% #2, {\it#1}}
This is perfect if the arguments of \Test are fixed once and for all.
But actually I'd like to get something different ... (perhaps I shouldn't have called the macro \Test but rather \MyMysteriousMacro :-). Suppose I know what the output of this macro (after expansion) should yield for *various* inputs. Now I just want to check whether the macro really does what I expect it to do. For example:
bool1 = check(\MyMysteriousMacro{Hello}{world}, "world, {\it Hello}") bool2 = check(\MyMysteriousMacro{Wolfgang}{How are you}, "How are you, {\it Wolfgang}")
etc.
Usually the macro would typeset something but here I'd just like this comparison with the supplied string (without anything ending up on paper).
Like this? \newconditional\MysteriousOne \newconditional\MysteriousTwo \letempty\MysteriousListOne \letempty\MysteriousListTwo \definecomplexorsimple\MyMysteriousMacro \def\complexMyMysteriousMacro[#1]#2#3% {\doifelse{#1}{list} {\appendtocommalist{#2}\MysteriousListOne \appendtocommalist{#3}\MysteriousListTwo}% {\simpleMyMysteriousMacro{#2}{#3}}} \def\simpleMyMysteriousMacro#1#2% {\setfalse\MysteriousOne \setfalse\MysteriousTwo \ExpandFirstAfter\doifinset{#1}\MysteriousListOne{\settrue \MysteriousOne}% \ExpandFirstAfter\doifinset{#2}\MysteriousListTwo{\settrue \MysteriousTwo}% \ifconditional\MysteriousOne\ifconditional\MysteriousTwo #1, {\it#2}% \fi\fi} \MyMysteriousMacro[list]{world}{Hello} \MyMysteriousMacro[list]{Wolfgang}{how are you} \starttext \MyMysteriousMacro{Hello}{world} \MyMysteriousMacro{Wolfgang}{how are you} \stoptext Wolfgang
Like this?
Wow, this is quite involved...
\newconditional\MysteriousOne \newconditional\MysteriousTwo
\letempty\MysteriousListOne \letempty\MysteriousListTwo
\definecomplexorsimple\MyMysteriousMacro
\def\complexMyMysteriousMacro[#1]#2#3% {\doifelse{#1}{list} {\appendtocommalist{#2}\MysteriousListOne \appendtocommalist{#3}\MysteriousListTwo}% {\simpleMyMysteriousMacro{#2}{#3}}}
\def\simpleMyMysteriousMacro#1#2% {\setfalse\MysteriousOne \setfalse\MysteriousTwo \ExpandFirstAfter\doifinset{#1}\MysteriousListOne{\settrue \MysteriousOne}% \ExpandFirstAfter\doifinset{#2}\MysteriousListTwo{\settrue \MysteriousTwo}% \ifconditional\MysteriousOne\ifconditional\MysteriousTwo #1, {\it#2}% \fi\fi}
\MyMysteriousMacro[list]{world}{Hello} \MyMysteriousMacro[list]{Wolfgang}{how are you}
\starttext
\MyMysteriousMacro{Hello}{world} \MyMysteriousMacro{Wolfgang}{how are you}
\stoptext
Actually I don't fully grasp what the above code does in detail ... but from the output I would deduce that it checks whether the input was {Wolfgang}{how are you}. If yes, it prints the content of that original sample macro to the page, if no, its output is supressed. That's still not quite what I want. First, if I change the argument to {Wolfgang}{how are you2} then ConTeXt crashes (2009.06.13 13:37 MKIV). Second, I do *not* want to modify the macro itself. I also do *not* want to filter the arguments in the macro and do any conditional processing. I'd rather leave the macro definition as is (perhaps the macro isn't even my idea) and just check whether the text finally output on page is what I expect. This I could do by visual inspection, however, if there are things like unbreakable spaces involved, I wouldn't normally see them in the PDF output unless a line break occurs at the critical position. Now if I could get the output of the macro as a fully expanded stream of TeX commands I could programmatically check for the presence of that unbreakable space that my (or some other) macro is supposed to insert. Perhaps there's no easy solution for this task :-( Many thanks anyway, Oliver
participants (2)
-
Oliver Buerschaper
-
Wolfgang Schuster