# TeX \@for loop within a section title

I am creating a macro for turning a list of files into a comma-separated list of monospaced file names, and using it as a heading.

I have written a short script for doing so: MWE:

\documentclass[10pt]{article}
\usepackage[usenames]{color} %used for font color
\usepackage{amssymb} %maths
\usepackage{amsmath} %maths
\usepackage[utf8]{inputenc} %useful to type directly diacritic characters

\usepackage{url}
\pagestyle{empty}
\begin{document}

\edef\files{file_1.x,file_2.y,file_3.z}

\edef\comma{}%

\makeatletter
\@for\file:=\files\do{%
\comma%
\edef\comma{, }%
\expandafter\url\expandafter{\file}%
}%

\end{document}


However, I cannot find how I can use the output as a heading: simply wrapping the \@for loop in a \section{} tag throws up Undefined Control Sequence errors. How could I achieve this?

• Are you sure that your loop works? – user31729 Feb 29 '16 at 13:38
• You want that, for each item in the list, \section{\url{<item>}} is issued? – egreg Feb 29 '16 at 14:43
• @egreg No, that the whole list is a single heading. – Charlie Harding Mar 1 '16 at 14:31

Here is a expl3 version with splitting of the list (using \clist...) and automatic setup as \section.

\documentclass[10pt]{article}
\usepackage[usenames]{color} %used for font color
\usepackage{amssymb} %maths
\usepackage{amsmath} %maths
\usepackage[utf8]{inputenc} %useful to type directly diacritic characters

\usepackage{url}
\usepackage{xparse}
\pagestyle{empty}
\ExplSyntaxOn
\newcommand{\splitlist}[2]{%
\clist_set:Nx \l_tmpa_clist {#1}
\clist_map_inline:Nn \l_tmpa_clist {\csname #2\endcsname{\protect\url{##1}}}
}
\ExplSyntaxOff

\begin{document}

\edef\files{file_1.x,file_2.y,file_3.z}

\splitlist{\files}{section}

\end{document}


Update

Since the file list contains _ characters, it's quite comfortable to go to expl3 features and use a \clist variable as well. However, the last item mustn't have a , value, so don't use that (I added a \int_compare conditional for this).

In order to allow such commands in a command with moving arguments like \section, it's better to use \DeclareRobustCommand (instead of using the command with \protect all the time)

\documentclass[10pt]{article}
\usepackage[usenames]{color} %used for font color
\usepackage{amssymb} %maths
\usepackage{amsmath} %maths
\usepackage[utf8]{inputenc} %useful to type directly diacritic characters

\usepackage{url}
\usepackage{xparse}
\pagestyle{empty}

\ExplSyntaxOn

\DeclareRobustCommand{\splitlist}[1]{%
\clist_set:Nx \l_tmpa_clist {#1}
\int_set:Nn \l_tmpb_int {\clist_count:N \l_tmpa_clist}
\clist_map_inline:Nn \l_tmpa_clist {
\int_incr:N \l_tmpa_int
\url{##1}%
% is it the last item? If yes, add no , then!
\int_compare:nNnT{\l_tmpa_int} < {\l_tmpb_int}{,~}
}
}
\ExplSyntaxOff

\begin{document}

\edef\files{file_1.x,file_2.y,file_3.z}

\section{\splitlist{\files}}

\end{document}


• \use:c{#2} is expl3 lingo for \csname#2\endcsname – egreg Feb 29 '16 at 14:44
• How does clist work? Where's the best reference, so I can understand how this works? – Charlie Harding Feb 29 '16 at 15:36
• @CharlieHarding: See the interface3.pdf documentation for expl3 language settings. \clist is a LaTeX 3 variable datatype meant for comma separated lists. – user31729 Feb 29 '16 at 16:18
• I want to wrap the whole list in a section, rather than each individual item in a section (so the title is "file_1.x, file_2.y, file_3.z"). How would I do that? – Charlie Harding Mar 1 '16 at 14:30
• @CharlieHarding: See the update please – user31729 Mar 1 '16 at 14:48