This is a little discussion about a method that might get you at least closer to where you want.
The
problem with the implementation of Katex on TW5 is the choice of $$ to
indicate latex is about to begin. This causes all sorts of havoc with
the TW macros, because TW macros use $ and $( to indicate the start of a
parameter. But, if you make a couple very tiny changes to the Katex
plugin, you could use, for instance, %% to indicate the start of latex
formatting.
Once you make this change, you can then write a macro like this:
\define latex(text:"",pi:"\pi",xi:"\xi",infty:"\infty",int:"\int",hat:"\hat")
%%
$text$
%%
\endEnter code here...
In
this example, I've only applied definitions for 5 latex symbols. I
don't know if there is an upper limit to symbol parameters that can be
created in this way. I guess this would be an interesting way to find
out ;-)
You then invoke this using the $symbol$ syntax of TW, like:
<<latex """f(x) = $int$_{-$infty$}^$infty$$hat$ f($xi$)\,e^{2 $pi$ i $xi$ x}\,d$xi$""" >>
This produces:

Now let's say you want to change the definition of pie on the fly. You can do it like this:
<<latex """f(x) = $int$_{-$infty$}^$infty$$hat$ f($xi$)\,e^{2 $pi$ i $xi$ x}\,d$xi$""" pi:"YUM" >>
which produces (I hope these images show up) :

You can also change the default definition everywhere just by changing the latex macro default parameter for PI.
So, that's my idea. Something to think about.
Mark