# TikZ foreach loop fails when the list doesn't end with a comma (Part II)

The following question is closely related to this one, however the solution given in the answer to that question does not work for the LaTeX document to be presented below, and therefore I've had to create this other post.

The following LaTeX document is saved in a file whose path is ~/test.tex.

\documentclass[tikz,border=1cm]{standalone}

\newcommand{\placepics}[1]{ \foreach \i in { #1 } { \pic {\i}; } }
\tikzset{ square/.pic={\draw (0,0) rectangle (1,1);} }

\begin{document}

\begin{tikzpicture}
\placepics{
square,
}
\end{tikzpicture}

\end{document}


When the following commands are executed at the terminal:

cd ~
pdflatex test

a PDF file is generated at the path ~/test.pdf. When opened in a PDF viewer, the file displays as follows:

If the comma is now deleted after the second occurrence of the word square in the document at ~/test.tex, so that the \foreach structure looks like this:

\foreach \i in {
square
}{\pic {\i};}


and the command pdflatex test is rerun, the execution fails, and the file ~/test.log contains the following snippet:

! Package pgfkeys Error: I do not know the key '/tikz/pics/square  ' and I am g
oing to ignore it. Perhaps you misspelled it.

See the pgfkeys package documentation for explanation.
Type  H <return>  for immediate help.
...

l.13    }

?
! Emergency stop.
...

l.13    }

End of file on the terminal!


If I now implement the solution suggested in the answer cited above, and place a % symbol at the point where the deleted comma used to be, so that the \foreach structure looks like this:

\foreach \i in {
square%
}{\pic {\i};}


and the command pdflatex test is rerun, the execution fails, and the file ~/test.log contains almost the same snippet as shown above, except that the offending key is given as '/tikz/pics/square ', i.e. the string square is now followed by a single space rather than by two spaces.

1. Why does the execution fail in the third run despite the % symbol that follows the string square as suggested in the answer cited above?

2. Is this a bug in PGF/TikZ? Should I report this to the TikZ project as a bug?

In your defintion of \placepics you add a(nother) space after the option that you pass to \pic namely between #1 and the following closing curly brace:

\newcommand{\placepics}[1]{ \foreach \i in { #1 } { \pic {\i}; } }
%                                              ^


You already found out in part I of your question that spaces (except for leading spaces) are not ignored in such an option list that is passed to a \foreach macro. The problem is the very same here. So, you really need to take care of spaces in such lists.

Removing this space will solve the problem:

\documentclass[tikz,border=1cm]{standalone}

\newcommand{\placepics}[1]{ \foreach \i in {#1} { \pic {\i}; } }
\tikzset{ square/.pic={\draw (0,0) rectangle (1,1);} }

\begin{document}

\begin{tikzpicture}
\placepics{
square%
}
\end{tikzpicture}

\end{document}