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.

Is there a way to use the TikZ \foreach command to do operations with parameters?

E.g. I would like to be able to write (just a silly example):

\newcommand{\foo}[9]{

    \foreach \x in{1,2,...,9}{
        \draw(0,#\x)--++(1,1);

    }
}

where #\x would mean the x th parameter of the command I'm defining with \newcommand.

This should come in handy since I'm writing macros that depend on global values that can be set arbitrarily. E.g. \def\N{9} would go in preamble and I'd write newcommand{\bar}[\N]{...}, but since I don't know beforehand how much parameters there will be, I can't define anything unless I write \foreach \x in{1,2,...,\N} and the above draw command.


What I actually like to do:

I'd like to have a command that plots some coordinates which are given. So I'd like \foo{#1}{#2}{#3} to expand to something like;

\draw plot coordinates {(360/\N*1:#1) (360/\N*2:#2) (360/\N*3:#3) ... (360/\N*\N:#N};

but I don't know if this is possible.

share|improve this question
    
can be done in the same way as I already pointed out: \def\saveCoordinates{} and then instead of the \draw do \xdef\saveCoordinates{\saveCoordinates(360/\N*2:#2)}. However, I do not understand the meaning of \N here –  Herbert Feb 3 '11 at 14:05
    
Great thanks! Where did you point that out exaclty? Was it in this question, I don't seem to find it. \N would mean the fraction of the full circle and is also the number of command parameters. –  romeovs Feb 3 '11 at 14:31

2 Answers 2

up vote 6 down vote accepted

I do not reall understand what exactly do you want to draw, but maybe this helps:

\documentclass{minimal}
\usepackage{tikz}

\makeatletter
\def\foo#1{\foo@i#1,,\@nil}%
\def\foo@i#1,#2,#3\@nil{%
  \draw(0,#1)--++(1,1);
  \ifx\relax#2\relax\else\foo@i#2,#3\@nil\fi}
\makeatother

\begin{document}
\begin{tikzpicture}
\foo{1,2,3,4,5,6,7,8,9}
\end{tikzpicture}
\begin{tikzpicture}
\foo{1,2,3,4,5,6}
\end{tikzpicture}

\end{document}
share|improve this answer
    
Not what I asked for but way better! This helps a lot thanks! –  romeovs Feb 3 '11 at 13:21

(Substantially revised answer.) The following should read in and draw as many arguments as you like, one at a time, provided that \N is defined and stores a positive integer.

\makeatletter
  \newcount\c@foo
  \newcommand\foo{%
    \c@foo=1\relax
    \@foo}
  \newcommand\@foo[1]{%
    \draw(0,#1)--++(1,1);%
    \advance\c@foo by 1\relax%
    \ifnum\c@foo>\N%
       \let\@tempa\relax
    \else
       \let\@tempa\@foo
    \fi
    \@tempa}
\makeatother
share|improve this answer
    
I know this is what I could do. But the example I gave is a simplification of what I really would want like to do. I've edited the question to make this clear. –  romeovs Feb 3 '11 at 13:01
    
I've revised my answer. It looks like Herbert's is more versatile, but this should also do what you want. –  Niel de Beaudrap Feb 3 '11 at 13:28

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.