[Tex/LaTex] Issue with \loop to create contents of tabular row

conditionalsloopsmacrostables

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}

enter image description here

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

Best Answer

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}
Related Question