Filtering a list with backwards regular expression

248 views
Skip to first unread message

Sara Beauregard

unread,
Jan 14, 2017, 4:58:40 PM1/14/17
to TiddlyWiki
In the project I'm working on, I'm using a lot of Latex equations and a lot of different variables in the equations so I wanted to create a template that would automatically pull in the tiddlers for the variables (tagged with "Variable") if they exist in the current tiddler field of "equation". My problem is that I can't seem to get regular expressions to work that way (backwards).

For example, I can create a list of equations a variable is in (equation tiddlers are tagged "Equation").

\define makePattern() 
^<.*?(?:=).*?(?:=).*?(?:$(var)$)
\end

</$set>
<$set name="var" value={{!!variable}}>
<$set name="varpattern" value=<<makePattern>>>
<$list filter="[tag[Equation]regexp:equation<varpattern>]">
<$transclude field="title"/>
<$transclude field="equation"/>
</$list>
</$set>
</$set>

However, I can't do the opposite, because what I would need to do is something like this but the longer string I am wanting to search into is the input into the regexp, not the object.

\define makePattern() 
^<.*?(?:=).*?(?:=).*?(?:$(var)$)
\end

<$set name="eq" value={{!!equation}}>
<$set name="eqpattern" value=<<makePattern>>>
<$list filter="[tag[Variable]regexp:variable<eqpattern>]">
<$transclude field="title"/>
<$transclude field="variable"/>
</$list>
</$set>
</$set>

Any ideas? Can regexp be used that way? I was thinking some combination of nested lists might help, and I tried my hand at creating a custom macro, but I haven't been able to figure this out.

Thanks,

Sara

Tobias Beer

unread,
Jan 15, 2017, 3:09:16 AM1/15/17
to TiddlyWiki
Hi Sara,

Can you give some precise examples as to what tiddlers tagged "Equation" or "Variable" look like, especially the relevant fields?

And based on those examples describe once more, which you want to see where?

From what I understand you wanted to show next to an Equation for a given variable (or all variables inside that equation?) at which other Equation tiddlers it is / they are also used inside the equation field... which begs the question: Are your variables actually unique that way throughout the wiki?

Not sure about your "other way around" though and what you want to achieve with it and what's actually in that variable field.

So, can you draft some pseudo-output of what you want displayed in the end and where?

Best wishes,

Tobias.

Sara Beauregard

unread,
Jan 15, 2017, 10:28:40 AM1/15/17
to TiddlyWiki

This is what the variable tiddler looks like once it's displayed. The code automatically found all the tiddlers tagged Equation that included actual vapor pressure in the "equation" field.


This is what an equation tiddler looks like. This is where I would like to include a list of all the tiddlers tagged "Variable" where the "variable' field of the tiddler is included in the "equation" field of the Equation tiddler.


The list of variables would look like this, but would only include the variables that are included in the equation.

My variables are unique through the tiddler. 


Thanks,


Sar

Tobias Beer

unread,
Jan 15, 2017, 10:35:56 AM1/15/17
to TiddlyWiki
Hi Sara,

I think there's one missing piece to your examples and it's the content / equation for evapotranspiration which — if I understand you correctly — is what you want to scan for any variables used. So, in order to do that, we'd need to see how the variables are  being used in that equation / or related to it.

Best wishes,

Tobias.

Tobias Beer

unread,
Jan 15, 2017, 10:40:56 AM1/15/17
to TiddlyWiki
Hi again,

Not sure if it's compatible to the current version of TiddlyWiki,
something like EucalyJ's matchfilter might do what you want.


Best wishes,

Tobias.

Sara Beauregard

unread,
Jan 15, 2017, 11:14:52 AM1/15/17
to TiddlyWiki
Sorry about the missing image, my toddler was trying to get me to play Legos as I was replying.


The actual text of the "equation" field in the "reference evapotranspiration" tiddler (tagged "Equation") is:
<$latex text="ET_0=ET_{wind}+ET_{rad}=\frac{0.408 \Delta (R_n-G)+\gamma (\frac{900}{T+273})u_2(e_s-e_a)}{\Delta+\gamma(1+0.34 u_2)}" displayMode="true"></latex>

The actual text of the "variable" field in the "actual vapor pressure" tiddler tagged "Variable" is:
e_a

So the code below works to return the equations that reference a variable. Since we can do a equation.match(".*?e_a.*?") (javascript format for example) type regex search. However, it doesn't work the other way, because if we do a variable.match(".*?<$latex text="ET_0=ET_{wind}+ET_{rad}=\frac{0.408 \Delta (R_n-G)+\gamma (\frac{900}{T+273})u_2(e_s-e_a)}{\Delta+\gamma(1+0.34 u_2)}" displayMode="true"></latex>.*?") it's going to return false. What I really want to do is another equation.match(".*?e_a.*?") for each of the variable tiddlers, but I can't figure out how to do that match format in the filter. It looks like the regexp filter expressions can only reference the field of the tiddler it's looking through for the object of the regular expression and can't use the field of the calling tiddler instead.


