1

Possible Duplicate:
How to programmatically make tabular rows using \\whiledo ?

I have a problem that may have an interesting answer. I am writing a macro whose output I would like to be a collection of rows to use within a tabular environment. I would like to do something similar to the following:

\begin{tabular}{stuff}
  \myMacro{}
\end{tabular}

and the net effect would be,

\begin{tabular}{stuff}
  row1\\
  row2\\
\end{tabular}

However, the following code:

\documentclass{report}
\usepackage{xparse}
\usepackage{etoolbox}

\newbool{myBool}%
\newcounter{rownum}%
\setcounter{rownum}{0}

\NewDocumentCommand\buildrows{}{%
\booltrue{myBool}%
\whileboolexpr{ bool {myBool} }{%
   \stepcounter{rownum}%
   \ifnumless{\therownum}{5}%
      {\therownum\\}%
      {\boolfalse{myBool}\setcounter{rownum}{0}}%
}%
}

\begin{document}
\noindent
\begin{tabular}{|c|}
\buildrows{}
\end{tabular}\\
Above is the output from buildrows, called inside of tabluar.
\hrule

\vspace{1cm}
\noindent
\buildrows{}
Above is the output from buildrows, after using tabular.
\hrule

\vspace{1cm}
\noindent
\buildrows{}
Above is the output from buildrows, after using buildrows.
\hrule

\vspace{1cm}
\noindent
\begin{tabular}{|c|}
1
2
3
4
5
\end{tabular}\\
This is what I think the first command is doing, but its not.
\hrule
\end{document}

(where the command in question is: {\therownum\\}%) is doing some weird things.

  • First, and this is 'obvious' after thinking about it, rather than printing the \\ to use in tabular, a new line is started and the loop progresses. How can I have the loop print the \\ so that it is recognized by tabular? Subbing \textbackslash doesn't work.
  • Second, what is tabular doing to the counter so that it starts at 2 the second time around?

Thanks for any help you can give!

marked as duplicate by Stephan Lehmke, Jake, David Carlisle, egreg, Marco Daniel May 1 '12 at 9:01

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Thanks for pointing that out. It has solved the problem of the "\\" (although I'm not sure how) but now I have a counter issue! Gah. – Scott H. May 1 '12 at 3:47
  • Actually, I should be able to figure it out from here. Thanks again. – Scott H. May 1 '12 at 4:06

Browse other questions tagged or ask your own question.