From this answer I learned how to print rows into 1 column table using pgffor:
| 1 |
| 2 |
| 3 |
Code looks like this:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pgffor}
\makeatletter
\newtoks\@tabtoks
\newcommand\addtabtoks[1]{\global\@tabtoks\expandafter{\the\@tabtoks#1}}
\newcommand*\resettabtoks{\global\@tabtoks{}}
\newcommand*\printtabtoks{\the\@tabtoks}
\makeatother
\begin{document}
\resettabtoks
\foreach \i in {1,...,3} {%
\expandafter\addtabtoks\expandafter{\i \\\hline}%
}
\begin{tabular}{ | c | }
\hline
\printtabtoks
\end{tabular}
\end{document}
Then I wanted to improve it and get a table with 2 columns:
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
Using both macro and simple expansion, I created this code:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pgffor}
\newcommand{\myline}[1]{
#1 & #1\\ \hline
}
\makeatletter
\newtoks\@tabtoks
\newcommand\addtabtoks[1]{\global\@tabtoks\expandafter{\the\@tabtoks#1}}
\newcommand*\resettabtoks{\global\@tabtoks{}}
\newcommand*\printtabtoks{\the\@tabtoks}
\makeatother
\begin{document}
%1-st attempt
\resettabtoks
\foreach \i in {1,...,3} {%
\expandafter\addtabtoks\expandafter{\i & \i\\\hline}
}
\begin{tabular}{ | c | c | }
\hline
\printtabtoks
\end{tabular}
%2-nd attempt
\resettabtoks
\foreach \i in {1,...,3} {%
\expandafter\addtabtoks\expandafter{\myline{\i}}
}
\begin{tabular}{ | c | c | }
\hline
\printtabtoks
\end{tabular}
\end{document}
1-st attempt gives:
| 1 | l |
| 2 | l |
| 3 | l |
2-nd attempt gives:
| l | l |
| l | l |
| l | l |
Why is "l" shown instead of \i value? And how to fix?
