我正在嘗試創建一個簡單的函數,它接受一些小寫字母 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}