2

I try to sort enumerated lists in latex. All I found is how to sort items by their ordinal number (see Order items in enumerate environment automatically):

\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}

Could you please help me how to rewrite \sortitem and \printsortlists such that I can use the following code:

\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}

If it is not an easy task, I would also be happy if you provide an alternative solution to this problem.

  • Hm. You have now two variants, one with numbers and one with keywords. Which one do you really want? – Ulrike Fischer Dec 3 '18 at 12:44
  • I would like to use keywords rather than numbers. The code containing numbers is the example which I found in tex.stackexchange.com. – Roloka Dec 3 '18 at 12:46
  • 1
    I think it should be possible to combine tex.stackexchange.com/a/344002 with your question, I will try it out and write an answer if it works. – Marijn Dec 3 '18 at 12:49
2
\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}

enter image description here

  • Beat me to it :) +1 – Marijn Dec 3 '18 at 12:59
  • Thank you, it works fine! If I could upvote your answer, I would do it. – Roloka Dec 3 '18 at 13:00
  • @Roloka why should you not be able to upvote and accept the answer? It is your question. – Ulrike Fischer Dec 3 '18 at 13:03
  • I received the following message: "Thanks for the feedback! Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score." – Roloka Dec 3 '18 at 13:07
  • 1
    I had forgotten. It should work now. – Ulrike Fischer Dec 3 '18 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.