2

I'm trying to put together four commands that encapsulate tikzpicture environments and the use of tikzmark, in one, all have their own keys, but, they have a lot of code in common. The commands are as follows :

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

and I created the tkmdraw command to work as follows:

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

hypothetically I think adding more options to tkmdraw, but, I think I'm repeating a lot of code and for sure there is a correct way to do it, here's my question:

How can I put four (or more) commands in one command, without repeating so much code using expl3?

This is the FULL sample file (I can not reduce it):

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

An image of the result that produces: result that produces

The idea behind is to "imitate" a bit, the old code that I had using pstricks, I'm trying to move my file to something more compatible with (pdf/lua)latex.

Best regards.

  • It may be a bit off-topic, but pstricks works well under XeLaTeX. If you're not playing with lua, you'll get an unicode output with pstricks and won't need to convert your code. – sztruks Oct 15 '18 at 06:16

1 Answers1

2

The code below really only rearranges the code in the OP with a few tweaks. The differences are:

  • throughout -a and -b have been changed to -above and -below. It is debatable that this is more readable but the main motivation for doing this is so that I can write #1=<length> in the brace commands, where #1 is either "above" or "below"
  • I have put the decoration code for the braces into the tkmdraw/brace-a* style settings so that the brace commands do not need to set this
  • I have moved the tkdraw/brace-* and tkmdraw/ncbar-* style settings out of the tikzpicture environment and into the \draw[...] command because this is where I think it belongs -- and this also works better with the last change
  • I have defined commands \DefineBarKeys and \DefineBraceKeys to set up the bar and brace keys, so that these code blocks do not need to be typed twice
  • I have defined \tkm@ncbar and \tkm@brace commands that do the bulk of the work for bars and braces. Even though these two commands are similar I do not see an easy way to combine these two commands given the differences between bars and braces.

These changes save about 30 lines of code but, more importantly, reduce code duplication. The output is, I think, unchanged:

enter image description here

Here is the modified code:

\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}
  • Thanks for the prompt reply, but, it does not work as expected ... did you see the output of the document? ...brace is not correct – Pablo González L Oct 15 '18 at 01:13
  • @PabloGonzálezL I think that the output is the same as that in the OP now. I have also reduced the code for defining the keys. –  Oct 15 '18 at 02:47
  • Great, can play a bit with boolean vars and keep short names :) ... a query, can save in a variable for example \l_tkmdraw_position and then assign values (above|below|left|right) when activated (bool=true) instead of using #1)? Total Thanks – Pablo González L Oct 15 '18 at 04:24
  • @PabloGonzálezL Can you give an example use? Instead of a boolean would it be better to have a choice key, with choices above, below, leftandright, that defaults to above? –  Oct 15 '18 at 13:53
  • I need!...style=choice :)...better – Pablo González L Oct 15 '18 at 14:05
  • Thank you very much for the response, I have marked it as accepted, with this I can reduce the code a bit. The style=ncbar-a|ncbar-b|brace-a|brace-b was one of my first attempts (like in https://tex.stackexchange.com/q/179180/7832), but, I did not succeed, in the end I was left with the comparison by string and then activate the keys. This is more perl style than LaTeX but it works. Agradecido Total. – Pablo González L Oct 16 '18 at 11:51
  • @PabloGonzálezL Sorry didn't get back to this yesterday. I still don't really understand what you want with style=... etc. If you haven't already solved this I"m happy to look at it if you can give more details as to what you want. –  Oct 17 '18 at 01:38
  • Let's see, when pass \tkmdraw[ncbar-a calls \tkmdraw_test_options: n if find ncbar-a then active \l_ncbar_a_bool, this is the usual logic I use when write a script, but, in (La)TeX is not like that because the expansion of tokens and other obscure arts. Works, but it is not written correctly from my point of view. The correct thing would be if "I find [ncbar-a, ..] or (the not yet created style = choice) [style = ncbar-a] then active\ l_ncbar_a_booland keys and code". Compare[#1]withncbar-aas text I think that is not the correct thing inexpl3`. – Pablo González L Oct 17 '18 at 02:52