使用参考编号对商品进行排序

使用参考编号对商品进行排序

我尝试在 Latex 中对枚举列表进行排序。我发现的只是如何按序数对项目进行排序(请参阅自动对枚举环境中的物品进行排序):

\documentclass{article}

\usepackage{pgffor}
\makeatletter
\newcounter{SortListTotal}
\newcommand{\sortitem}[1]{\stepcounter{SortListTotal}\expandafter\def\csname SortItem\arabic{SortListTotal}\endcsname{#1}}
\newcommand{\printsortlist}[1]{\@for\currentitem:=#1\do{\item\csname SortItem\currentitem\endcsname}\setcounter{SortListTotal}{0}}
\makeatother

\begin{document}

\begin{enumerate}
\sortitem{This is the third item.}
\sortitem{This is the fourth item.}
\sortitem{This is the first item.}
\sortitem{This is the second item.}
\printsortlist{3,4,1,2}
\end{enumerate}

\end{document}

您能否帮助我如何重写\sortitem\printsortlists以便我可以使用以下代码:

\newcommand{\nc}{three,four,one,two}
\begin{enumerate}
\sortitem{one}{This is the third item.}
\sortitem{two}{This is the fourth item.}
\sortitem{three}{This is the first item.}
\sortitem{four}{This is the second item.}
\printsortlist{\nc}
\end{enumerate}

如果这不是一件容易的事,如果您能提供解决这个问题的替代解决方案,我也会很高兴。

答案1

\documentclass{article}

\usepackage{xparse}
\ExplSyntaxOn
\prop_new:N \g__rol_sortlist_prop
\NewDocumentCommand\sortitem { m m }
 {
  \prop_gput:Nnn\g__rol_sortlist_prop { #1 }{ # 2}
 }

\NewDocumentCommand \printsortlist { m }
 {
  \clist_set:No \l_tmpa_clist { #1 }
  \clist_map_inline:Nn \l_tmpa_clist 
   {
    \item \prop_item:Nn \g__rol_sortlist_prop {##1} 
   }       
 }  
\ExplSyntaxOff

\begin{document}

\newcommand{\nc}{three,four,one,two}
\begin{enumerate}
\sortitem{one}{This is the third item.}
\sortitem{two}{This is the fourth item.}
\sortitem{three}{This is the first item.}
\sortitem{four}{This is the second item.}
\printsortlist{\nc}
\end{enumerate}

\end{document}

在此处输入图片描述

相关内容