是否可以设置 LaTeX3 序列的特定项目?

是否可以设置 LaTeX3 序列的特定项目?

我想使用一个seq数组,然后通过索引设置该数组中的特定项。我知道我可以通过以下方式访问某项,\seq_item:Nn但不知道是否有类似的东西\seq_set_item:Nnn

\documentclass{article}

\usepackage{xparse}

\ExplSyntaxOn

\seq_new:N \my_seq

\seq_set_split:Nnn \my_seq {;} {a;;b;c;d}

% set item 5 to x
% \seq_set_item: Nnn \my_seq { 5 } { x }

\NewDocumentCommand {\printseq} {}
 {
  \seq_use:Nn \my_seq {,~}
 }

\ExplSyntaxOff


\begin{document}
Sequence: \printseq
\end{document}

这样的事可能吗,或者 LaTeX3 中是否有其他结构可以用作索引数组?


一些背景信息

我想在新包中使用它,通过给出其编号将 TikZ 样式/选项应用于列表中的特定元素。例如

\mycmd[2=red]{a b c b}

应该将列表处理a b a c为四个 TikZ node(工作正常)并且第二个(b)将变为红色,因为应用的选项是red

答案1

这是我的第一次尝试

\documentclass{article}

\usepackage{xparse}

\ExplSyntaxOn

\seq_new:N \my_seq

\seq_set_split:Nnn \my_seq {;} {a;;b;c;d}


\cs_new:Npn \seq_set_item: Nnn #1 #2 #3
 {
  \seq_if_exist:NTF #1
  { \__tobi_seq_setitem_aux:Nnn #1 #2 #3 }
  { \__msg_kernel_expandable_error:nnn { kernel } { bad-variable } {#1} }
 }

\seq_new:N \l_tobi_tma_seq
\seq_new:N \l_tobi_tmb_seq
\cs_generate_variant:Nn \seq_put_right:Nn { Nf }

\cs_new:Npn  \__tobi_seq_setitem_aux:Nnn #1 #2 #3
 {
  \seq_set_eq:NN \l_tobi_tma_seq #1
  \seq_set_eq:NN \l_tobi_tmb_seq \c_empty_seq
  \int_step_inline:nnnn { 1 } { 1 } { #2 -1 }
    {
     \seq_put_right:Nf \l_tobi_tmb_seq { \seq_item:Nn \l_tobi_tma_seq  { 1 }  }
     \seq_pop_left:NN \l_tobi_tma_seq \l_tmpa_tl
    }
 \seq_put_right:Nn \l_tobi_tmb_seq { #3 }
 \seq_concat:NNN #1 \l_tobi_tmb_seq \l_tobi_tma_seq
 }
% set item 5 to x
\seq_set_item: Nnn \my_seq { 5 } { x }

\NewDocumentCommand {\printseq} {}
 {
  \seq_use:Nn \my_seq {,~}
 }

\ExplSyntaxOff


\begin{document}
Sequence: \printseq
\end{document}

请注意:此方法非常简单。它不关心序列的原始数字。如果您想在包中使用它,则需要进行更多测试。

答案2

这是我根据 David 和 Bruno 的评论提出的解决方案。

\documentclass{article}

\usepackage{xparse,tikz}

\ExplSyntaxOn

% DEFINE VARIABLES
% sequence to save the word list
\seq_new:N \l_tobi_words_seq
% sequence to proces the hightlight key value pair
\seq_new:N \l_tobi_highlight_seq
% property list to ave the higlights
\prop_new:N \l_tobi_highlights_prop
% process counter
\int_new:N \l_tobi_counter_int

% DEFINE VARIANTS
% we'll need argument variants for these macros
\cs_generate_variant:Nn \prop_get:Nn { NV }
\cs_generate_variant:Nn \prop_put:Nnn { Nxx }

% MACRO TO PROCESS HIGHLIGHT LIST
% this macro reads the property list, given as comma
% separated key value pairs
\cs_new_protected:Npn \tobi_evaluate_higlights:n #1
 {
  % clear the properties from last time, otherwiese the highlights
  % will stay permanent
  \prop_clear:N \l_tobi_highlights_prop
  % process the comma separatet list
  \clist_map_inline:nn { #1 }
   {
    % spilt each list item at the equal sign and store
    % the part before and after it into a sequence
    \seq_set_split:Nnn \l_tobi_highlight_seq { = } { ##1 }
    % set the propertie from the current key value pair
    \prop_put:Nxx \l_tobi_highlights_prop
     {
      % the first sequence item is the key, i.e. the
      % part before the equal sign
      \seq_item:Nn \l_tobi_highlight_seq { 1 }
     }
     {
      % the second sequence item it the value, i.e.
      % the part behind the equal sign
      \seq_item:Nn \l_tobi_highlight_seq { 2 }
     }
   }
 }

% MACRO TO FORMAT A SEQUENCE
% (more complex in real life ...)
\NewDocumentCommand {\printseq} { O{} m }
 {
  % process the optional argument as property list
  \tobi_evaluate_higlights:n { #1 }
  % store the second arguemt in a sequence
  \seq_set_split:Nnn \l_tobi_words_seq {;} { #2 }
  % set the porcess counter to 0
  \int_zero:N \l_tobi_counter_int
  % process each word sequence item
  \seq_map_inline:Nn \l_tobi_words_seq
   {
    % increment the process counter
    \int_incr:N \l_tobi_counter_int
    % draw the item insid a TikZ node and apply the property
    % according to the process counter, it will be empty if the
    % property isn't defined
    \tikz \node [draw,\prop_get:NV \l_tobi_highlights_prop \l_tobi_counter_int] {##1};
   }
 }

\ExplSyntaxOff


\begin{document}
Sequence: \printseq[2=red]{foo ; bar ; baz}
\end{document}

相关内容