# How to get the value of counter in pgffor while using \expandafter

I looked into this answer and tried to improve it. My goal is to get this table:

| 1 |
| 2 |
| 3 |


| 1 |
| 1 |
| 1 |


Code:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pgffor}
\makeatletter
\newtoks\@tabtoks
\newcommand*\resettabtoks{\global\@tabtoks{}}
\newcommand*\printtabtoks{\the\@tabtoks}
\makeatother
\begin{document}
\resettabtoks
\foreach \i in {1,...,3} {%
}

\begin{tabular}{ | c | }
\hline
\printtabtoks
\end{tabular}
\end{document}


I think, it's caused by \expandafter, but I don't know, how to fix it.

• I added code to my answer for coping with your problem and also the one in your other question – egreg Jul 26 '16 at 9:47

you want to add the expansion of \i not the token \i so

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pgffor}
\makeatletter
\newtoks\@tabtoks
\newcommand*\resettabtoks{\global\@tabtoks{}}
\newcommand*\printtabtoks{\the\@tabtoks}
\makeatother
\begin{document}
\resettabtoks
\foreach \i in {1,...,3} {%

• @user4035 your code added \i three times producing a token register \i\\ \i\\ \i\\  so when you used it you got the final value of \i three times. This uses \expandafter to expand \i before adding it to the register so the register holds 1\\ 2\\ 3\\  so when you use it you get 1 2 3 – David Carlisle Jul 26 '16 at 9:13