[Tex/LaTex] How to process a comma separated list

comma-separated listlatex3

Using receipt of "LaTeX3: unable to convert space separated list into clist" I elaborated the following code for my needs:

\documentclass{book}
 ...
\usepackage{expl3}

\begin{document}

\newcommand{\PrintAnswer}[1]{%
\InputIfFileExists{#1}{\refstepcounter{subsection}}{\typeout{*** #1 not found ***}}}

\ExplSyntaxOn
\cs_new:Nn\PrintAnswerList:n{
  \clist_set:Nx\l_csv_clist{#1}
  \clist_map_inline:Nn\l_csv_clist{
    \typeout{**** Printing ##1.ans}
    \ExplSyntaxOff
    \PrintAnswer{##1.ans}
    \ExplSyntaxOn
  }
}
\PrintAnswerList:n{\inputfiles}
\ExplSyntaxOff

\end{document} 

It is intended for conditional compilation of a textbook, every chapter of which (eg, 01.tex, 02.tex) writes answers to problems to the file named after the name of the chapter source file, (eg, 01.ans, 02.ans etc). Near the end of the book these files are read in by the macro \PrintAnswer. Usually, I compile only few chapters using the following trick to keep desired chapters in \inputfiles macro:

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

All that works fine, but I was forced to switch off experimental syntax before \PrintAnswer{##1.ans} because otherwise answer files are not processed correctly (in particular, LaTeX complains that the commands for greek letters are not defined and hyphenation is broken). Therefore my question is How can one rewrite the above code using user-level LaTeX3 commands? I found \SplitList command in xparse package. Can it help?

Best Answer

In LaTeX3, the preferred way to get user-level functions is xparse and its \NewDocumentCommand function.

If the list was not a comma-separated list, then you would have to do something like

\ExplSyntaxOn
\NewDocumentCommand{\PrintAnswerList}{>{\SplitList;}m}
  { \tl_map_inline:nn {#1} { \PrintAnswer {##1.ans} } }
\ExplSyntaxOff
\PrintAnswerList { file01 ; file 02 ; file03 }

Then \PrintAnswer would be performed outside the scope of the expl syntax.

Here, your life is both slightly easier because you have a comma-separated list, and slightly harder because it is not given directly, but is given hidden inside a macro, \inputfiles. In the code below, I used \clist_map_inline:on, which expands its clist argument once before performing the second argument for each item. Since this particular variant is not available in the kernel, we need to provide it, with

\clist_generate_variant:Nn \clist_map_inline:nn { o }

All in all, you can do for instance (I changed \PrintAnswer too)

\documentclass{book}
 ...
\usepackage{expl3,xparse}
\ExplSyntaxOn
\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~****} }
  }
\cs_generate_variant:Nn \clist_map_inline:nn {o}
\NewDocumentCommand { \PrintAnswerList } { m }
  { \clist_map_inline:on {#1} { \PrintAnswer {##1.ans} } }
\ExplSyntaxOff

\begin{document}

\PrintAnswerList{\inputfiles}

\end{document}
Related Question