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.
\documentclass{article}
\usepackage{forarray}
\begin{document}
  \newcommand{\mytext}[1]{\gdef\themytext{#1}}
  \newcommand{\mytest}[1]{\ForEach{,}{\typeout{\thislevelitem}}{#1}}

  % this one works, it prints
  % x
  % y
  % z
  \mytest{x,y,z}

  % this one doesn't work, it prints
  % x,y,z
  \mytext{x,y,z}
  \mytest{\themytext}

\end{document}

This is a simplified example of an issue I'm facing writing a LaTeX class file. I can't get \ForEach to work when the list parameter comes via a global macro definition.

How can I modify the definition of \mytex so that the example works?

Thanks

share|improve this question
    
I suppose that \ForEachX is what you need. –  egreg Jun 22 '12 at 13:41
    
@egreg \ForEachX would be easier –  StrongBad Jun 22 '12 at 14:00
    
Yes, \ForEachX fixes this issue for me very cleanly. Thank you. –  fosstel Jun 22 '12 at 14:06
add comment

2 Answers 2

This works for me:

\documentclass{article}
\usepackage{forarray}
\begin{document}
  \newcommand{\mytext}[1]{\gdef\themytext{#1}}
  \newcommand{\mytest}[1]{\ForEachX{,}{\typeout{\thislevelitem}}{#1}}

  % this one works, it prints
  % x
  % y
  % z
  \mytest{x,y,z}

  % this one works, it prints
  % x
  % y
  % z
  \mytext{x,y,z}
  \mytest{\themytext}

\end{document}

Notice the use of \ForEachX, which expands the third argument, instead of \ForEach.

share|improve this answer
add comment

If you are willing to modify how you call \mytest, then

\expandafter\mytest\expandafter{\themytext}

gives

x

y

z

or you could define \mytest differently

\makeatletter
  \newcommand{\mytest}[1]{\edef\mylist{#1}%
    \expandafter\@mytest\expandafter{\mylist}}%
  \newcommand{\@mytest}[1]{\ForEach{,}{\typeout{\thislevelitem}}{#1}}
\makeatother
share|improve this answer
    
Thanks, the first option is fine with me to use inside my class. By extension, how would the syntax be if \mytest would have two parameters instead of one, both to be expanded? \expandafter\mytest\expandafter{\something}\expandafter{\themytext} doesn't work. –  fosstel Jun 22 '12 at 13:48
    
If there are two arguments, you are going to need a messy chain of \expandafters. I would consider redefining mytest. The chain would look like: \expandafter\expandafter\expandafter\mytest\expandafter\expandafter\expandafter‌​{\expandafter\something\expandafter}\expandafter{\themytext} –  StrongBad Jun 22 '12 at 13:57
    
Yeap, that's messy indeed! Thank you again. –  fosstel Jun 22 '12 at 14:00
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.