使用分隔符按单词拆分短语,不能使用字符串变量

使用分隔符按单词拆分短语,不能使用字符串变量

目标:使用 xparse 函数 \SplitList 和 \ProcessList 发送一个字符串变量,该变量包含一个短语,该短语包含用逗号或分号分隔的多个单词。

问题该函数无法识别字符串或无法拆分短语。请参阅示例,其中我能够拆分作为函数参数手动输入的短语

\documentclass{article}
\usepackage{xparse}
\makeatletter
\NewDocumentCommand{\isplit}{>{\SplitList{,}} m}{\ProcessList{#1}
\i@split}
\NewDocumentCommand{\i@split}{>{\SplitList{;}}m}{\ProcessList{#1}\i@paint}
\NewDocumentCommand{\i@paint}{m}{
{\fbox{#1}}%
}
\makeatother

\newtoks\myvar  % ** Best way to keep string variable values hidden? 
\newcommand{\anotherstring}{}   % ** Shows value upon assigment.  
                                % ** Not good
\begin{document}

\myvar{doc begins}%
% new command variable; not preferred I want to hide the assigned string
\anotherstring{do not want; to show, the string value; yet}
\newline%
\the\myvar%     % ** An example to show how to retrieve the value of hpvar
\newline%
\newline%
\newline%
this is how it should be: \isplit{this,sample;works!}%
\newline%
oops! \isplit{\anotherstring}
\newline%
\myvar{this,sample;hpvar does not work}%
myvar: \isplit{\the\myvar}%
\end{document}

输出: 分裂还是不分裂,这是个问题

答案1

这一切都是随包装一起“开箱即用的” listofitems

\documentclass{article}
\usepackage{listofitems}
\begin{document}
\def\anotherstring{do not want; to show, the string value; yet}

\setsepchar{,||;}

\readlist*\mylist{this,sample;works!}
\showitems\mylist

\readlist*\mylist{\anotherstring}
\showitems\mylist

The list has \mylistlen{} items.

Item 4 of the list is ``\mylist[4]''

\readlist*\mylist{this,sample;hpvar does not work}
\showitems\mylist
\end{document}

在此处输入图片描述

答案2

显然\SplitList不会扩展参数,使用另一个wrapper命令将首先扩展参数,然后将其输入到\isplit其中,然后依次使用\i@split等。

\documentclass{article}
\usepackage{xparse}
\makeatletter
\NewDocumentCommand{\isplit}{m}{\expandafter\@@isplit\expandafter{#1}}


\NewDocumentCommand{\@@isplit}{>{\SplitList{,}} m}{%
  \ProcessList{#1}{\i@split}}
\NewDocumentCommand{\i@split}{>{\SplitList{;}}m}{\ProcessList{#1}\i@paint}
\NewDocumentCommand{\i@paint}{m}{%
{\fbox{#1}}%
}
\makeatother

\newtoks\myvar  % ** Best way to keep string variable values hidden? 
\newcommand{\anotherstring}{}   % ** Shows value upon assigment.  
                                % ** Not good
\begin{document}

\myvar={doc begins}%
% new command variable; not preferred I want to hide the assigned string
\renewcommand{\anotherstring}{do not want; to show, the string value; yet}

\the\myvar%     % ** An example to show how to retrieve the value of hpvar

this is how it should be: \isplit{this,sample;works!}%

oops! \isplit{\anotherstring}

\myvar{this,sample;hpvar does not work}%
\the\myvar

myvar: \isplit{\the\myvar}%
\end{document}

在此处输入图片描述

答案3

不确定这个用途是什么;尽管如此,你还是误用了xparse

\documentclass{article}
\usepackage{xparse}
\usepackage{xcolor} % for the example

\ExplSyntaxOn
\NewDocumentCommand{\isplit}{smO{\fbox}}
 {
  \IfBooleanTF{#1}
   { \hpcolos_isplit:VN #2 #3 }
   { \hpcolos_isplit:nN { #2 } #3 }
 }
\cs_new_protected:Nn \hpcolos_isplit:nN
 {
  \tl_set:Nn \l__hpcolos_isplit_input_tl { #1 }
  \tl_replace_all:Nnn \l__hpcolos_isplit_input_tl { ; } { , }
  \seq_set_split:NnV \l__hpcolos_isplit_input_seq { , } \l__hpcolos_isplit_input_tl
  \seq_map_function:NN \l__hpcolos_isplit_input_seq #2
 }
\cs_generate_variant:Nn \hpcolos_isplit:nN { V }
\ExplSyntaxOff

\newcommand{\coloredbox}[1]{\colorbox{green}{#1}}

\begin{document}

\newcommand{\mystring}{do not want; to show, the string value; yet}

\isplit{do not want; to show, the string value; yet}

\isplit*{\mystring}

\isplit{do not want; to show, the string value; yet}[\coloredbox]

\isplit*{\mystring}[\coloredbox]

\end{document}

该字符串被规范化为具有逗号分隔符,然后拆分并将每个项目传递给可选参数 default 中的函数\fbox

还可以做更多的事情,但是您的问题太笼统,无法进行猜测。

在此处输入图片描述

相关内容