In the MWE below, I want to create a \doPlotCoords macro from scratch, by appending \addplot commands:
\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{adjustbox}
\usepackage{tikz}
\usetikzlibrary{pgfplots.groupplots}
\usetikzlibrary{shapes}
\usetikzlibrary{positioning}
\usetikzlibrary{decorations.pathreplacing}
\xdef\doPlotCoords{}
\foreach \r in {0,1,...,10}{%
\xdef\tempCoords{} %
\foreach \c in {0,1,...,10}{ %
\xdef\tempCoords{\tempCoords (\r,\c)\space}
} % end \foreach \c
% just \xdef concatenating gives "! Undefined control sequence." for \addplot; use \protected@edef
% \xdef\doPlotCoords{ \doPlotCoords
% \noexpand\addplot+[color=blue,mark=*,mark options={blue},] coordinates{ \tempCoords };}
% with "\protected@xdef", I get "! You can't use a prefix with `the character @'.":
\makeatletter %
\protected@xdef\doPlotCoords{ \doPlotCoords
\protect\addplot+[color=blue,mark=*,mark options={blue},] coordinates{ \tempCoords };}
\makeatother
} % end \foreach \r
\begin{document}
\show\doPlotCoords
\end{document}
Eventually, I'd like the \doPlotCoords macro to contain:
\addplot+[color=blue,mark=*,mark options={blue},] coordinates{ (0,0) (0,1) ... };
\addplot+[color=blue,mark=*,mark options={blue},] coordinates{ (1,0) (1,1) ... };
...
... so I could just use \doPlotCoords as a "subroutine" to execute all those \addplots; however, currently it fails with:
! You can't use a prefix with `the character @'.
<to be read again>
@
l.26 }
% end \foreach \r
I think the main problem is that: upon the first \xdef, \protect/\noexpand may be obeyed; but when concatenating, the \doPlotCoords already contains the \addplot, so when the \xdef tries to expand \doPlotCoords, it also wants to expand the \addplot inside. But I cannot really see what the "You can't use a prefix with `the character @'." should refer to, when using \protected@xdef (got that via Minimal \protected@edef example).
So how could I build a macro using a \edef\mymacro{\mymacro something else} type concatenation, such that the protected tokens inside \mymacro are not expanded when the \edef runs multiple times on \mymacro? (I kind of think that this "multiple times" might justify calling this approach "recursive macro concatenation"; but I'm not sure, as this isn't a "real" recursion of the function-calling-itself kind...)
\xdeforiginally, but I cannot recall what the problem was, but it made me look further, and at that point I found\protected@xdefand I used it; good to know it isn't really needed. Cheers! – sdaau Jun 02 '14 at 23:32\let\addplot\relax– David Carlisle Jun 02 '14 at 23:34