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'm trying to create a dynamic for loop:

\def\forinmacro{{a,b,c,d}}
\iftb{SomeBool}{\def\forinmacro{{a}}}
\foreach \m in \forinmacro {...}

Note if I change the last line to

\foreach \m in {a,b,c,d} {...}

it works.

Unfortunately the comma's seem to screw up the foreach and it gives errors(changing a,b,c,d to a lets it work).

Why is \def\forinmacro{{a,b,c,d}} breaking the foreach but \def\forinmacro{{a}} not?

share|improve this question
    
Please add a minimal working example (MWE). –  percusse Jul 29 '12 at 17:34
    
Both your instances of \forinmacro have only one element. –  egreg Jul 29 '12 at 21:31
add comment

1 Answer 1

up vote 5 down vote accepted

You need to remove the extra braces.

\documentclass{article}
\usepackage{pgffor}
\usepackage{xparse, ifthen}
\DeclareDocumentCommand\iftb{m m G{\relax}}{\ifthenelse{\boolean{#1}}{#2}{#3}}
\begin{document}
\newboolean{SomeBool}\setboolean{SomeBool}{true}
\iftb{SomeBool}{\def\forinmacro{a}}
\foreach \m in \forinmacro {This is letter : \m\par}
\def\forinmacro{x,y,w,z}
\foreach \m in \forinmacro {This is letter : \m\par}
\end{document}
share|improve this answer
add comment

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.