7

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

0

3 Answers 3

7

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}
8

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}

results

2

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}

enter image description here

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}
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .