怎样才能将四个(或更多)命令放在一个命令中,而无需使用 expl3 重复太多代码?

怎样才能将四个(或更多)命令放在一个命令中,而无需使用 expl3 重复太多代码?

我正在尝试将四个命令组合在一起,这些命令封装了tikzpicture环境和使用tikzmark,它们都有自己的键,但它们有很多共同的代码。命令如下:

\tkm@ncbara[key=val]{mark-A}{mark-B}{text}
\tkm@ncbarb[key=val]{mark-A}{mark-B}{text}
\tkm@bracea[key=val]{mark-A}{mark-B}{text}
\tkm@braceb[key=val]{mark-A}{mark-B}{text}

我创建了tkmdraw如下命令:

\tkmdraw[ncbar-a, key val from ncbara]{mark-A}{mark-B}{text}
\tkmdraw[ncbar-b, key val from ncbarb]{mark-A}{mark-B}{text}
\tkmdraw[brace-a, key val from bracea]{mark-A}{mark-B}{text}
\tkmdraw[brace-a, key val from braceb]{mark-A}{mark-B}{text}

假设我认为向 tkmdraw 添加更多选项,但是,我认为我重复了很多代码,并且肯定有正确的方法来做到这一点,这是我的问题:

如何才能将四个(或更多)命令放在一个命令中,而不必重复使用那么多代码expl3

这是完整的示例文件(我无法缩小它):

% arara: pdflatex: {interaction: nonstopmode, draft: yes}
% arara: pdflatex: {interaction: nonstopmode}
% arara: clean: { extensions: [ aux, log, out ] }
\documentclass[10pt]{article}
\usepackage{amsmath,xparse,tikz}
\usetikzlibrary{tikzmark,arrows.meta,calc,decorations.pathreplacing}
\pagestyle{empty}

% Whidth for center node ... \TkM{name}{content}
\newlength{\MyL}
\newcommand*{\TkM}[2]{\settowidth{\MyL}{$\mathsurround0pt #2$}#2\hspace{-\MyL}\makebox[\MyL]{\pgfmark{#1}}}

\makeatletter
% active : for expl3 
\def\@colon{:}
% default style for ncbar
\tikzset{tkmdraw/ncbar-a/.style={ >={Straight Barb[length=1.5pt,round]},
                        font=\footnotesize,color=gray,text=red,above=-3.5pt,},}
\tikzset{tkmdraw/ncbar-b/.style={ >={Straight Barb[length=1.5pt,round]},
                        font=\footnotesize,color=gray,text=red,below=-2.5pt,},}
% default style for brace
\tikzset{tkmdraw/brace-a/.style={ font=\footnotesize,color=gray,text=red,},}
\tikzset{tkmdraw/brace-b/.style={ font=\footnotesize,color=gray,text=red,},}
\ExplSyntaxOn

\keys_define:nn { tkmdraw/ncbar-a }
 {
  ncbar-a  .bool_set:N        =  \l_tmpa_bool,
  tikz     .code:n            =  \tikzset{tkmdraw/ncbar-a/.append~style = {#1},},
  tikz     .initial:n         =  {},
  tikz     .value_required:n  =  true,
  text     .value_required:n  =  true,
  font     .value_required:n  =  true,
  color    .value_required:n  =  true,
  above    .value_required:n  =  true,
  arrow    .value_required:n  =  true,
  text     .meta:n            =  { tikz = { text = #1 }},
  font     .meta:n            =  { tikz = { font = #1 }},
  color    .meta:n            =  { tikz = { color = #1 }},
  above    .meta:n            =  { tikz = { above = #1 }},
  arrows   .meta:n            =  { tikz = { arrows = {#1} }},
  vsep-A   .dim_set:N         =  \l_ncbar_a_A_dim,
  vsep-A   .initial:n         =  1.85ex,
  vsep-B   .dim_set:N         =  \l_ncbar_a_B_dim,
  vsep-B   .initial:n         =  1.85ex,
  height   .dim_set:N         =  \l_ncbar_a_h_dim,
  height   .initial:n         =  0.5em,
  raise    .meta:n            =  { vsep-A = #1 , vsep-B = #1 }
 }

\NewDocumentCommand{\tkm@ncbara}{ O{} m  m +m}
  {
  \group_begin:
   \IfNoValueF {#1} { \keys_set:nn { tkmdraw/ncbar-a }{ #1 } }
    \begin{tikzpicture}[overlay,remember~ picture,tkmdraw/ncbar-a]
      \draw[<->]([yshift=\l_ncbar_a_A_dim]$(pic~ cs \@colon #2)$) -- ++(0,\l_ncbar_a_h_dim) -| 
        node[align=center,near~ start] {#4} 
       ([yshift=\l_ncbar_a_B_dim]$(pic~ cs \@colon #3)$);
    \end{tikzpicture}%
  \group_end:
  }

\keys_define:nn { tkmdraw/ncbar-b }
 {
  ncbar-b  .bool_set:N        =  \l_tmpa_bool,
  tikz     .code:n            =  \tikzset {tkmdraw/ncbar-b/.append~style = {#1},},
  tikz     .initial:n         =  {},
  tikz     .value_required:n  =  true,
  text     .value_required:n  =  true,
  font     .value_required:n  =  true,
  color    .value_required:n  =  true,
  above    .value_required:n  =  true,
  arrow    .value_required:n  =  true,
  text     .meta:n            =  { tikz = { text = #1 }},
  font     .meta:n            =  { tikz = { font = #1 }},
  color    .meta:n            =  { tikz = { color = #1 }},
  below    .meta:n            =  { tikz = { below = #1 }},
  arrows   .meta:n            =  { tikz = { arrows = {#1} }},
  vsep-A   .dim_set:N         =  \l_ncbar_b_A_dim,
  vsep-B   .dim_set:N         =  \l_ncbar_b_B_dim,
  height   .dim_set:N         =  \l_ncbar_b_h_dim,
  vsep-A   .initial:n         =  -0.5ex,
  vsep-B   .initial:n         =  -0.5ex,
  height   .initial:n         =  -0.5em,
  raise    .meta:n            =  { vsep-A = #1 , vsep-B = #1 }
 }

\NewDocumentCommand{\tkm@ncbarb}{ O{} m  m +m}
  {
  \group_begin:
   \IfNoValueF {#1} { \keys_set:nn { tkmdraw / ncbar-b }{ #1 } }
    \begin{tikzpicture}[overlay,remember~ picture,tkmdraw/ncbar-b]
      \draw[<->]([yshift=\l_ncbar_b_A_dim]$(pic~ cs \@colon #2)$) -- ++(0,\l_ncbar_b_h_dim) -| 
        node[align=center,near~ start] {#4} 
       ([yshift=\l_ncbar_b_B_dim]$(pic~ cs \@colon #3)$);
    \end{tikzpicture}%
  \group_end:
  }

\keys_define:nn { tkmdraw/brace-a }
 {
  brace-a    .bool_set:N        =  \l_tmpa_bool,
  tikz       .code:n            =  \tikzset{tkmdraw/brace-a/.append~style = {#1},},
  tikz       .initial:n         =  {},
  tikz       .value_required:n  =  true,
  text       .meta:n            =  { tikz = { text = #1 }},
  font       .meta:n            =  { tikz = { font = #1 }},
  color      .meta:n            =  { tikz = { color = #1 }},
  text       .value_required:n  =  true,
  font       .value_required:n  =  true,
  color      .value_required:n  =  true,
  above      .dim_set:N         =  \l_brace_above_dim,
  above      .initial:n         =  2.5pt,
  above      .value_required:n  =  true,
  amplitude  .dim_set:N         =  \l_brace_a_h_dim,
  amplitude  .initial:n         =  0.4em,
  amplitude  .value_required:n  =  true,
  vsep-A     .dim_set:N         =  \l_brace_a_A_dim,
  vsep-B     .dim_set:N         =  \l_brace_a_B_dim,
  vsep-A   .initial:n           =  1.85ex,
  vsep-B   .initial:n           =  1.85ex,
  raise    .meta:n              =  { vsep-A = #1 , vsep-B = #1 }
 }

\NewDocumentCommand{\tkm@bracea}{ O{} m  m +m}
  {
  \group_begin:
   \IfNoValueF {#1} { \keys_set:nn { tkmdraw / brace-a }{ #1 } }
    \begin{tikzpicture}[overlay,remember~ picture,tkmdraw/brace-a]
      \draw [decorate,decoration={brace, amplitude=\l_brace_a_h_dim}]
      ([yshift=\l_brace_a_A_dim]$(pic~ cs \@colon #2)$) -- ([yshift=\l_brace_a_B_dim]$(pic~ cs \@colon #3)$)
      node [align=center,midway,above = \l_brace_above_dim] {#4};
    \end{tikzpicture}
  \group_end:
  }

\keys_define:nn { tkmdraw/brace-b }
 {
  brace-b    .bool_set:N        =  \l_tmpa_bool,
  tikz       .code:n            =  \tikzset{tkmdraw/brace-b/.append~style = {#1},},
  tikz       .initial:n         =  {},
  tikz       .value_required:n  =  true,
  text       .meta:n            =  { tikz = { text = #1 }},
  font       .meta:n            =  { tikz = { font = #1 }},
  color      .meta:n            =  { tikz = { color = #1 }},
  text       .value_required:n  =  true,
  font       .value_required:n  =  true,
  color      .value_required:n  =  true,
  below      .dim_set:N         =  \l_brace_below_dim,
  below      .initial:n         =  2.5pt,
  below      .value_required:n  =  true,
  amplitude  .dim_set:N         =  \l_brace_b_h_dim,
  amplitude  .initial:n         =  0.4em,
  amplitude  .value_required:n  =  true,
  vsep-A     .dim_set:N         =  \l_brace_b_A_dim,
  vsep-B     .dim_set:N         =  \l_brace_b_B_dim,
  vsep-A     .initial:n         =  -1ex,
  vsep-B     .initial:n         =  -1ex,
  raise      .meta:n            =  { vsep-A = #1 , vsep-B = #1 }
 }

\NewDocumentCommand{\tkm@braceb}{ O{} m  m +m}
  {
  \group_begin:
   \IfNoValueF {#1} { \keys_set:nn { tkmdraw/brace-b }{ #1 } }
    \begin{tikzpicture}[overlay,remember~ picture,tkmdraw/brace-b]
      \draw [decorate,decoration={brace, mirror, amplitude=\l_brace_b_h_dim}]
      ([yshift=\l_brace_b_A_dim]$(pic~ cs \@colon #2)$) -- ([yshift=\l_brace_b_B_dim]$(pic~ cs \@colon #3)$)
      node [align=center,midway,below = \l_brace_below_dim] {#4};
    \end{tikzpicture}
  \group_end:
  }

\keys_define:nn { tkmdraw }
 {
  @ncbar-a  .bool_set:N  =  \l_ncbar_a_bool,
  @ncbar-b  .bool_set:N  =  \l_ncbar_b_bool,
  @brace-a  .bool_set:N  =  \l_brace_a_bool,
  @brace-b  .bool_set:N  =  \l_brace_b_bool,
  ncbar-a   .meta:n      =  { @ncbar-a = true, @ncbar-b = false, @brace-a = false, @brace-b = false },
  ncbar-b   .meta:n      =  { @ncbar-b = true, @ncbar-a = false, @brace-a = false, @brace-b = false },
  brace-a   .meta:n      =  { @brace-a = true, @ncbar-b = false, @ncbar-a = false, @brace-b = false },
  brace-b   .meta:n      =  { @brace-b = true, @ncbar-b = false, @ncbar-a = false, @brace-a = false },
 }

\NewDocumentCommand{\tkmdraw}{o m m +m}
 {
  \group_begin:
  \tkmdraw_test_options:n { #1 }
  \IfBooleanT{ \l_ncbar_a_bool } { \tkm@ncbara[#1]{#2}{#3}{#4} }
  \IfBooleanT{ \l_ncbar_b_bool } { \tkm@ncbarb[#1]{#2}{#3}{#4} }
  \IfBooleanT{ \l_brace_a_bool } { \tkm@bracea[#1]{#2}{#3}{#4} }
  \IfBooleanT{ \l_brace_b_bool } { \tkm@braceb[#1]{#2}{#3}{#4} }
  \group_end:
 }

\cs_new_protected:Npn \tkmdraw_test_options:n #1
 {
  \str_case:xnF { \clist_item:nn { #1 } { 1 } }
   {
   {ncbar-a} { \keys_set:nn { tkmdraw  } { ncbar-a } \keys_set:nn { tkmdraw/ncbar-a } {#1}}
   {ncbar-b} { \keys_set:nn { tkmdraw  } { ncbar-b } \keys_set:nn { tkmdraw/ncbar-b } {#1}}
   {brace-a} { \keys_set:nn { tkmdraw  } { brace-a } \keys_set:nn { tkmdraw/brace-a } {#1}}
   {brace-b} { \keys_set:nn { tkmdraw  } { brace-b } \keys_set:nn { tkmdraw/brace-b } {#1}}
   }
   {}
 }
\cs_generate_variant:Nn \str_case:nnF { x }
\ExplSyntaxOff
\makeatother

\begin{document}
Whit \verb+\tkm@ncbara+ and \verb+\tkm@ncbarb+ command  ... OK :)

\[ (\TkM{a1}{2}\TkM{a2}{p}q^{2})(\TkM{a3}{3}p\TkM{a4}{q})=6p^{2}q^{3} \]

\makeatletter
\tkm@ncbara[color=green,text=red,height= 0.3em]{a1}{a3}{ncbara}
\tkm@ncbarb[color=red,text=green,height= -0.3em]{a2}{a4}{ncbarb}
\makeatother

Whit \verb+\tkm@bracea+ and \verb+\tkm@braceb+ command  ... OK :)

\[ (\TkM{b1}{2}\TkM{b2}{p}q^{2})(\TkM{b3}{3}p\TkM{b4}{q})=6p^{2}q^{3} \]

\makeatletter
\tkm@bracea[color=green,text=red, amplitude= 0.3em]{b1}{b3}{bracea}
\tkm@braceb[color=red,text=green, amplitude= 0.3em]{b2}{b4}{braceb}
\makeatother

\par\vspace{0.65cm}

Whit \verb+\tkmdraw[key=val]{...}+ command  ... OK :)
\par\vspace{0.65cm}
% adapted from https://tex.stackexchange.com/a/76507/7832

\begin{align*}
(3{,}14 \times \TkM{c1}{10^{-2}}) + (1\TkM{c2}{{,}}2 \times  \TkM{c3}{10^{-4}}) 
             &= (3{,}14 \times \TkM{c4}{10^{-2}}) + (0\TkM{c5}{{,}}012 \times \TkM{c6}{10^{-2}})\\[3ex]
             &= (3{,}14+0{,}012) \times  10^{-2}\\
&= \TkM{c7}{3}{,}152 \times \TkM{c8}{10}^{-2} 
\end{align*}

\tkmdraw[ncbar-b,color=blue,text=red,raise=-3pt]{c1}{c3}{not equal}
\tkmdraw[ncbar-b,color=red,text=blue,raise=-3pt]{c4}{c6}{equal}
\tkmdraw[ncbar-a,color=red,text=blue]{c2}{c5}{The decimal is moved to the left \\ to increase the exponent}
\tkmdraw[brace-b,color=red,text=green, amplitude= 0.3em]{c7}{c8}{$0{,}30152$}
\end{document}

产生的结果图像: 产生的结果

背后的想法是“模仿”一点,我曾经使用的旧代码pstricks,我试图将我的文件移动到更兼容的东西(pdf/lua)latex

此致。

答案1

下面的代码实际上只是对 OP 中的代码进行了重新排列,并做了一些调整。不同之处在于:

  • 整个-a-b都已更改为-above-below。这是否更具可读性尚有争议,但这样做的主要动机是我可以#1=<length>在括号命令中写入,其中#1是“上方”或“下方”
  • 我已将括号的装饰代码放入tkmdraw/brace-a*样式设置中,这样括号命令就不需要设置这个
  • 我已将tkdraw/brace-*tkmdraw/ncbar-*样式设置从tikzpicture环境中移出并移到\draw[...]命令中,因为我认为这是它应该出现的位置——而且这与上次更改配合得更好
  • 我已经定义了命令\DefineBarKeys\DefineBraceKeys设置了横杠和括号键,这样这些代码块就不需要输入两次了
  • 我已定义\tkm@ncbar\tkm@brace命令,它们负责完成横线和括号的大部分工作。尽管这两个命令很相似,但考虑到横线和括号之间的差异,我看不出有什么简单的方法可以组合这两个命令。

这些更改节省了大约 30 行代码,但更重要的是减少了代码重复。我认为输出没有变化:

在此处输入图片描述

修改后的代码如下:

\documentclass[10pt]{article}
\usepackage{amsmath,xparse,tikz}
\usetikzlibrary{tikzmark,arrows.meta,calc,decorations.pathreplacing}
\pagestyle{empty}

% Width for center node ... \TkM{name}{content}
\newlength{\MyL}
\newcommand*{\TkM}[2]{\settowidth{\MyL}{$\mathsurround0pt #2$}#2\hspace{-\MyL}\makebox[\MyL]{\pgfmark{#1}}}

\makeatletter

% default style for ncbar
\tikzset{
  tkmdraw/ncbar-above/.style={
    >={Straight Barb[length=1.5pt,round]},
   font=\footnotesize,color=gray,text=red,above=-3.5pt,
  },
  tkmdraw/ncbar-below/.style={
    >={Straight Barb[length=1.5pt,round]},
    font=\footnotesize,color=gray,text=red,below=-2.5pt,
  }
}

% default style for brace - amplitude set by \tkm@brace
\tikzset{
  tkmdraw/brace-above/.style={
     font=\footnotesize,color=gray, text=red,
     decorate, decoration={brace, amplitude=#1}
  },
  tkmdraw/brace-below/.style={
     font=\footnotesize,color=gray,text=red,
     decorate, decoration={brace, mirror, amplitude=#1}
  },
}

% active : for expl3
\def\@colon{:}

\ExplSyntaxOn
\cs_new_protected:Npn \__define_bar_keys:nnn #1#2#3 { % #1=above/below, #2=sep, #3=height
  \keys_define:nn { tkmdraw/ncbar-#1 }
   {
    ncbar-#1 .bool_set:N        =  \l_tmpa_bool,
    tikz     .code:n            =  \tikzset {tkmdraw/ncbar-#1/.append~style = {##1},},
    tikz     .initial:n         =  {},
    tikz     .value_required:n  =  true,
    text     .value_required:n  =  true,
    font     .value_required:n  =  true,
    color    .value_required:n  =  true,
    #1       .value_required:n  =  true,
    arrow    .value_required:n  =  true,
    text     .meta:n            =  { tikz = { text = ##1 }},
    font     .meta:n            =  { tikz = { font = ##1 }},
    color    .meta:n            =  { tikz = { color = ##1 }},
    #1       .meta:n            =  { tikz = { #1 = ##1 }},
    arrows   .meta:n            =  { tikz = { arrows = {##1} }},
    vsep-A   .dim_set:c         =  {l_ncbar_#1_A_dim},
    vsep-B   .dim_set:c         =  {l_ncbar_#1_B_dim},
    height   .dim_set:c         =  {l_ncbar_#1_h_dim},
    vsep-A   .initial:n         =  #2,
    vsep-B   .initial:n         =  #2,
    height   .initial:n         =  #3,
    raise    .meta:n            =  { vsep-A = ##1 , vsep-B = ##1 }
   }
}

\__define_bar_keys:nnn{above}{1.85ex}{0.5em}
\__define_bar_keys:nnn{below}{-0.5ex}{-0.5em}

\NewDocumentCommand{\tkm@ncbar}{ m m m  m +m}
  {
  \group_begin:
    \IfNoValueF {#2} { \keys_set:nn { tkmdraw/ncbar-#1 }{ #2 } }
    \begin{tikzpicture}[overlay, remember~picture]
      \draw[tkmdraw/ncbar-#1, <->]
        ([yshift=\use:c{l_ncbar_#1_A_dim}]$(pic~cs \@colon #3)$) -- ++(0,\use:c{l_ncbar_#1_h_dim}) -|
        node[align=center,near~ start] {#5} ([yshift=\use:c{l_ncbar_#1_B_dim}]$(pic~cs \@colon #4)$);
    \end{tikzpicture}%
  \group_end:
  }

\NewDocumentCommand{\tkm@ncbara}{ O{} m m +m}
  {
  \tkm@ncbar{above}{#1}{#2}{#3}{#4}
  }

\NewDocumentCommand{\tkm@ncbarb}{ O{} m m +m}
  {
  \tkm@ncbar{below}{#1}{#2}{#3}{#4}
  }

\cs_new_protected:Npn \__define_brace_keys:nn #1#2 { % #1=above/below, #2=sep, #3=height
  \keys_define:nn { tkmdraw/brace-#1 }
   {
    brace-#1 .bool_set:N        =  \l_tmpa_bool,
    tikz        .code:n            =  \tikzset{tkmdraw/brace-#1/.append~style = {##1},},
    tikz        .initial:n         =  {},
    tikz        .value_required:n  =  true,
    text        .meta:n            =  { tikz = { text = ##1 }},
    font        .meta:n            =  { tikz = { font = ##1 }},
    color       .meta:n            =  { tikz = { color = ##1 }},
    text        .value_required:n  =  true,
    font        .value_required:n  =  true,
    color       .value_required:n  =  true,
    #1          .dim_set:c         =  {l_brace_#1_dim},
    #1          .initial:n         =  2.5pt,
    #1          .value_required:n  =  true,
    amplitude   .dim_set:c         =  {l_brace_#1_h_dim},
    amplitude   .initial:n         =  0.4em,
    amplitude   .value_required:n  =  true,
    vsep-A      .dim_set:c         =  {l_brace_#1_A_dim},
    vsep-B      .dim_set:c         =  {l_brace_#1_B_dim},
    vsep-A      .initial:n         =  #2,
    vsep-B      .initial:n         =  #2,
    raise       .meta:n            =  { vsep-A = ##1 , vsep-B = ##1 }
   }
}
\__define_brace_keys:nn{above}{1.85ex}
\__define_brace_keys:nn{below}{-1.00ex}

\NewDocumentCommand{\tkm@brace}{m m m m +m}
  {
  \group_begin:
    \IfNoValueF{#2} { \keys_set:nn{tkmdraw/brace-#1}{#2} }
    \begin{tikzpicture}[overlay, remember~picture]
      \draw[tkmdraw/brace-#1=\use:c{l_brace_#1_h_dim}]
        ([yshift=\use:c{l_brace_#1_A_dim}]$(pic~cs \@colon #3)$)--([yshift=\use:c{l_brace_#1_B_dim}]$(pic~cs \@colon #4)$)
        node [align=center, midway, #1=\use:c{l_brace_#1_dim}] {#5};
    \end{tikzpicture}
  \group_end:
  }

\NewDocumentCommand{\tkm@bracea}{ O{} m m +m}
  {
  \tkm@brace{above}{#1}{#2}{#3}{#4}
  }

\NewDocumentCommand{\tkm@braceb}{ O{} m m +m}
  {
  \tkm@brace{below}{#1}{#2}{#3}{#4}
  }

\keys_define:nn { tkmdraw }
 {
  @ncbar-above  .bool_set:N  =  \l_ncbar_a_bool,
  @ncbar-below  .bool_set:N  =  \l_ncbar_b_bool,
  @brace-above  .bool_set:N  =  \l_brace_a_bool,
  @brace-below  .bool_set:N  =  \l_brace_b_bool,
  ncbar-above   .meta:n      =  { @ncbar-above = true, @ncbar-below = false, @brace-above = false, @brace-below = false },
  ncbar-below   .meta:n      =  { @ncbar-below = true, @ncbar-above = false, @brace-above = false, @brace-below = false },
  brace-above   .meta:n      =  { @brace-above = true, @ncbar-below = false, @ncbar-above = false, @brace-below = false },
  brace-below   .meta:n      =  { @brace-below = true, @ncbar-below = false, @ncbar-above = false, @brace-above = false },
 }

\NewDocumentCommand{\tkmdraw}{o m m +m}
 {
  \group_begin:
  \tkmdraw_test_options:n { #1 }
  \IfBooleanT{ \l_ncbar_a_bool } { \tkm@ncbara[#1]{#2}{#3}{#4} }
  \IfBooleanT{ \l_ncbar_b_bool } { \tkm@ncbarb[#1]{#2}{#3}{#4} }
  \IfBooleanT{ \l_brace_a_bool } { \tkm@bracea[#1]{#2}{#3}{#4} }
  \IfBooleanT{ \l_brace_b_bool } { \tkm@braceb[#1]{#2}{#3}{#4} }
  \group_end:
 }

\cs_new_protected:Npn \tkmdraw_test_options:n #1
 {
  \str_case:xnF { \clist_item:nn { #1 } { 1 } }
   {
   {ncbar-above} { \keys_set:nn { tkmdraw  } { ncbar-above } \keys_set:nn { tkmdraw/ncbar-above } {#1}}
   {ncbar-below} { \keys_set:nn { tkmdraw  } { ncbar-below } \keys_set:nn { tkmdraw/ncbar-below } {#1}}
   {brace-above} { \keys_set:nn { tkmdraw  } { brace-above } \keys_set:nn { tkmdraw/brace-above } {#1}}
   {brace-below} { \keys_set:nn { tkmdraw  } { brace-below } \keys_set:nn { tkmdraw/brace-below } {#1}}
   }
   {}
 }
\cs_generate_variant:Nn \str_case:nnF { x }
\ExplSyntaxOff
\makeatother

\begin{document}
With \verb+\tkm@ncbara+ and \verb+\tkm@ncbarb+ command  ... OK :)

\[ (\TkM{a1}{2}\TkM{a2}{p}q^{2})(\TkM{a3}{3}p\TkM{a4}{q})=6p^{2}q^{3} \]

\makeatletter
\tkm@ncbara[color=green,text=red,height= 0.3em]{a1}{a3}{ncbara}
\tkm@ncbarb[color=red,text=green,height= -0.3em]{a2}{a4}{ncbarb}
\makeatother

With \verb+\tkm@bracea+ and \verb+\tkm@braceb+ command  ... OK :)

\[ (\TkM{b1}{2}\TkM{b2}{p}q^{2})(\TkM{b3}{3}p\TkM{b4}{q})=6p^{2}q^{3} \]

\makeatletter
\tkm@bracea[color=green,text=red, amplitude= 0.3em]{b1}{b3}{bracea}
\tkm@braceb[color=red,text=green, amplitude= 0.3em]{b2}{b4}{braceb}
\makeatother

\par\vspace{0.65cm}

With \verb+\tkmdraw[key=val]{...}+ command  ... OK :)
\par\vspace{0.65cm}
% adapted from https://tex.stackexchange.com/a/76507/7832

\begin{align*}
(3{,}14 \times \TkM{c1}{10^{-2}}) + (1\TkM{c2}{{,}}2 \times  \TkM{c3}{10^{-4}})
             &= (3{,}14 \times \TkM{c4}{10^{-2}}) + (0\TkM{c5}{{,}}012 \times \TkM{c6}{10^{-2}})\\[3ex]
             &= (3{,}14+0{,}012) \times  10^{-2}\\
&= \TkM{c7}{3}{,}152 \times \TkM{c8}{10}^{-2}
\end{align*}

\tkmdraw[ncbar-below,color=blue,text=red,raise=-3pt]{c1}{c3}{not equal}
\tkmdraw[ncbar-below,color=red,text=blue,raise=-3pt]{c4}{c6}{equal}
\tkmdraw[ncbar-above,color=red,text=blue]{c2}{c5}{The decimal is moved to the left \\ to increase the exponent}
\tkmdraw[brace-below,color=red,text=green, amplitude= 0.3em]{c7}{c8}{$0{,}30152$}
\end{document}

相关内容