Using receipt of How to process a comma separated list?, I elaborated the following code for my needs:


%\seq_new:N \l_input_seq % declare a list (seq) variable
\cs_if_exist:NTF \l_input_seq { } { \seq_new:N \l_input_seq }

%\tl_new:N \l_last_word_tl % declare a "token list" variable
\cs_if_exist:NTF \l_last_word_tl { } { \tl_new:N \l_last_word_tl }

\NewDocumentCommand { \PrintAnswer } { m }
    \file_if_exist:nTF {#1}
        \iow_term:n {****~Printing~#1}
        \file_input:n {#1}
        \refstepcounter {subsection}
        \iow_term:n {****~#1~not~found~****}
        %\iow_term:n {****~splitting~#1~about~/~****}
        % Split the argument into words about / sign.
        \seq_set_split:Nnn \l_input_seq { / } {#1}
        %% Remove the last word from the seq.
        %\seq_pop_right:NN \l_input_seq \l_last_word_tl
        % Stores the right-most item from a <sequence> in the <token list
        % variable> without removing it from the <sequence>. The <token list
        % variable> is assigned locally. If <sequence> is empty the <token 
        % list variable> will contain the special marker \q_no_value
        \seq_get_right:NN \l_input_seq \l_last_word_tl
        \file_if_exist:nTF {\l_last_word_tl}
            \iow_term:x {****~Printing~\l_last_word_tl~****}
            \file_input:n { \l_last_word_tl }
            \refstepcounter {subsection}
            %\iow_term:n {****~\l_last_word_tl~not~found~****}
            %\iow_term:x {****~\l_last_word_tl~not~found~****}
            \iow_term:x {****~\l_last_word_tl\space not~found~****}
\cs_generate_variant:Nn \clist_map_inline:nn {o}
\NewDocumentCommand { \PrintAnswerList } { m }
  { \clist_map_inline:on {#1} { \PrintAnswer {##1.ans} } }

It is intended for conditional compilation of a textbook, every chapter of which (eg, 101.tex, 102.tex) writes answers to problems to the file named after the name of the chapter source file, (eg, 101.ans, 102.ans etc). Near the end of the book these files are read in by the macro \PrintAnswer as foolows:


where \inputfiles macro is defined at the start of compilation using the following trick to keep comma separated list of desired chapters:

\typein[\inputfiles]{^^JEnter filename(s) for \protect\includeonly:}

Everything works fine. But now I'd like to extend the functionality of this code to allow for sorting of the list. Suppose, I type 105,106,104 (with wrong order of files), then the existing code prints the answers in that order while 104,105,106 (with correct order of files) is desired. Unfortunately, my own knowledge of latex3 coding is insufficient to solve this problem. Can someone direct me on the way to a solution?

  • Alphabetical or numerical sorting? – egreg Jan 20 '16 at 8:06
  • @egreg: What's the difference? I guess that alphabetical. Currently all filenames contain digits only, but I changed naming scheme several times in the past. – Igor Kotelnikov Jan 20 '16 at 13:19
  • 21.tex comes later, in alphabetical order, than 111.tex – egreg Jan 20 '16 at 13:22
  • @egreg: Usually I put leading zero and 21.tex will be 021.tex. Then, alphabetical and numerical sorting gives same result, – Igor Kotelnikov Jan 21 '16 at 5:32
  • @IgorKotelnikov For alphabetical sorting you might want to fall back to Lua. – Henri Menke Mar 12 '16 at 9:48

Your Answer

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

Browse other questions tagged or ask your own question.