我正在尝试创建一个简单的函数,它接受一些小写字母 a、b、r、e、r 等,删除所有重复字母,然后对它们进行排序。然后我想将输出列在列表中
Input:
\supportMaterials{r,r,e,a}
Output:
\begin{enumerate}
\item a
\item e
\item r
\end{enumerate}
- 我尝试使用以下代码:https://tex.stackexchange.com/a/333666/8306。但是,要创建排序列表,当我尝试枚举列表时没有给出任何输出。也许它扩展得太晚了?
- 此外,我不太清楚如何删除重复项。
这是我迄今为止的尝试
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{enumitem}
\usepackage{expl3,xparse,xstring}
\ExplSyntaxOn
\prg_new_conditional:Nnn \john_string_if_before:nn { p,T,F,TF }
{% I hope the LaTeX3 police won't catch me
\int_compare:nTF { \pdftex_strcmp:D { #1 } { #2 } < 0 }
{
\prg_return_true:
}
{
\prg_return_false:
}
}
\NewDocumentCommand{\sortlist}{smm}
{
\IfBooleanTF{#1}
{
\clist_set:No \l__john_sortlist_data_clist { #2 }
}
{
\clist_set:Nn \l__john_sortlist_data_clist { #2 }
}
\john_sortlist:N \l__john_sortlist_data_clist
\clist_set_eq:NN #3 \l__john_sortlist_data_clist
}
\clist_new:N \l__john_sortlist_data_clist
\cs_new_protected:Nn \john_sortlist:N
{
\clist_sort:Nn #1
{
\john_string_if_before:nnTF { ##1 } { ##2 }
{
\sort_return_same:
}
{
\sort_return_swapped:
}
}
}
\NewDocumentCommand\supportMaterialHelp{m}{%
\IfStrEqCase{#1}{%
{r}{R}%
{b}{B}%
{k}{K}%
{c}{C}%
{a}{A}%
{e}{E}%
}[]%
}
\NewDocumentCommand{\supportMaterial}{ m }
% Input is a list {r,b,k,...} which is defined in the function
% \supportMaterialHelp above. This code loops through the values
% and creates an enumerate with the values
{%
\sortlist{#1}{\sortedInputList}%
\begin{enumerate}[label={--}]%
% This works fine
\clist_map_inline:nn { #1 } { \item \supportMaterialHelp{##1} }
% Why does this fail to work?
\clist_map_inline:nn { \sortedInputList } { \item \supportMaterialHelp{##1} }
\end{enumerate}
}
\ExplSyntaxOff
\begin{document}
\supportMaterial{r,e,k}
\end{document}
答案1
如果项目是单个小写字母,则可以比较它们的 ASCII 码。
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\supportMaterials}{m}
{
\begin{itemize}
\nebuch_supportmaterials:n { #1 }
\end{itemize}
}
\clist_new:N \l__nebuch_supportmaterials_clist
\cs_new_protected:Nn \nebuch_supportmaterials:n
{
\clist_set:Nn \l__nebuch_supportmaterials_clist { #1 }
\clist_remove_duplicates:N \l__nebuch_supportmaterials_clist
\clist_sort:Nn \l__nebuch_supportmaterials_clist
{
\int_compare:nTF { `##1 > `##2 }
{ \sort_return_swapped: }
{ \sort_return_same: }
}
\clist_map_function:NN \l__nebuch_supportmaterials_clist \__nebuch_supportmaterials_print:n
}
\cs_new_protected:Nn \__nebuch_supportmaterials_print:n
{
\item
\str_case:nn { #1 }
{
{r}{R}
{b}{B}
{k}{K}
{c}{C}
{a}{A}
{e}{E}
}
}
\ExplSyntaxOff
\begin{document}
\supportMaterials{r,r,e,a}
\end{document}
答案2
您不需要任何包。
\documentclass{article}
\newif\ifmember
\makeatletter% for \@for see e.g. https://tex.stackexchange.com/a/100684/121799
% from https://tex.stackexchange.com/a/498576/121799
% but with arbitrary stuff instead of integers
\newcommand{\MemberQ}[2]{\global\memberfalse%
\edef\temp{#2}%
\@for\next:=#1\do{\ifx\next\temp\relax\global\membertrue\fi}}
\edef\MyItems{a,b,e,r}
\newcommand{\supportMaterials}[1]{%
\begin{itemize}
\@for\next:=\MyItems\do{\edef\temp{\next}%
\MemberQ{#1}{\temp}%
\ifmember \item \temp\fi%
}%
\end{itemize}}
\makeatother
\begin{document}
\supportMaterials{r,r,e,a}
\end{document}