Graphics, Figures & TablesTable with Number of Columns as Parameter

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
segovia
Posts: 2
Joined: Sat Jul 02, 2011 6:18 am

Table with Number of Columns as Parameter

Post by segovia »

I am trying to create an environment for building a table. The number of columns wanted would be passed as a parameter. This is my MWE:

Code: Select all

\documentclass{amsart}

\usepackage{ifthen}
\newcounter{nbcol}
\newcounter{nbcolt}
\newcommand{\tmpcmd}{}

\newenvironment{tabledl}[1]{
\setcounter{nbcol}{#1}
\setcounter{nbcolt}{#1}
\renewcommand{\tmpcmd}{\whiledo{\value{nbcolt} > 0}{c\addtocounter{nbcolt}{-1}}} %builds a string of 'c's
\bigskip
\begin{center}\begin{tabular}{\tmpcmd}
}{
\end{tabular}\end{center}}

\begin{document}

\begin{tabledl}{3}
a&b&c\\d&e&f
\end{tabledl}

\end{document}
When compiling, I get this error message:

Code: Select all

! Argument of \@tempc has an extra }.
<inserted text>
                \par
l.23 \begin{tabledl}{3}
The error is caused by the line

Code: Select all

\renewcommand{\tmpcmd}{\whiledo{...
because if I replace with

Code: Select all

\renewcommand{\tmpcmd}{ccc}
everything works just fine.

Could someone tell me what is going on and how to fix the problem? I have been using LaTeX for a few years, but I don't have much experience in TeX/LaTeX programming.

Thank you!

Daniel

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

Learn LaTeX easily with newest books:

The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis

The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more

LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis

User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Table with Number of Columns as Parameter

Post by localghost »

If I understand you right, you want to create a table with a certain number of identical column types. In this case the solution is very simple.

Code: Select all

\begin{tabular}{*{10}{c}}
% table content
\end{tabular}
If that's not what you want, please enlighten me.


Best regards and welcome to the board
Thorsten
segovia
Posts: 2
Joined: Sat Jul 02, 2011 6:18 am

Table with Number of Columns as Parameter

Post by segovia »

Thank you Thorsten for your speedy reply.

Indeed, you provide me with a way to solve my problem! All it takes is

Code: Select all

\begin{center}\begin{tabular}{*{\value{nbcol}}{c}}
which is much simpler than what I was trying to do... :oops:

I am however still curious about what was causing the bug, as that way of doing things might be useful in some other context.

Thanks again, and have a nice week-end!

Daniel
Post Reply