Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I've looked all over and can't seem to find a succinct answer to this question. Is it possible (and if so, how) to create a command that will iterate through its `arguments' of comma-separated values and act upon them?

Example mostly stolen from "For loop" in newcommand:

\newcommand{\pdfappendix}[1]{
    for \image in #1
    {
       \includegraphics[scale=0.6]{\image.pdf}
    }
}

The goal here is to create a command that can print a file menu structure (or some arbitrary path) like so:

\ppath{Command,TeXing Options,Generate PDF (C-c C-t C-p)}

potentially with an optional argument for a delimiter (defaulting to \to or something)

which would produce

Command -> TeXing Options -> Generate PDF (C-c C-t C-p)

In the words of holy ed, ?

share|improve this question
1  
There are plenty of questions for lists see tex.stackexchange.com/a/19761/963 and maybe you can post a more specific question –  Yiannis Lazarides Dec 17 '12 at 21:03
1  
Can you explain in how far the answers in the questions you linked don't fulfil your needs? –  Stephan Lehmke Dec 17 '12 at 21:13
    
For a more specific question, see Understanding a \@for loop. –  Alan Munn Dec 17 '12 at 21:22
    
It would also be beneficial to peruse the posts tagged with comma-separated-list. –  Werner Dec 17 '12 at 21:44
    
Actually did that. I'll go through them again - I skipped a few where I didn't foresee the general answer. –  Sean Allred Dec 17 '12 at 22:29

2 Answers 2

up vote 10 down vote accepted

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.

share|improve this answer
    
Very clever fix for the first item bit. –  Sean Allred Dec 17 '12 at 23:35

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.)

share|improve this answer
1  
You need to use \@for \el:=#2\do{\nextitem\el}%. But this really should have been a separate question. –  Peter Grill Dec 18 '12 at 22:26
1  
It looks like you forgot a % at the end of line 6: \@for ... \el}%. –  jon Dec 18 '12 at 22:27
    
I should have made it far more clear it was rhetorical (but I was actually about to ask a separate question, funnily enough). That clears up on side of it, but not the other. I found that (in the original) the macro was padding the output with a single space. It makes sense that your fix takes care of the one on the right and not on the left. I'm wondering now if the \def is giving us an intermittent space for some reason. –  Sean Allred Dec 18 '12 at 22:30
    
Found it, and I'm dumb. Missed a % after the first line in my typeup. Edited. –  Sean Allred Dec 18 '12 at 22:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.