Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am having trouble with using a loop inside a tabular environment:

\newcounter{it}
\setcounter{it}{0}

\begin{tabular}{ll}
  \loop\ifnum\theit<4
    \addtocounter{it}{1}
    Q & A\\
  \repeat
\end{tabular}

I receive an error about argument of loop having an extra } and the compilation stops at &. I don't get an error when it is a single column table, but even in that case the result is not as expected: I get a single execution of the loop in place of 4.

Can someone tell me what I am missing? Is there special considerations when using loops inside tabular?

share|improve this question
    
I found out that to solve the problem with & I can define something like \def\tand{&} and use \tand in place of &. But I still get an incorrect output. –  Kaveh Apr 1 '12 at 7:05
add comment

5 Answers

up vote 7 down vote accepted

Yep, don't do that. tabulars are really special and macro-unfriendly (for instance every & closes a group).

Try

\def\mylines{}%
\loop\ifnum\theit<4
  \addtocounter{it}{1}
  \expandafter\def\expandafter\mylines\expandafter{%
    \mylines
    Q & A\\
  }%
\repeat

outside the tabular and then

\begin{tabular}{ll}
  \mylines
\end{tabular}
share|improve this answer
add comment

collect all lines with a token register and then print the lines:

\documentclass{article}
\newcounter{it}  \setcounter{it}{0}
\newtoks\tabtoks
\newcommand\addtabtoks[1]{\tabtoks\expandafter{\the\tabtoks#1}}
\newcommand*\resettabtoks{\tabtoks{}}
\newcommand*\printtabtoks{\the\tabtoks}
\begin{document}

\resettabtoks
\loop\ifnum\theit<4
  \stepcounter{it} \addtabtoks{Q & A\\}
\repeat

\begin{tabular}{ll}
\printtabtoks
\end{tabular}

\end{document}
share|improve this answer
1  
This technique has been introduced here by you as well. –  I am who I say I am Apr 1 '12 at 7:09
add comment

I solved my problem by using a while loop instead:

\def\tand{&}

\newcounter{it}
\begin{tabular}{ll}%
  \setcounter{it}{1}%
  \whiledo{\theit<3}{%
    Q \tand A \\%
  \stepcounter{it}%
  }%
  {\theit} \tand \\
\end{tabular}

PS: if wondering why I need to print the last one separately, the reason is that the execution leave a partial line (I am using tabular{|l|l|}).

share|improve this answer
add comment

Your own answer has to deal with the extra line problem. Here is a way to do that without having to separately construct the last line thereby keeping your table line structure in one place inside the loop. I threw in a \hline or two to show they don't upset the applecart either.

Solution Output

\documentclass[12pt]{article}
\usepackage[a6paper,landscape]{geometry}

\usepackage{ifthen}

\def\tand{&}

\begin{document}
%
\newcounter{it}
\begin{tabular}{ll}
\hline
  \setcounter{it}{1}%
  \whiledo{\theit<4}{%
    Q \tand A \\%
    \ifnum\value{it}=3\hline\end{tabular}\fi
  \stepcounter{it}%
  }%
 \par Immediately after the table
%
\end{document}
share|improve this answer
add comment

The latex kernel has some loops, but with the @ letter, thus their use is not so widespread. The \whiledo of ifthen is a wrapper of the \@whilesw of the latex kernel; the latter is expandable, whereas the \whiledo wrapper of ifthen is not. However, here as shown in the answer of the OP's as well as in this answer, one can use with success \whiledo.

It is possible to recycle the \@whilesw of LaTeX and then, there is no problem with the final line in the tabular.

\documentclass[12pt]{article}

% \usepackage{ifthen} % not needed

% \def\tand{&} % not needed

% recycling the \@whilesw of LaTeX into a num test
% pay attention to correctly end explicit numbers in 
% \ifnum tests with a space, if they are last.
\makeatletter
  \def\xwhilenum #1\do{\@whilesw{\ifnum #1}\fi }
\makeatother

\begin{document}
\newcounter{it}

\begin{tabular}{ll}
\hline
  \setcounter{it}{1}%
  \xwhilenum{4>\value{it}}\do {%
    \stepcounter{it}%
     Q & A \\
  }%
\hline
\end{tabular}
\makeatother
 \par Immediately after the tabular
%

\begin{tabular}{ll}
\hline
  before & the loop \\
  \setcounter{it}{1}%
  \xwhilenum{4>\value{it}}\do {%
    \stepcounter{it}%
     Q & A \\
  }%
  still in & the tabular \\
\hline
\end{tabular}
\makeatother
 \par Immediately after the tabular
%
\end{document}

while loop in tabular

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.