4

This question is similar to TikZ \foreach loop with macro-defined list but here I'd like the macro-defined list to take an argument.

For example, in the following MWE:

\documentclass{article}
\usepackage{pgffor}
\begin{document}
\begin{description}
\item[Macro without argument]
  \newcommand{\macrowithoutargument}{0,...,10}%
  \foreach \x in \macrowithoutargument {[\x]}
\item[Macro with argument]
  \newcommand{\macrowithargument}[1]{#1,...,10}%
%  \foreach \x in \macrowithargument{1} {[\x]}
\end{description}
\end{document}

the \foreach loop with:

  • macro-without-argument-defined list (\macrowithoutargument) works like a charm,
  • macro-with-argument-defined list (\macrowithargument{1}) fails.

Please note that I'm stick with TikZ's \foreach loops: the previous MWE is a minimization of a problem I encounter with TikZ picture where I'd like to draw just a part of a protactor, with the start and end angles given as arguments of a macro.

Hence my question: is it possible for the TikZ's \foreach loop list to be defined with a macro that takes argument(s)?

  • 3
    Workaround: \edef\tmp{\macrowithargument{1}} \foreach \x in \tmp {[\x]} – Steven B. Segletes Nov 17 '16 at 19:29
  • 1
    I think a better solution is to put the whole for loop in a macro. \foreach is programmed to accept either a list, or a macro without arguments that produces a list. – Pieter van Oostrum Nov 17 '16 at 20:02
  • Do you mean something like the \VoltageCurve command in tex.stackexchange.com/questions/339611/… ? – Andrew Nov 17 '16 at 20:22
  • @Andrew Yes, except I wanted the macro with parameter to be used as list, not to put the to put the whole for loop in a macro, as answered by David or suggested by Piet. – Denis Bitouzé Nov 17 '16 at 20:27
  • @StevenB.Segletes Write an answer with your "workaround"! – Paul Gaborit Nov 17 '16 at 23:28
7

I'd do

\documentclass{article}
\usepackage{pgffor}
\begin{document}
\begin{description}
\item[Macro without argument]
  \newcommand{\macrowithoutargument}{0,...,10}%
  \foreach \x in \macrowithoutargument {[\x]}
\item[Macro with argument]
  \newcommand{\macrowithargument}[1]{\foreach \x in {#1,...,10}}%
  \macrowithargument{1} {[\x]}
\end{description}
\end{document}
| improve this answer | |
  • Thank you so much. It is so easy when well written. I'll use my \irange and \jrange now – Tinmarino Sep 1 '18 at 9:37

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.