# using \ForEach to input chapters in a document

I have several chapters in the folder `/Chapters` and I have the following LaTeX code:

``````\newcommand\chapnames{Chap1, Chap2, Chap3, Chap4, Chap5, Chap6}

\ForEach
{,}
{\input{./Chapters/\thislevelitem}}
{\chapnames}
``````

Unfortunately, this piece of code does not work unless I change it to:

``````\ForEach
{,}
{\input{./Chapters/\thislevelitem}}
{Chap1, Chap2, Chap3, Chap4, Chap5, Chap6}
``````

Is there a way to make it work while using `\chapnames` variable as shown in the `\ForEach` code at the top?

-
What package are you using for `\ForEach`? –  egreg Jan 6 '13 at 19:22
This might be of interest: How to iterate through the name of files in a folder –  Werner Jan 6 '13 at 19:24
I'm using forarray package. –  Ivan Jan 6 '13 at 19:25
@Qrrbrbirlbel Thanks! That worked nicely :) –  Ivan Jan 6 '13 at 19:38

From the `forarray` manual, subsection 3.1.2 “The command `\ForEachX`”, page 4:

The command `\ForEachX` processes the list of items in the same way as the command `\ForEach`. However, it expands its third argument, a token containing the actual list, before processing it. It has the following syntax:

``````\ForEachX{<separator >}{<function>}{<list token>}
``````

Therefore, your code sample has to be written as

``````\newcommand\chapnames{Chap1, Chap2, Chap3, Chap4, Chap5, Chap6}

\ForEachX
{,}
{\input{./Chapters/\thislevelitem}}
{\chapnames}
``````
-