In TikZ there is the \foreach construct which one can use to iterate over a list of items and draw them as desired.

Is it possible to encode, say, edge style parameters in the list like so (which unfortunately gives a Runaway Argument error):


      sty1/.style args={#1:#2}{postaction=decorate,decoration={name=markings, mark=at position #1 with {\draw (0,0) -- (1,1) node {#2}; }}},
      sty2/.style args={#1:#2:#3}{postaction=decorate,decoration={name=markings, mark=at position #1 with {\draw (0,0) -- node {#3} (1,1) node {#2}; }}}

    % option 1
    \foreach \a/\b in {sty1/.7:AParam2, sty2/.7:AParam2:AParam3, sty1/.7:AParam2, sty2/.7:AParam2:AParam3}{
        \path (0,0) edge[\a=\b] (1,1);

    % option 2 - the preferred option because I need the number at different independent places
    \foreach \a/\b in {1/.7:AParam2, 2/.7:AParam2:AParam3, 1/.7:AParam2, 2/.7:AParam2:AParam3}{
        \path (0,0) edge[sty\a=\b] (1,1);

As you can see the corresponding style of a list item determines the number of arguments that are given to the style.

Is there maybe a royal road to encode style parameters with a variable argument count in the \foreach list?

What I'm trying to do is to mark edges with a variable number of lines of different color. I want to define the number of lines and colors in the \foreach list along with other parameters for each edge, e.g., the text in an annotation node, in and out angles and stuff like that.

  • Since you're using the style inside the \foreach (which has only one \path) why not use the style explicitly written inside edge? Like in \path (0,0) edge[postaction=decorate,decoration={name=markings, mark=at position {\a} with {\draw (0,0) -- (1,1) node {\b};}}] (1,1);? Jan 17, 2017 at 18:56
  • That's because the \foreach in my actual document does not contain only the \path element. This is just an MWE that ought to show what I'm after. Aside from the \path the \foreach contains nodes that get placed in along an arc around another node. That's why I'm using a \foreach. To be able to modify the number of nodes and the styles only by altering the list and not having to add/remove additional TikZ code. Jan 17, 2017 at 19:46

1 Answer 1


In the OP's code, the styles receive \b as the value, while they expect a value of the form ...:... (sty1) and ...:...:... (sty2). This is why the Runaway argument error is raised. The solution is to expand the value using handler .expanded before passing it to the style, like so:

\path (0,0) edge[sty\a/.expanded=\b] (1,1);

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.