This is a followup to Egregs answer in the question Expandable math, The code is included here as an MWE.
It would be a great help if I could somehow delete all previeously defined variables, or assign them only within a group. Another possibility could be simply to allow reassigning of variables with the same name. As of now, this gives an error on attempting to do so.
Edit: I've added some lines to the MWE at the bottom, which tries to set the variables within a group(but still makes them global), attempting to redefine a variable, which gives an error on trying to print it.
Error given:
./root.tex:122: Missing \endcsname inserted.
\cs_set_nopar:Npxl.122 }
\documentclass{article}
\usepackage{xparse,l3regex,siunitx,xcolor}
\ExplSyntaxOn
\seq_new:N \g_runart_variables_seq
\prop_new:N \l__runart_variables_temp_prop
\tl_new:N \l__runart_variables_matte_tl
\tl_new:N \l__runart_variables_item_tl
\NewDocumentCommand{\definevariable}{mm}
{ % #1 is the name, #2 is the key-value set
\seq_gput_right:Nn \g_runart_variables_seq { #1 }
\prop_clear:N \l__runart_variables_temp_prop
\keys_set:nn { runart/variables } { #2 }
\prop_gclear_new:c { g_runart_var_#1_prop }
\prop_gset_eq:cN { g_runart_var_#1_prop } \l__runart_variables_temp_prop
}
% syntactic sugar
\cs_new_protected:Nn \__runart_gput:nn
{
\prop_gput:Nnn \l__runart_variables_temp_prop { #1 } { #2 }
}
% keys
\keys_define:nn { runart/variables }
{
value .code:n = \__runart_gput:nn { value } { #1 },
value .value_required:n = true,
unit .code:n = \__runart_gput:nn { unit } { #1 },
unit .value_required:n = true,
name .code:n = \__runart_gput:nn { name } { #1 },
name .value_required:n = true,
sisetup .code:n = \__runart_gput:nn { sisetup } { #1 },
}
\NewDocumentCommand{\matte}{m}
{ % #1 is the expression to output
\tl_set:Nn \l__runart_variables_matte_tl { #1 }
\seq_map_inline:Nn \g_runart_variables_seq
{
\regex_replace_all:nnN
{ ##1 }
{ \c{runart_variable_use_matte:n} \cB\{ ##1 \cE\} }
\l__runart_variables_matte_tl
}
\tl_use:N \l__runart_variables_matte_tl
}
\NewDocumentCommand{\formal}{m}
{
\tl_set:Nn \l__runart_variables_matte_tl { #1 }
\seq_map_inline:Nn \g_runart_variables_seq
{
\regex_replace_all:nnN
{ ##1 }
{ \c{runart_variable_use_formal:n} \cB\{ ##1 \cE\} }
\l__runart_variables_matte_tl
}
\tl_use:N \l__runart_variables_matte_tl
}
\cs_new_protected:Nn \runart_variable_use_matte:n
{
\use:x % it's necessary to expand the optional argument
{
\SI
[\prop_item:cn { g_runart_var_#1_prop } {sisetup}]
{\prop_item:cn { g_runart_var_#1_prop } {value}}
{\prop_item:cn { g_runart_var_#1_prop } {unit}}
}
}
\cs_new_protected:Nn \runart_variable_use_formal:n
{
\prop_item:cn { g_runart_var_#1_prop } { name }
}
\ExplSyntaxOff
\begin{document}
{
\definevariable{a_car}
{
name=a_{\mathrm{car}},
value=20,
unit=\metre\per\second,
}
\definevariable{v_car}
{
name=v_{\mathrm{car}},
value=40,
unit=\metre,
}
\definevariable{t_car}
{
name=t_{\mathrm{car}},
value=2,
unit=\second,
sisetup={color=red},
}
\[
\matte{
a_car=\frac{v_car}{t_car}
}
\]
}
Trying to redefine a variable:
\definevariable{t_car}
{
name=t_{\mathrm{car}},
value=2,
unit=\second,
sisetup={color=red},
}
\[
\matte{
t_car
}
\]
\end{document}

\definevariableeverywhere and this would clear the already assigned values, in case you use a previously used name. Note that it would be easy to define variables just in the scope of anequationenvironment, but not foralignorgather. – egreg Jan 06 '16 at 11:11align,gatheretc. Well, if it doesn't work it isn't the end of the world. Just means that I'll have to make sure all my variables are unique. – Runar Jan 06 '16 at 12:05