尝试使用带有可选参数的 collcell.sty

尝试使用带有可选参数的 collcell.sty

我正在尝试使用collcell.stywith xparse。如果我有一个带有可选参数的宏,则使用 withcollcell.sty似乎不会获取可选参数。例如:

\documentclass{article}

\usepackage{xparse}
\usepackage{xcolor}
\usepackage{collcell}

\DeclareDocumentCommand\coloritt{omm}{%
    \IfNoValueTF{#3}{#2}{#2\textcolor{#1}{#3}}%
}

\DeclareDocumentCommand{\foo}%
    {O{blue}>{\SplitArgument{1}{-}}m}
    {\coloritt[#1]#2}

\newcolumntype{L}{>{\collectcell\foo}l<{\endcollectcell}}

\begin{document}

\begin{tabular}{lL}
    Something&Some-thing\\
    Ask&[red]Ask-ing\\ %% <<-- Does not work
    NULL&NULL
\end{tabular}

\foo[red]{abcd-efgh} %% <<-- Does work.

\end{document}

Collcell 和 xparse 示例

我究竟做错了什么?

答案1

包装collcell将收集到的细胞用括号包裹起来,因此你最终会得到例如

\foo{[red]Ask-ing}

而不是

\foo[red]{Ask-ing}

事实上,对于后一种情况,你必须自己找到添加括号的方法。仅仅使用xparse你可能会得到

\documentclass{article}

\usepackage{xparse}
\usepackage{xcolor}
\usepackage{collcell}

\DeclareDocumentCommand\coloritt{omm}{%
    \IfNoValueTF{#3}{#2}{#2\textcolor{#1}{#3}}%
}

\DeclareDocumentCommand{\foo}{m}{\fooa#1\stop}
\DeclareDocumentCommand{\fooa}
    {O{blue}>{\SplitArgument{1}{-}}u{\stop}}
    {\coloritt[#1]#2}

\newcolumntype{L}{>{\collectcell\foo}l<{\endcollectcell}}

\begin{document}

\begin{tabular}{lL}
    Something&Some-thing\\
    Ask&[red]Ask-ing\\ %% <<-- Does not work
    NULL&NULL
\end{tabular}

\end{document}

但理想情况下,你应该将文档标记与处理分开。(这显然是一个极端情况:我们在这里到底是在编程还是不在编程!)

答案2

两种不同的方法;第一种使用标准expl3构造,第二种使用正则表达式。

第一的

\documentclass{article}
\usepackage{collcell,xparse,xcolor}

\NewDocumentCommand{\coloritt}{O{blue}mo}{%
  \IfNoValueTF{#3}{#2}{#2\textcolor{#1}{#3}}%
}

\ExplSyntaxOn
\NewDocumentCommand{\processcell}{m}
 {
  \sgmoye_processcell:n { #1 }
 }

\tl_new:N \l_sgmoye_processcell_oargi_tl
\tl_new:N \l_sgmoye_processcell_oargii_tl
\tl_new:N \l_sgmoye_processcell_marg_tl
\seq_new:N \l_sgmoye_processcell_text_seq

\cs_new_protected:Npn \sgmoye_processcell:n #1
 {
  \tl_clear:N \l_sgmoye_processcell_oargi_tl
  \tl_clear:N \l_sgmoye_processcell_oargii_tl
  \tl_clear:N \l_sgmoye_processcell_marg_tl
  \seq_set_split:Nnn \l_sgmoye_processcell_text_seq { ] } { #1 }
  \int_compare:nTF { \seq_count:N \l_sgmoye_processcell_text_seq == 1 }
   { % no initial optional argument
    \__sgmoye_other_args:n { #1 }
   }
   {
    \tl_set:Nx \l_sgmoye_processcell_oargi_tl
     {
      \seq_item:Nn \l_sgmoye_processcell_text_seq { 1 } ] % reinstate the ]
     }
    \tl_set:Nx \l_sgmoye_processcell_marg_tl
     {
      \seq_item:Nn \l_sgmoye_processcell_text_seq { 2 }
     }
    \__sgmoye_other_args:V \l_sgmoye_processcell_marg_tl
   }
  \use:x
   {
    \coloritt
    \exp_not:V \l_sgmoye_processcell_oargi_tl
    \exp_not:V \l_sgmoye_processcell_marg_tl
    \exp_not:V \l_sgmoye_processcell_oargii_tl
   }
 }

\cs_new_protected:Npn \__sgmoye_other_args:n #1
 {
  \seq_set_split:Nnn \l_sgmoye_processcell_text_seq { - } { #1 }
  \int_compare:nTF { \seq_count:N \l_sgmoye_processcell_text_seq == 1 }
   { % no -
    \tl_set:Nn \l_sgmoye_processcell_marg_tl { { #1 } }
   }
   {
    \tl_set:Nx \l_sgmoye_processcell_marg_tl
     {
      { \seq_item:Nn \l_sgmoye_processcell_text_seq { 1 } }
     }
    \tl_set:Nx \l_sgmoye_processcell_oargii_tl
     {
      [ \seq_item:Nn \l_sgmoye_processcell_text_seq { 2 } ]
     }
   }
 }
\cs_generate_variant:Nn \__sgmoye_other_args:n { V }
\ExplSyntaxOff

\newcolumntype{L}{>{\collectcell\processcell}l<{\endcollectcell}}

\begin{document}

\begin{tabular}{lL}
    Something&Some-thing\\
    Ask&[red]Ask-ing\\ %% <<-- Does not work
    NULL&NULL
\end{tabular}

\end{document}

第二

\documentclass{article}
\usepackage{collcell,xparse,l3regex,xcolor}

\NewDocumentCommand{\coloritt}{O{blue}mo}{%
  \IfNoValueTF{#3}{#2}{#2\textcolor{#1}{#3}}%
}

\ExplSyntaxOn
\NewDocumentCommand{\processcell}{m}
 {
  \sgmoye_processcell:n { #1 }
 }

\tl_new:N \l_sgmoye_processcell_oarg_tl
\tl_new:N \l_sgmoye_processcell_marg_tl
\seq_new:N \l_sgmoye_processcell_text_seq

\cs_new_protected:Npn \sgmoye_processcell:n #1
 {
  \tl_clear:N \l_sgmoye_processcell_oarg_tl
  \regex_match:nnTF { \A \[ } { #1 }
   {% there is an optional argument: split the two parts
    \regex_extract_once:nnN { \A (\[.*\]) (.*) \Z } { #1 } \l_sgmoye_processcell_text_seq
    \tl_set:Nx \l_sgmoye_processcell_oarg_tl
     {
      \seq_item:Nn \l_sgmoye_processcell_text_seq { 2 }
     }
    \tl_set:Nx \l_sgmoye_processcell_marg_tl
     {
      \seq_item:Nn \l_sgmoye_processcell_text_seq { 3 }
     }
   }
   {
    \tl_set:Nn \l_sgmoye_processcell_marg_tl { #1 }
   }
  \regex_match:nVTF { \- } \l_sgmoye_processcell_marg_tl
   {
    \regex_replace_once:nnN { \A (.*) \- (.*) \Z } { \cB\{\1\cE\}[\2] } \l_sgmoye_processcell_marg_tl
   }
   {
    \tl_set:Nx \l_sgmoye_processcell_marg_tl
     {
      { \exp_not:V \l_sgmoye_processcell_marg_tl }
     }
   }
  \use:x
   {
    \coloritt
    \exp_not:V \l_sgmoye_processcell_oarg_tl
    \exp_not:V \l_sgmoye_processcell_marg_tl
   }
 }
\cs_generate_variant:Nn \regex_match:nnTF { nV }
\ExplSyntaxOff

\newcolumntype{L}{>{\collectcell\processcell}l<{\endcollectcell}}

\begin{document}

\begin{tabular}{lL}
    Something&Some-thing\\
    Ask&[red]Ask-ing\\
    NULL&NULL
\end{tabular}

\end{document}

两者的输出

在此处输入图片描述

相关内容