Passing the value of a field to a macro

I am embarrassed to ask this as I am sure it’s a simple question with a straightforward answer but I have exhausted my capabilities and myself.

I have a number of tiddlers with a field called molecular_formula whose value is the molecular formula of a chemical compound (e.g. C6H12O6). A little while ago I became aware of the mol-mass plugin created by @jerojasro so I thought it would be a grand idea to use it to calculate the molecular mass of a compound from the value in the molecular_formula field.

I hoped it would be as simple as <<molmass {{!!molecular_formula}}>> but that didn’t work. Neither did the $macrocall method as discussed in How can I pass the value of the a field or text into a macro. Looking at https://tiddlywiki.com/#Macros didn’t help me either. I was reduced to trying all sorts of (random) combinations of ' " "" """" { } [ ] ( ) < > __ etc. to no avail (inevitably). I have failed…

As ever, any pointer to a solution are greatefull received…

To pass a parameter value that comes from a field, you need to use the $macrocall widget, which requires named parameters.

Examining the macro source code (see $:/plugins/jerojasro/molmass/macros.js) shows that the macro supports two named parameters, formula and verbose.

Try this:

<$macrocall $name="molmass" formula={{!!molecular_formula}}/>

or

<$macrocall $name="molmass" formula={{!!molecular_formula}} verbose="yes"/>

Note:
As of TW5.3.0, you can also use the $transclude widget to invoke a macro, like this:

<$transclude $variable="molmass" formula={{!!molecular_formula}}/>

enjoy,
-e

2 Likes

Many thanks for this Eric… all your examples work perfectly.

I had tried $macrocall and thought I’d need named parameters but I was stuggling to know where to find them.

I’ve just updated to TW5.3.5 (from 3.2.5, I think) so I’ll probably go with the $transclude method. Of course, this just threw a single line red error when I tried it befoe updating.

Take care.