# Problem using macro with TikZ foreach

I am using a simple macro adapted from an answer to the following SO question:

https://stackoverflow.com/questions/3951392/latex-space-after-every-letter

The modified macro simply takes a string and adds a newline after each letter...

It works fine until I try to use it with a TikZ foreach loop at which point instead of returning the string with a char per line it returns the string...

Any thoughts on how or if the issue can be resolved?

The LaTeX is:

\documentclass[twoside,letterpaper,12pt]{article}

\usepackage{tikz}

\makeatletter
\newcommand{\charperline}[1]{%
\@tfor\letter:=#1\do{%
\letter\\%
}%
}
\makeatother

\begin{document}
\charperline{This Works}
\foreach \tmp in {Not This,Or This} {
\charperline{\tmp}
}
\end{document}


And the following is the result from pdflatex:

There is nothing noteworthy in the log file.

EDIT

Just adding a slightly simpler version...

\documentclass[twoside,letterpaper,12pt]{article}

\usepackage{pgffor}

\makeatletter
\begin{document}
\foreach \tmp in {Not This,Or This} {
\@tfor\letter:=\tmp\do{%
\letter\\%
}%
}
\makeatother
\end{document}

• Welcome to TeX.SX! – Andrew May 8 '15 at 5:17

The problem is that you need to force \tmp to be expanded as a sequence of letters whereas at the moment \charperline is seeing it only as a single token. I am sure that there is an approved way of doing this but here is a way that I have used to get around similar problems in the past:

Edit As percusse says in the comments, a better solution is to use:

\foreach \tmp in {Not This,Or This} {
\expandafter\charperline\expandafter{\tmp}
}

\documentclass[twoside,letterpaper,12pt]{article}

\usepackage{tikz}
\makeatletter
\newcommand{\charperline}[1]{%
\@tfor\letter:=#1\do{%
\letter\\%
}%
}
\makeatother

\begin{document}
\charperline{This Works}
\foreach \tmp in {Not This,Or This} {
\edef\Charpy{\noexpand\charperline{\tmp}}
\Charpy
}
\end{document}


The \edef forces \tmp to expand so that \Charpy is defined to be \charperline{Not This} (when \tmp is "Not This"), and then \Charpy applies \charperline to "Not This" as you want it to:

• \expandafter\charperline\expandafter{\tmp} should also work. – percusse May 8 '15 at 5:39
• @percusse I am confused because, for me, \expandafter\charperline\expandafter{\tmp} does not work?? If instead I try \expandafter\charperline\expandafter\tmp it is a little better in that the "N" from "Not this" is on its' own line, but not the rest... (I am nervous however as whenever I "correct" egreg I am invariably wrong and you understand this much better than I do:) – Andrew May 8 '15 at 5:43
• I'm definitely not in that league but it works when I replace the body of the foreach loop with that one liner. So it becomes \foreach\tmp in{Not This,Or This}{\expandafter\charperline\expandafter{\tmp}}. – percusse May 8 '15 at 5:48
• Thanks guys! Both of the solutions worked for me. I had tried \expandafter but clearly don't understand it fully because I tried to use a single \expandafter before \charperline{\tmp} with no effect. And I've learned something new with the use of \edef and \noexpand. Thanks!! – Mike N. May 8 '15 at 5:51
• @percusse Yep, it was me: I missed the second \expandafter... :( This is a much better solution than mine. – Andrew May 8 '15 at 5:52