Define a new command that inherits from multiple other command options
hi! I'm new to ConTeXt. I want to define a command with sidenote function. This [setupsidenote] command inherits the options of [setupmargindata], [setupcounter] and [setupframed]. just like [setupenumeration] inherits the option of [setupcounter]. I've defined the following sidenote command by searching, and I know how to define a new option for it. But what I don't know is how to get it to inherit the options of other commands and perform these features correctly. Any clue is warmly welcome. ``` % macros=mkvi \unprotect \installnamespace {sidenote} \installcommandhandler \????sidenote {sidenote} \????sidenote \setupsidenote[ align=, conversion=n, way=bytext, style=, ] \definecounter [SidenoteMarkNumber] [prefix=no] \definemargindata [SidenoteContent] [outer] [margin=margin,width=\outermarginwidth] \setupmargindata [SidenoteContent] [stack=yes,style=\it] \def\sidenotemark{\rawcountervalue[SidenoteMarkNumber]} \def\convertsidenotemark#1{\high{\convertnumber{#1}{\sidenotemark}}} \def\sidenote{\dosingleempty\dosidenote} \def\dosidenote[#1]#2{% \getparameters[SMsidenote] [align=\sidenoteparameter{align}, conversion=\sidenoteparameter{conversion}, way=\sidenoteparameter{way}, #1]% \setupcounter [SidenoteMarkNumber] [#1] \incrementcounter[SidenoteMarkNumber] \convertsidenotemark{\SMsidenoteconversion}% mark in text \SidenoteContent[#1]{\setscript[hanzi]\setup[\SMsidenotealign]% \convertsidenotemark{\SMsidenoteconversion}% mark with note #2 }} \protect \starttext 天地玄黄,宇宙洪荒。日月盈昃,辰宿列张。寒来暑往,秋收冬\sidenote[align=flushleft] {天地玄黄,宇宙洪荒。} \stoptext ```
On 25 Apr 2024, at 09:24, ai2472206007@yeah.net wrote:
But what I don't know is how to get it to inherit the options of other commands and perform these features correctly.
Your example didn't work for me - none of the Chinese|Japanese[1] characters showed. AFAIK there is no way to "inherit" functionality. You still need to write \setupsidenote to call \setupcounter, \setupframed at the right points to achieve the right effects. What you can do is have \setupsidenote accept all options in one big long list and then pass the ones that apply to setupcounter to each use of \setupcounter, pass on the ones that apply to setupframed to each use of \setupframed and so on. Thsi is releatively easy in Lua where you could define a list of all possible commands and which macro they apply to then loop through it looking to see if that option had been supplied and building a set of parameters for \setupframed, \setupcounter etc. Hope this makes sense. But I feel there's probably a better way - it's a lot of effort to go to in order to have a single "does it all macro" instead of calling two or three macros each time. Regards, [1] Apologies for my ignorance. — Bruce Horrocks Hampshire, UK
Thanks for your reply. The kanji part requires a specific font and typescript file, which I defined on my own computer but forgot to add to this example. This is something I didn't take into account. It's true, I need to think about what exactly my command needs, not build an all-powerful command. It's my poor consideration.
ai2472206007@yeah.net schrieb am 25.04.2024 um 10:24:
hi!
I'm new to ConTeXt. I want to define a command with sidenote function. This [setupsidenote] command inherits the options of [setupmargindata], [setupcounter] and [setupframed]. just like [setupenumeration] inherits the option of [setupcounter].
I've defined the following sidenote command by searching, and I know how to define a new option for it. But what I don't know is how to get it to inherit the options of other commands and perform these features correctly.
Any clue is warmly welcome.
Hi ???, not all commands provide a way to inherit their options but the "framed" and "counter" mechanism supports it. To use the framed mechanism with your own command replace \installcommandhandler with \installframedcommandhandler. This creates a command named \inherited...framed which can be customized with the setups of the new namespace. %%%% begin example \unprotect \installnamespace {sidenote} \installframedcommandhandler \????sidenote {sidenote} \????sidenote \protect \starttext \inheritedsidenoteframed{Text in a frame!} \setupsidenote[framecolor=red,width=8cm,height=2cm] \inheritedsidenoteframed{Text in a frame!} \stoptext %%%% end example To use the counter mechanism with your commands you have to first use \installcounterassociation to create the two new commands \register...counter (this ensures the default counter values are used when you don't set anything) and \synchronize...counters (which updates the counter values when you use the setup of your own command). Unlike the framed mechanism this no longer works with the root instance of your own commands because we create a new counter only when you create a instance (with \define...) of the new command. %%%% begin example \unprotect \installnamespace {sidenote} \installcommandhandler \????sidenote {sidenote} \????sidenote \installcounterassociation{sidenote} \appendtoks \registersidenotecounter\currentsidenote \definecounter[\currentsidenote]% \to \everydefinesidenote \appendtoks \synchronizesidenotecounters \to \everysetupsidenote \protect \starttext % create a new sidenote instance with a associated counter \definesidenote[example] \start % use grouping to keep the sidenote instance local \def\currentsidenote{example}% set the sidenote instance to example \incrementcounter[example]% increment the example/sidenote counter \convertedcounter[example]% print the example/sidenote counter \stop % change the number format of the example/sidenote counter \setupsidenote[example][numberconversion=romannumerals] \start \def\currentsidenote{example} \incrementcounter[example] \convertedcounter[example] \stop \stoptext %%%% end example Wolfgang
Thanks for the detailed answer, my problem was solved very well through the code you provided ------ Muyik
participants (3)
-
ai2472206007@yeah.net
-
Bruce Horrocks
-
Wolfgang Schuster