6

I am trying to create a script which expands some easy to read and write commands into more full-blown math formulas which uses variables so that it first prints its names and then on a new line, it would print its values. For instance:

So, instead of writing: a_{car}=\frac{v_car}{t_car}, a_{car}=\frac{\SI{20}{metre\per\second}}{\SI{20}{seconds}}, I would just write \matte{acar=\frac{vcar}{tcar}, and it would expand to that first line.

On a different question here on tex.stackexchange, I got a very good general answer to how to accomplish this, but I need some help adjusting it to my own code. The general answer is located here. I'll include that code here:

\documentclass{article}
\usepackage{xparse,l3regex,siunitx}

\ExplSyntaxOn

\prop_new:N \g_runart_variables_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 formatting
  \prop_gput:Nnn \g_runart_variables_prop { #1 } { #2 }
 }

\NewDocumentCommand{\removevariable}{m}
 { % #1 is the name
  \prop_gremove:Nn \g_runart_variables_prop { #1 }
 }

\NewDocumentCommand{\matte}{m}
 { % #1 is the expression to output
  \tl_set:Nn \l__runart_variables_matte_tl { #1 }
  \prop_map_inline:Nn \g_runart_variables_prop
   {
    \tl_set:Nn \l__runart_variables_item_tl { ##2 }
    \regex_replace_all:nnN
     { ##1 }
     { \u{l__runart_variables_item_tl} }
     \l__runart_variables_matte_tl
   }
  \tl_use:N \l__runart_variables_matte_tl
 }

\cs_new_protected:Nn \runart_variable_use:n
 {
  \prop_item:Nn \g_runart_variables_prop { #1 }
 }
\ExplSyntaxOff

\definevariable{a_car}{\SI{20}{\metre\per\second}}
\definevariable{v_car}{\SI{40}{\metre}}
\definevariable{t_car}{\SI{2}{\second}}

\begin{document}

\[
\matte{
  a_car=\frac{v_car}{t_car}
}
\]

\end{document}

This works great on it's own, but In my document I use a different method for defining variables, like this:

\NewDocumentCommand{\varSet}{definevariable}
 % #1 : name, like a_car
 % #2 : value, like 20
 % #3 : unit, like \metre, this is for siunitx
 % #4 : printed name, like \Omega_{R1}
 % #5 : optional sisetup for this var only, like color=red
{
  \prop_gclear_new:c { g_giacomo_var_#1_prop }
  \prop_gput:cnx { g_giacomo_var_#1_prop } { value } { #2 }
  \prop_gput:cnn { g_giacomo_var_#1_prop } { unit } { #3 }
  \prop_gput:cnn { g_giacomo_var_#1_prop } { name } { #4 }
      \IfNoValueTF { #5 } {
         \prop_gput:cnn { g_giacomo_var_#1_prop } { sisetup } {blank}
     } {
         \prop_gput:cnn { g_giacomo_var_#1_prop } { sisetup } { #5 }
     }

 }

It would be really great if it would be possible to adjust the answer I got to my setup.

Runar
  • 6,082

1 Answers1

6

Here's an implementation, with a better key-value input for your variables:

\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_put:nn
 {
  \prop_put:Nnn \l__runart_variables_temp_prop { #1 } { #2 }
 }

% keys
\keys_define:nn { runart/variables }
 {
  value .code:n = \__runart_put:nn { value } { #1 },
  value .value_required:n = true,
  unit .code:n = \__runart_put:nn { unit } { #1 },
  unit .value_required:n = true,
  name .code:n = \__runart_put:nn { name } { #1 },
  name .value_required:n = true,
  sisetup .code:n = \__runart_put: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

\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},
 }

\begin{document}

\[
\matte{
  a_car=\frac{v_car}{t_car}
}
\]
\[
\formal{
  a_car=\frac{v_car}{t_car}
}
\]
\end{document}

enter image description here

egreg
  • 1,121,712