Iterating through comma-separated arguments

etoolbox's list processing capabilities are straight forward:

enter image description here

\documentclass{article}
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\newcommand{\ppath}[2][$\;\triangleright\;$]{%
  \def\nextitem{\def\nextitem{#1}}% Separator
  \renewcommand*{\do}[1]{\nextitem\textsf{##1}}% How to process each item
  \docsvlist{#2}% Process list
}
\begin{document}
A decent file path is \ppath{File,New,Document}.
\end{document}

The separator \nextitem is defined to do nothing during its first use. \do defines how each item is processed, while \docsvlist processes a comma-separated list. See Cunning (La)TeX tricks for a short discussion on the use of \nextitem.


Found an answer in another question's answer. With @Werner's help (specifically the deferred \def trick), the pure TeX solution works without the need for extra packages. What follows is a minimal working example of what I was looking for.

\documentclass{article}

\makeatletter
\newcommand{\ppath}[2][ $\triangleright$ ]{%
  \def\nextitem{\def\nextitem{#1}}%
  \@for \el:=#2\do{\nextitem\el}%
}
\makeatother

\begin{document}
  A decent file path is \ppath{File,New,Why}.  I said, Why.
\end{document}

Output:

Output

(Thanks @Peter, @jon.)


Here is a solution with LaTeX3, which might be simpler

\documentclass{standalone}
\ExplSyntaxOn
\NewDocumentCommand \ppath { O{$\;\triangleright\;$} m } {
  \group_begin:
  \clist_set:Nn \l_tmpa_clist { #2 }
  \clist_use:Nn \l_tmpa_clist { #1 }
  \group_end:
}
\ExplSyntaxOff
\begin{document}
A decent file path is \ppath{File,New,Document}.
\end{document}

The output is

enter image description here

Some explanations:

  1. We declare a document command with an optional argument with a default value and a mandatory one
  2. We wrap the commands in a group to use local variables
  3. We store the mandatory argument in a scratch variable named \l_tmpa_clist of type clist
  4. We "use" that variable to display the list with the given separator

The LaTeX3 commands documentation is available on CTAN as interface3.pdf and may be available locally with texdoc interface3.