15

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, ?

18

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.

  • 2
    Very clever fix for the first item bit. – Sean Allred Dec 17 '12 at 23:35
  • As highlighted in adding \chapter in KOMA scrbook generates a “! LaTeX Error: \do undefined.”, using the \do command might lead to errors because numerous other packages/macros redefines it (e.g. notably \chapter of KOMA's classes). egreg thus suggests here a variant of this answer without using \do. – ebosi May 1 '17 at 13:41
  • Is it possible to handle the last item differently like making \ppah{a,b,c} produce "a, b and c"? – Mr. Ree Aug 23 '17 at 13:17
  • 1
    @Mr.Ree: Try this code (output). – Werner Aug 23 '17 at 20:13
  • @Werner Thanks for your nice code! I've only found some codes which are too complicated to understand before. – Mr. Ree Aug 25 '17 at 9:45
8

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

  • 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

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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