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\textbackslashdoesn'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!