3

I am trying to create a simple function that takes in some lowercase letters a,b,r,e,r etc removes any duplicate letters, and sort them. Then I want to list the output in a list

Input:

\supportMaterials{r,r,e,a}

Output:

\begin{enumerate}
    \item a
    \item e
    \item r
\end{enumerate}
  • I tried to use the following code: https://tex.stackexchange.com/a/333666/8306. To create a sorted list, however, no output is given when I try to enumerate the list. Perhaps it expands too late?
  • In addition I am not quite sure how to remove duplicates.

Here is my attempt so far

\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}
  • You need \exp_args:No \clist_map_inline:nn { \sortedInputList } ... – Ulrike Fischer Aug 8 '19 at 15:12
3

If the items are single lowercase letters, you can compare their ASCII codes.

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

enter image description here

|improve this answer|||||
1

You do not need any packages for that.

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

enter image description here

|improve this answer|||||

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.