pgf add tag
**My (open) question**

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


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]( since I intend to draw or calculate values for these equations at some point.

**Example of applications I have in mind**



% I used long formulae on purpose
  formula/.is family,
  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)},


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


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

		$\RF[call]$ \\
		$\RF[put]$  \\ 
		$\RF[d1]$   \\
		$\RF[d2]$   \\ 
	\caption{Much simplier tables}


Top Answer
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.
\makeatletter% stolen from forest
\pgfkeys{/handlers/.global style/.code=\pgfkeys{\pgfkeyscurrentpath/.global code=\pgfkeysalso{#1}}}
\pgfkeysdef{/handlers/.global code}{\pgfkeysglobaldef{\pgfkeyscurrentpath}{#1}}
  \expandafter\global\expandafter\let\csname pgfk@#1\endcsname#2%
  \pgfkeys@temptoks{#2}\expandafter\xdef\csname pgfk@#1\endcsname{\the\pgfkeys@temptoks}%
% I used long formulae on purpose
  formula/.is family,
  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=\\}%
  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@error{You need to load the pgffor package to use the .list key syntax.}%
    % Use foreach to unfold the list
    \foreach \pgf@keys@key in{#1}%

\pgfkeys{formula/.cd,@#1/.global style={#2}}%


You can use formulas from your repository:

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,
\pgfkeys{formula/do align/.rlist={d_1,d_2}}
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. 
![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.