从 LaTeX 中变量内存储的字符串中获取子字符串

从 LaTeX 中变量内存储的字符串中获取子字符串

我目前遇到以下问题,但找不到解决方案。这是关于从字符串变量中获取子字符串...

  1. 我有一个嵌套的值列表:

    \ExplSyntaxOn
        \NewDocumentCommand{\DeclareListOfValues}{ m m }
        {
            \clist_gclear_new:c {g_list_of_values_#1_clist}
            \clist_gset:cn {g_list_of_values_#1_clist} {#2}
        }
    
        \NewDocumentCommand{\GetValueFromList}{ m m }
        {
            \clist_item:cn {g_list_of_values_#1_clist} {#2}
        }
    \ExplSyntaxOff
    
    %List structure: {TotResClassA,TotResClassB,TotResClassC,TotResClassD,TotResClassE}
    \DeclareListOfValues{GraphiteArrangementClassesTotalRes}{70.2,29.8,0,0,0}
    %List structure: {{ImgName,ResClassA,ResClassB,ResClassC,ResClassD,ResClassE},{...},{...},...}
    \DeclareListOfValues{GraphiteArrangementClasses}{
        {14-1-11,73.6,26.4,0,0,0}, 
        {15-1-4,72.7,27.3,0,0,0}, 
        {15-1-6,67.7, 32.3,0,0,0},
        {16-1-10,75.6, 24.4,0,0,0},
        {1562-3-6,68.7,31.3,0,0,0},
        {1562-3-16,71.6,28.4,0,0,0},
        {1563-3-8,85.9,14.1,0,0,0},
        {1563-3-13,71.8,28.2,0,0,0},
        {1564-2-7,58.1,41.9,0,0,0},
        {1564-3-14,56.2,43.8,0,0,0}
    }
    
  2. 然后我获取列表中的第一个元素,如下所示:

    \newcommand{\teststr}{\GetValueFromList{GraphiteArrangementClasses}{1}} %teststr: 14-1-11,73.6,26.4,0,0,0 
    

我现在需要从这个字符串中获取图像的名称,它是14-1-11。我尝试使用 xstring,但没有成功。只有当 teststring 是逗号分隔的任意字符串时,它才有效,例如

    \newcommand{\teststr}{123,2563,58888,9541278}
    \StrBefore[1]{\teststr}{,} %res: 123

但如果我从列表中获取字符串,就像我的情况一样 - 而且我无法理解,为什么以及实际上有什么区别!?

如果有人能帮助我解决这个问题,我会非常高兴!

谨致问候,迈克尔

答案1

我猜您不仅想检索一件物品,还想检索里面的任何物品。

\documentclass{article}

\ExplSyntaxOn
\NewDocumentCommand{\DeclareListOfValues}{ m m }
  {
    \clist_gclear_new:c {g_list_of_values_#1_clist}
    \clist_gset:cn {g_list_of_values_#1_clist} {#2}
  }

\NewExpandableDocumentCommand{\GetValueFromList}{ m o m }
  {
    \IfValueTF { #2 }
      {
        \exp_args:Ne \clist_item:nn { \clist_item:cn {g_list_of_values_#1_clist} {#3} } { #2 }
      }
      {
        \clist_item:cn {g_list_of_values_#1_clist} {#3}
      }
  }
\ExplSyntaxOff

%List structure: {TotResClassA,TotResClassB,TotResClassC,TotResClassD,TotResClassE}
\DeclareListOfValues{GraphiteArrangementClassesTotalRes}{70.2,29.8,0,0,0}
%List structure: {{ImgName,ResClassA,ResClassB,ResClassC,ResClassD,ResClassE},{...},{...},...}
\DeclareListOfValues{GraphiteArrangementClasses}{
    {14-1-11,73.6,26.4,0,0,0}, 
    {15-1-4,72.7,27.3,0,0,0}, 
    {15-1-6,67.7, 32.3,0,0,0},
    {16-1-10,75.6, 24.4,0,0,0},
    {1562-3-6,68.7,31.3,0,0,0},
    {1562-3-16,71.6,28.4,0,0,0},
    {1563-3-8,85.9,14.1,0,0,0},
    {1563-3-13,71.8,28.2,0,0,0},
    {1564-2-7,58.1,41.9,0,0,0},
    {1564-3-14,56.2,43.8,0,0,0}
}

\begin{document}

\GetValueFromList{GraphiteArrangementClasses}{1}

\GetValueFromList{GraphiteArrangementClasses}[1]{1}

\GetValueFromList{GraphiteArrangementClasses}[2]{1}

\GetValueFromList{GraphiteArrangementClasses}{2}

\GetValueFromList{GraphiteArrangementClasses}[1]{2}

\GetValueFromList{GraphiteArrangementClasses}[2]{2}

\end{document}

在此处输入图片描述

相关内容