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.

This question is directly related to Foreach inside a TikZ matrix. But I can't expand it with a macro inside, such that:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usepackage{etoolbox}

\begin{document}
\newcommand{\somecommand}[1]{#1}
\begin{tikzpicture}
  \let\mymatrixcontent\empty
  \foreach \c in {a,b,c}{%
    \expandafter\gappto\expandafter\mymatrixcontent\expandafter{\somecommand{\c} \\}%
    % or
    % \xappto\mymatrixcontent{\expandonce{\somemacro{\c}\\}}
  }
  \matrix [matrix of nodes,ampersand replacement=\&] {
    \mymatrixcontent
  };
\end{tikzpicture}

\end{document}

I get the error: ERROR: Missing } inserted.

share|improve this question
    
\mymatrixcontent expands to `\c \\\c \\\c \`. –  egreg May 29 '12 at 14:17
    
Yes, but how do I expand \c in the foreach loop at the same time..? –  aagaard May 29 '12 at 14:20

2 Answers 2

up vote 6 down vote accepted

If you add \show\mymatrixcontent before \matrix, you can see that the expansion is

\c  \\\c  \\\c  \\

which is not what you want. You have to expand \c before feeding it as an argument to \somecommand. You can do it by

\newcommand{\somecommand}[1]{#1}
\begin{tikzpicture}
  \let\mymatrixcontent\empty
  \def\gapptoaux#1{\gappto\mymatrixcontent{\somecommand{#1} \\}}
  \foreach \c in {a,b,c}{
    \expandafter\gapptoaux\expandafter{\c}
  }
  \matrix [matrix of nodes,ampersand replacement=\&] {
    \mymatrixcontent
  };
\end{tikzpicture}
share|improve this answer
    
Thanks, that was really helpful! –  aagaard May 29 '12 at 14:44

I have had exactly the same problem in a different context (the loop counter not being expanded in a table cell). To practice my understanding of how the expansion works I tried to fix the problematic line in the original code directly (commented in the code below) - two more \expandafters do the job.

Egreg's solution looks nicer and cleaner but the crude way might be easier to understand for those like me who have only just met \expandafter. Hope this helps someone.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usepackage{etoolbox}

\begin{document}
\newcommand{\somecommand}[1]{#1}
\begin{tikzpicture}
  \let\mymatrixcontent\empty
  \foreach \c in {a,b,c}{%
  %------------------------------
    % original code:
    %\expandafter\gappto\expandafter\mymatrixcontent\expandafter{\somecommand{\c} \\}%
    % changed to:
    \expandafter\gappto\expandafter\mymatrixcontent\expandafter{\expandafter\somecommand\expandafter{\c} \\}%
  %------------------------------
    % or
    % \xappto\mymatrixcontent{\expandonce{\somemacro{\c}\\}}
  }
  \matrix [matrix of nodes,ampersand replacement=\&] {
    \mymatrixcontent
  };
\end{tikzpicture}

\end{document}
share|improve this answer

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.