\define makePattern() 
^<.*?(?:=).*?(?:=).*?(?:$(var)$)
\end

</$set>
<$set name="var" value={{!!variable}}>
<$set name="varpattern" value=<<makePattern>>>
<$list filter="[tag[Equation]regexp:equation<varpattern>]">
<$transclude field="title"/>
<$transclude field="equation"/>
</$list>
</$set>
</$set>

I looked at the matchfilter plugin, but that doesn't solve the problem I am having. I did look at it as an example of how I might write my own plugin but I'm still learning javascript macro syntax and couldn't quite follow all the code.

Thanks,

Sara

Tobias Beer

unread,
Jan 15, 2017, 12:44:37 PM1/15/17
to TiddlyWiki
Hi Sara,

Ah, I see now. I guess, there's no easy way to identify what's a variable in LaTeX and what isn't, i.e. you're not using some <<varName>> or $(varName)$ pattern one could match.

So, yes the other way around, by looking up which existing variable is in your equation appears to be the more reliable approach.

Best wishes,

Tobias.

Tobias Beer

unread,
Jan 15, 2017, 1:03:59 PM1/15/17
to TiddlyWiki
Hi Sara,

Perhaps something like:

\define varPattern()
^<.*?(?:=).*?(?:=).*?(?:$(var)$)
\end

<$list filter="[has[variable]sort[title]]" variable="varTiddler">
<$set name="var" filter="[<varTiddler>get[variable]]">
<$list filter="[all[current]regexp:equation<varPattern>]">
<$tiddler tiddler=<<varTiddler>>>
<$link><$view field="title"/></$link>: <code><$text text=<<var>>/></code><br>
</$tiddler>
</
$list>
</$set>
</
$list>

...assuming all[current] resolves to your equation tiddler.

Best wishes,

Tobias.

Ste Wilson

unread,
Jan 15, 2017, 3:44:00 PM1/15/17
to TiddlyWiki
Hi Sara,

First thank you for a "ah ha" moment on how to put latex in fields so
http://stephenteacher.tiddlyspot.com/#To%20replace%20once%20%24%20is%20fixed.
Works, at least until the missing $ problem is solved (I really should mention on git hub that that's still happening).

Secondly...
I don't understand.
I've no clue what or why you are doing. Does it mean you have to do less latex typing, once the equation is done once..
Could should I take advantage of this technique in my tiddlywiki as it's equation heavy?

http://stephenteacher.tiddlyspot.com

What would be the advantage? Is it increased flexablity?
intrigued but puzzeled.

Ste


Tobias Beer

unread,
Jan 15, 2017, 4:23:44 PM1/15/17
to TiddlyWiki
Hi Ste,
 

What would be the advantage? Is it increased flexablity?
intrigued but puzzeled.


From what I understand, the idea is to show for each equation what variables are being used and for each variable in what equations they're being used. So, a pretty basic desire ...though with a huge turnout in terms of making relations more explorable.

Best wishes,

Tobias.

Sara Beauregard

unread,
Jan 15, 2017, 10:57:46 PM1/15/17
to TiddlyWiki
Thank you so much for your help, using some of the tricks you showed me, I got it to work:

\define varPattern() 
$(var)$
\end

{{!!equation}}

<table>
<tr><th>Variable</th><th>Description</th><th>Unit</th></tr>
<$set name="eq" value={{!!equation}}>
<$list filter="[tag[Variable]sort[title]]" variable="varTiddler">
<$set name="var" filter="[<varTiddler>get[variable]]">
<$set name="unit" filter="[<varTiddler>get[unit]]">
<$list filter="[tag[Equation]field:equation<eq>regexp:equation<varPattern>]">
<$tiddler tiddler=<<varTiddler>>>
<tr><th>
<$transclude field="variablelatex"/>
</th><th><$link><$transclude field="title"/></$link></th><th><$transclude field="unitlatex"/></th></tr>
</$tiddler>
</$list>
</$set>
</$set>
</$list>
</$set>
</table>

Yeah!!!

Thanks,

Sara

Tobias Beer

unread,
Jan 16, 2017, 7:01:44 AM1/16/17
to tiddl...@googlegroups.com
Hi Sara,

Great to see you got it working. Cool stuff.

If I get it right, I think your varPattern perhaps needs a little polishing.
At least, it should constrain matches by respecting word boundaries, e.g.:

\define varPattern()
\b$(var)$\b
\end

...so that a_b does not match a_b_c.

Best wishes,

Tobias.

Ste Wilson

unread,
Jan 18, 2017, 9:44:35 AM1/18/17
to TiddlyWiki
Hi sara,
I don't suppose you'd consider putting your Wiki on tiddlyspot or uploading an example so I could have a look so set up could you?
I kind of get what your doing but want to see how it works and then I can see if it's something which might be be benificial to my Wiki.

Ste

Reply all
Reply to author
Forward
0 new messages