Rewriting this macro as a procedure

I defined this macro:

\define scaled-nutrient(nutrient)
<$text text={{{ [{##$nutrient$}divide<ref-qty>multiply<cur-qty>precision[3]] }}}/>
\end

then I found out about procedures and that they should be preferred. Now I’d like to rewrite the macro as a procedure, however there’s an issue: {##<<nutrient>>} is not valid filter syntax.

How am I supposed to to this?

In a filter, a transcluded field/index value is essentially a shortcut for <currentTiddler>get[fieldname] or <currentTiddler>getindex[indexname]. So you could define an equivalent procedure like this:

\procedure scaled-nutrient(nutrient)
<$text text={{{ [<currentTiddler>getindex<nutrient>divide<ref-qty>multiply<cur-qty>precision[3]] }}}/>
\end

In this particular case, since the only content of your macro/procedure is a text widget, I’d actually recommend using a function instead. Functions are specifically designed to return the plain text result of a filter, so you can eliminate the text widget. Try this:

\function scaled-nutrient(nutrient)
[<currentTiddler>getindex<nutrient>divide<ref-qty>multiply<cur-qty>precision[3]]
\end

You can use the function the same way you’d use the corresponding macro: <<scaled-nutrient nutrientName>>

2 Likes

Works like a charm, thank you!

Very nice solution @etardiff.
Using procedures/functions the script is more semantic, and simpler to understand!

The old style: {##$nutrient$} is really confusing!

1 Like