JeT
**My (open) question**

How can I extend the benefits of pgfkeys to store and resuse formulae ?

**Context**

So many long formuale I use again and again... I saw many advantages to store my formulae in pgfkeys:

- It simplifies typing (e.g. \RF[delta]), especially when long formulae got to be in a table.
- it avoids the multiplication of \def (I get lost after 100 macros with weirder and longer names to remember).
- One can use numbers (e.g. \RF[d1])
- One centralizes the format and thus one can check the consistency of notations.
- as a result, one avoid discrepancies in typing of the formulae between different documents.
- Could be useful for index, labelling/cross referencing, nomenclatures
- ... probably many other thing (I've not thought about yet...)

**Where do I stand ?**

So far I have a simple working (storing) solution (see MWE below).

**Where do I need insight ?**

If you had to think about the equations you keep using, How would you see a smart way to centralize information you'll need about these equations ? And reuse it efficiently.

I bascally use /.initial but there must be other keys to do the job better (list ?).

PS : Probably related to [this great answer](https://topanswers.xyz/tex?q=1557) since I intend to draw or calculate values for these equations at some point.

**Example of applications I have in mind**


\documentclass{article}

\usepackage{tikz}
\usepackage{mathtools}

% I used long formulae on purpose
\pgfkeys{
formula/.is family,
formula,
spot/.initial		= { S },
today/.initial	= { t_0 },
maturity/.initial	= { T },
TTM/.initial		= { T-t },
abm/.initial		= { {S_{t}=S_{0}+\mu t+\sigma W_{t}} },
gmb/.initial		= { \mu=\mathbb{E}(R_{t})=\mathbb{E}\left[\frac{dS_{t}}{S_{t}}\right] },
forward/.initial	= { F =\displaystyle Se^{rt}},
forward div repo/.initial	= { F_T = S_0 e^{(r-\text{div}-\text{repo})\times T} },
delta/.initial	= { \mathcal{N}\left(\frac{\ln \frac{F}{K} +\frac{1}{2}\sigma^2T}{\sigma \sqrt{T}}\right)  },
d1/.initial		= { d_1 = \displaystyle \frac{\ln\frac{S}{K} + (r + \frac{\sigma^2}{2})T}{\sigma \sqrt{T}}},
d2/.initial		= { d_2 = \displaystyle \frac{\ln\frac{S}{K} + (r - \frac{\sigma^2}{2})T}{\sigma \sqrt{T}}},
edp/.initial		= { \Theta + r S \Delta + \frac{1}{2} \sigma^2 S^2 \Gamma = r V},
call/.initial		= { C(S, T) =  S \mathcal{N}\left(   d_1 \right) - K e^{-rT} \mathcal{N}\left(   d_2\right)},
put/.initial		= { P(S, T) = -S \mathcal{N}\left( - d_1 \right) + K e^{-rT} \mathcal{N}\left( - d_2\right)},
}

\newcommand\formula[1]{\pgfkeys{formula,#1}}

% ReuseFormula (\RF)
\newcommand\RF[1][]{ \formula{#1} }

\begin{document}

It's easy to use in text $\RF[call]$.

This is Delta, noted $\Delta$ that is worth $\RF[delta]$.

Delta  is a rate of variation generally used to know the senisitivity of your option to changes in your underlying.

$\RF[forward div repo]$

$\RF[edp]$

Or format table with such long formulae

\begin{table}[tph]
\centering
\begin{tabular}{c}
\hline
$\RF[call]$ \\
$\RF[put]$  \\
$\RF[d1]$   \\
$\RF[d2]$   \\
\hline
\end{tabular}
\caption{Much simplier tables}
\end{table}

\end{document}



user 3.14159
I'm not sure if any of my suggestions is useful. Yes, you can store elements of equations in pgf keys, and this can make a lot of sense because of name space limitations. You can also "learn" and repeat combinations, but this affords global styles. Unfortunately, I was unable to get satisfactory results in align environments generated by the /.list key handler, but if you are willing to use a "refined" list handler this works, too.

\documentclass{article}
\usepackage{pgffor}
\usepackage{mathtools}
\makeatletter% stolen from forest
\pgfkeys{/handlers/.global style/.code=\pgfkeys{\pgfkeyscurrentpath/.global code=\pgfkeysalso{#1}}}
\pgfkeysdef{/handlers/.global code}{\pgfkeysglobaldef{\pgfkeyscurrentpath}{#1}}
\long\def\pgfkeysglobaldef#1#2{%
\long\def\pgfkeys@temp##1\pgfeov{#2}%
\pgfkeysgloballet{#1/.@cmd}{\pgfkeys@temp}%
\pgfkeysglobalsetvalue{#1/.@body}{#2}%
}
\def\pgfkeysgloballet#1#2{%
\expandafter\global\expandafter\let\csname pgfk@#1\endcsname#2%
}
\long\def\pgfkeysglobalsetvalue#1#2{%
\pgfkeys@temptoks{#2}\expandafter\xdef\csname pgfk@#1\endcsname{\the\pgfkeys@temptoks}%
}
\makeatother
\newcommand{\diff}{\mathop{}\!\text{d}}
\DeclareMathOperator{\Euler}{e}
% I used long formulae on purpose
\pgfkeys{
formula/.is family,
formula/.unknown/.code={#1},
formula/.cd,
plain/.code={#1},
edp/.code		= { \Theta + r S \Delta + \frac{1}{2} \sigma^2 S^2 \Gamma = r V},
spot/.code		= { S },
today/.code		= { t_0 },
maturity/.code	= { T },
TTM/.code			= { T-t },
delta/.initial	= { \mathcal{N}\left(\frac{\ln \frac{F}{K} +\frac{1}{2}\sigma^2T}{\sigma \sqrt{T}}\right)  },
d_1/.code		= { \frac{\ln\frac{S}{K} + (r + \frac{\sigma^2}{2})T}{\sigma \sqrt{T}}},
d_2/.code		= { \frac{\ln\frac{S}{K} + (r - \frac{\sigma^2}{2})T}{\sigma \sqrt{T}}},
do align/.code={\pgfkeys{formula/list/unless first=\\}%
#1&=\formula{#1}},
}
\makeatletter
\newif\if@pgf@rlist@first
\pgfkeys{
formula/.cd,
list/only first/.code={\if@pgf@rlist@first#1\fi},
list/unless first/.code={\if@pgf@rlist@first\global\@pgf@rlist@firstfalse\else#1\fi},
}
\pgfkeys{/handlers/.rlist/.code=%
{%
\ifx\foreach\@undefined%
\pgfkeys@error{You need to load the pgffor package to use the .list key syntax.}%
\fi%
\global\@pgf@rlist@firsttrue
% Use foreach to unfold the list
\def\pgf@keys@temp{}%
\foreach \pgf@keys@key in{#1}%
{\expandafter\expandafter\expandafter\gdef%
\expandafter\expandafter\expandafter\pgf@keys@temp%
\expandafter\expandafter\expandafter{\expandafter\pgf@keys@temp\expandafter{\pgf@keys@key}}}%
\edef\pgf@keys@list@path{\pgfkeyscurrentpath}%
\expandafter\expandafter\expandafter\pgf@keys@do@list%
\expandafter\expandafter\expandafter{\expandafter\pgf@keys@list@path\expandafter}\pgf@keys@temp\pgf@stop%
}%
}
\makeatother

\newcommand\formula[2][]{%
\ifx#1\relax\relax
\else
\pgfkeys{formula/.cd,@#1/.global style={#2}}%
\fi
\pgfkeys{formula/.cd,#2}}

\newcommand\RF[2][]{\formula{@#2}}

\begin{document}
You can use formulas from your repository:
$\formula{edp}$.\bigskip

The optional argument can be used to store the keys in a global (!) style:
$\formula[f1]{spot,plain={=},today}$. As you can see, plain can be used to add
some arbitrary elements.\bigskip

You can repeat the style, $\RF{f1}$.\bigskip

Align can be used, too,
\begin{align}
\pgfkeys{formula/do align/.rlist={d_1,d_2}}
\end{align}
but at the expense of defining a refined'' list handler \texttt{/.rlist}. This
list handler allows us to distinguish the first item from the rest. Notice that
the \texttt{/.list} key handler is based on \verb|\foreach|, for which some
folklore says that it always puts things in groups and cannot deal with ampersands
($\&$) and the like. As one can see, the \texttt{/.list} key handler does not
have any of those limitations, nor does \verb|\foreach|, one only has to
train'' it a little bit, which is precisely what the \texttt{/.list} key
handler does. The above-mentioned ficiticous limitations are those which are
often said to prevent us from using \verb|\foreach| to create tabulars,
matrices, and/or aligned equations.
\end{document}

![Screen Shot 2021-05-19 at 2.55.34 PM.png](/image?hash=7848a262d22a64beb19a1ecb0830363aa9781f9d8c319783e86267b013778aab)

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.