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 have, for example, a set of multidimensional values: {{red,1,12pt},{blue,3,10pt},{green,2,15pt}}. With \psforeach I want to iterate the values with 3 variables: \clr, \x, and \size.

Is there a trick to iterate a set of multidimensional values with \psforeach?


Edit: By combining the existing answers, the following is the result.

\documentclass{article}
\usepackage{pstricks-add}

\usepackage[active,tightpage]{preview}
\PreviewBorder=12pt
\PreviewEnvironment{pspicture}


\def\getValues#1,#2,#3\relax{\def\clr{#1}\def\x{#2}\def\size{#3}}
\makeatletter

\begin{document}
\begin{pspicture}[showgrid=top](-3,-3)(3,3)
    \@for\abc:={{red,1,12pt},{blue,3,10pt},{green,2,15pt}}% this percent sign is needed!
    \do{\expandafter\getValues\abc\relax
        \pscircle[linecolor=\clr,linewidth=\size](0,0){\x}
        % other graphics objects go here!
    }
\end{pspicture}
\end{document}

enter image description here

share|improve this question
    
you can split the argument of psforeach with something like this: \def\tempa#1,#2,#3{\def\clr{#1}\def\x{#2}\def\size{#3}}\tempa\argumentofpsforea‌​ch\relax –  Marco Daniel Jun 19 '12 at 17:26
    
@MarcoDaniel: Could you show the complete solution as an answer? –  Oh my ghost Jun 19 '12 at 22:54
    
@Forgiver: you can simplify it with \do{\expandafter\psCircle\abc\relax} and \def\psCircle#1,#2,#3\relax{\pscircle[linecolor=#1,linewidth=#3]{#2}} –  Herbert Jun 23 '12 at 10:54
    
@Herbert:But I need \clr, \x, and \size for other usage. –  Oh my ghost Jun 23 '12 at 15:50
    
ok, then you're right. –  Herbert Jun 23 '12 at 15:59

3 Answers 3

up vote 4 down vote accepted
+50

only the first argument has to be put into double braces:

\documentclass{minimal}
\usepackage{pstricks}
\def\temp#1{\expandafter\tempB#1\relax}
\def\tempB#1,#2,#3\relax{Colour: #1; X: #2; Size: #3 \par}

\begin{document}
\psforeach{\abc}{{{red,1,12pt}},{blue,3,10pt},{green,2,15pt}}{%
  \expandafter\temp\expandafter{\abc}}

\end{document}

and the same without loading any package:

\documentclass{minimal}

\def\tempB#1,#2,#3\relax{Colour: #1; X: #2; Size: #3 \par}
\begin{document}
\makeatletter

\@for\abc:={{red,1,12pt},{blue,3,10pt},{green,2,15pt}}%
  \do{\expandafter\tempB\abc\relax}
\end{document}
share|improve this answer
    
Thanks for answering. But why does the first element have to be enclosed by an extra pair of braces? –  Oh my ghost Jun 23 '12 at 8:17
    
the first argument is expanded to see if there is something like 1,..,3. However, you can call it a bug or a feature ... :-) –  Herbert Jun 23 '12 at 9:05
    
Hopefully this bug fix will be announced in the incoming pst-news. :-) –  Oh my ghost Jun 23 '12 at 16:35
2  
I'll do my very best ... :-) –  Herbert Jun 23 '12 at 16:56

How about using pgffor:

enter image description here

\documentclass{article}
\usepackage{pgffor}
\begin{document}
\def\tempa#1,#2,#3\relax{\def\clr{#1}\def\x{#2}\def\size{#3}}
\foreach \abc in {{red,1,12pt},{blue,3,10pt},{green,2,15pt}}{
  \expandafter\tempa\abc\relax%
  Colour: \clr; X: \x; Size: \size\par
}%
\end{document}

Each element in \foreach, called \abc is expanded and passed to \tempa, which breaks it up into the three components as \clr, \x, \size.

share|improve this answer

The macro \cptdofunclist doesn't need any delimiter on the last argument of \do. It can see.

\documentclass{article}
\usepackage{catoptions}
\usepackage{graphicx}
\usepackage{xcolor}

\begin{document}
\def\do#1,#2,#3{Colour: #1; X: #2; Size: #3\par}
\cptdofunclist[;]{red, 1, 12pt; blue, 3, 10pt; green, 2, 15pt}

\par\bigskip
\def\do#1,#2,#3,#4{\scalebox{#3}[#4]{\textcolor{#1}{#2}}}
\cptdofunclist[;]{red, Red, 2, 3; blue, Blue, 4, 5; green, Green, 6, 7}
\end{document}

enter image description here

Or, if you insist on using pgffor:

\documentclass{article}
\usepackage{pgffor}
\begin{document}
\def\tempa(#1,#2,#3){Colour: #1; X: #2; Size: #3\par}
\foreach \abc in {(red,1,12pt),(blue,3,10pt),(green,2,15pt)}{%
  \expandafter\tempa\abc\relax
}
\end{document}
share|improve this answer

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.