# Issue with \loop to create contents of tabular row

I want to create a table where items in columns are alphabetic A,B,C,......,M, and I would like insert them with a loop, here an example

\documentclass{article}

\newcounter{countA}

\def\myline{}
\loop\ifnum\thecountA<12
\stepcounter{countA}
\expandafter\def\expandafter\myline\expandafter{%
\myline
\Alph{countA} &
}%
\repeat

\begin{document}

\centering

\begin{tabular}{|*{13}{c|}}
\hline
A & B & C & D & E & F & G & H & I & G & K & L & M \\
\hline
\end{tabular}

\bigskip

\begin{tabular}{|*{13}{c|}}
\hline
\myline M\\
\hline
\end{tabular}

\end{document}


The result is a tabular where all cells contain the same letter L, how can I fix myline macro to obtain same as the first table, or if there another method to create such loop in tabular

The problem is that \myline is not expanded. Using \edef instead of \def and removing al the expandafters solves this.

\documentclass{article}

\newcounter{countA}

\def\myline{}
\loop\ifnum\thecountA<12
\stepcounter{countA}
\edef\myline{%
\myline
\Alph{countA} &
}%
\repeat

\begin{document}

\centering

\begin{tabular}{|*{13}{c|}}
\hline
A & B & C & D & E & F & G & H & I & G & K & L & M \\
\hline
\end{tabular}

\bigskip

\begin{tabular}{|*{13}{c|}}
\hline
\myline M\\
\hline
\end{tabular}

\end{document}


Here is an example without using \loop...\repeat (recursive):

\documentclass{article}

\newcounter{countA}
\def\myline{%
\stepcounter{countA}\Alph{countA}&%
\ifnum\thecountA<12%
\myline
\fi
}

\begin{document}
\centering

\begin{tabular}{|*{13}{c|}}
\hline
A & B & C & D & E & F & G & H & I & G & K & L & M \\
\hline
\end{tabular}

\bigskip

\begin{tabular}{|*{13}{c|}}
\hline
\myline M\\
\hline
\end{tabular}

\end{document}


EDIT: This needs the counter countA to be set to 0 each time the macro is called, because the loop is executed each time \myline is called instead of saving the results of the loop into \myline. One might solve this by inserting a \else\setcounter{countA}{0}, but the loop is still executed everytime.

EDIT2: To allow stuff similar to egreg's "more generic approach" but making it even more generic:

\documentclass{article}

\newcounter{countA}
\newcommand{\myline}[2][0]{%
\ifnum#1>0\setcounter{countA}{#1}\fi%
\stepcounter{countA}\Alph{countA}&%
\ifnum\numexpr#2-1>\value{countA}%
\myline{#2}%
\else%
\stepcounter{countA}\Alph{countA}%
\setcounter{countA}{0}%
\fi%
}

\begin{document}
\centering

\begin{tabular}{|*{13}{c|}}
\hline
A & B & C & D & E & F & G & H & I & G & K & L & M \\
\hline
\end{tabular}

\bigskip

\begin{tabular}{|*{13}{c|}}
\hline
\myline{13}\\
\hline
\myline[13]{26}\\
\hline
\end{tabular}

\end{document}


A more generic approach:

\documentclass{article}
\usepackage{etoolbox}

\newcounter{countA}
\newcommand{\alphline}[1]{%
% I want #1 letters
\setcounter{countA}{1}%
\def\finalline{A}%
\loop\ifnum#1>\value{countA}%
\stepcounter{countA}%
\xappto\finalline{& \Alph{countA}}%
\repeat
\finalline
}

\begin{document}

\begin{tabular}{|*{13}{c|}}
\hline
\alphline{13} \\
% for comparison
A & B & C & D & E & F & G & H & I & G & K & L & M \\
\hline
\end{tabular}

\end{document}


Same idea in expl3 (no counter needed)

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\alphline}{m}
{
\tl_set:Nn \l_tmpa_tl { A }
\int_step_inline:nnnn { 2 } { 1 } { #1 }
{
\tl_put_right:Nn \l_tmpa_tl { & \int_to_Alph:n { ##1 } }
}
\tl_use:N \l_tmpa_tl
}
\ExplSyntaxOff

\begin{document}

\begin{tabular}{|*{13}{c|}}
\hline
\alphline{13} \\
% for comparison
A & B & C & D & E & F & G & H & I & G & K & L & M \\
\hline
\end{tabular}

\end{document}