可扩展数学

可扩展数学

我正在尝试创建一个脚本,将一些易于读写的命令扩展为更全面的数学公式,该公式使用变量,以便它首先打印其名称,然后在新行上打印其值。例如:

因此, a_{car}=\frac{v_car}{t_car}, a_{car}=\frac{\SI{20}{metre\per\second}}{\SI{20}{seconds}}我不需要这样写:,而只需写 \matte{acar=\frac{vcar}{tcar},它就会扩展至第一行。

在 tex.stackexchange 上的另一个问题上,我得到了一个关于如何实现这一点的非常好的通用答案,但我需要一些帮助来调整它以适应我自己的代码。通用答案位于这里。我将在此处包含该代码:

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

这本身就很有效,但在我的文档中,我使用不同的方法来定义变量,如下所示:

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

 }

如果能够根据我的设置调整我得到的答案那就太好了。

答案1

这是一个实现,为您的变量提供了更好的键值输入:

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

在此处输入图片描述

相关内容