COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Creating a variable preamble in tabular env

• Email
• Follow

I already have a solution to this in the memoir class (thanks GL and
Lars!), but not for LaTeX's base classes.
Rather than explain, here's an MWE which should be clear:

==== This works =====
\documentclass{article}
\begin{document}

\newcommand{\cols}{ccc}

\begin{tabular}[t]{\cols}
blah & blah & blah \\
blah & blah & blah \\
\end{tabular}
\end{document}

===== This fails =====
\documentclass{article}
\begin{document}
\newcounter{n}

\newcommand{\repeatntimes}[2]{ % usage \repeatntimes{c}{3} -> ccc
\setcounter{n}{0}%
\ifnum\value{n}<#2\repeat}

\newcommand{\cols}{\repeatntimes{c}{3}}

\begin{tabular}[t]{\cols}
blah & blah & blah \\
blah & blah & blah \\
\end{tabular}
\end{document}

(note - \repeatntimes works in all classes, just not in the preamble
of the tabular env)

My instinct is pointing me in the direction of an expansion problem,
i.e. what's getting passed is \begin{tabular}{\cols} not
\begin{tabular}{ccc}.

Anyway, any help would be greatly appreciated  :-) .

Bev

 0

See related articles to this posting

Le 18/11/2010 18:49, bev a �crit :
> I already have a solution to this in the memoir class (thanks GL and
> Lars!), but not for LaTeX's base classes.
> Rather than explain, here's an MWE which should be clear:
>
> ==== This works =====
> \documentclass{article}
> \begin{document}
>
> \newcommand{\cols}{ccc}
>
> \begin{tabular}[t]{\cols}
>     blah&  blah&  blah \\
>     blah&  blah&  blah \\
> \end{tabular}
> \end{document}
>
> ===== This fails =====
> \documentclass{article}
> \begin{document}
> \newcounter{n}
>
> \newcommand{\repeatntimes}[2]{ % usage \repeatntimes{c}{3} ->  ccc
> \setcounter{n}{0}%
> \ifnum\value{n}<#2\repeat}
>
> \newcommand{\cols}{\repeatntimes{c}{3}}
>
> \begin{tabular}[t]{\cols}
>    blah&  blah&  blah \\
>    blah&  blah&  blah \\
> \end{tabular}
> \end{document}
>
> (note - \repeatntimes works in all classes, just not in the preamble
> of the tabular env)
>
> My instinct is pointing me in the direction of an expansion problem,
> i.e. what's getting passed is \begin{tabular}{\cols} not
> \begin{tabular}{ccc}.
>
> Anyway, any help would be greatly appreciated  :-) .

I told you before that array.sty already implements this function:

\begin{tabular}[t]{*3{c}}

\Therefore \repeat \n \times \would \be \implemented \as :
\newcolumntype\RepeatC[1]{*{#1}{c}}

The process of \@mkpream is interesting and I invite you to look
at the log:

\usepackage{etex}
\loggingall
\begin{tabular}{*3{c}}
\begin{tabular}{\repeatntimes{c}{3}}

to see what happens.
It doesn't seem to be clear in your mind :-)

> Bev


 0

On Thu, 18 Nov 2010 09:49:01 -0800 (PST), bev <fbe2@eyremail.net>
wrote:

>I already have a solution to this in the memoir class (thanks GL and
>Lars!), but not for LaTeX's base classes.
>Rather than explain, here's an MWE which should be clear:
>
>==== This works =====
>\documentclass{article}
>\begin{document}
>
>\newcommand{\cols}{ccc}
>
>\begin{tabular}[t]{\cols}
>   blah & blah & blah \\
>   blah & blah & blah \\
>\end{tabular}
>\end{document}
>
>===== This fails =====
>\documentclass{article}
>\begin{document}
>\newcounter{n}
>
>\newcommand{\repeatntimes}[2]{ % usage \repeatntimes{c}{3} -> ccc
>\setcounter{n}{0}%
>\ifnum\value{n}<#2\repeat}
>
>\newcommand{\cols}{\repeatntimes{c}{3}}

This code does not work by expansion alone, so it will not
work for almost any use other than simply printing the
second argument. Another approach is to define \repeatntimes
so that it adds the first argument to a macro multiple times:
\makeatletter
\newcommand{\repeatntimes}[2]{%
\def\result{}%
\setcounter{n}{0}%
\ifnum\value{n}<#2\repeat}
\makeatother
\repeatntimes{c}{3}
\let\cols=\result

Where \g@addto@macro is a useful latex internal command.
It redefines the first argument (\result} to be its
previous definition with the second argument added
at the end. So \result starts out empty and then each

I used \result as a temporary macro to hold the result,
then immediately transfered it to the macro that will be
used later. This makes \result available again for another
invokation. One could also define \repeatntimes to take a
macro argument and to store the result directly in it.

There are other ways to do this, but this is conceptually
simplest, I think.

>
>\begin{tabular}[t]{\cols}
>  blah & blah & blah \\
>  blah & blah & blah \\
>\end{tabular}

Note that using a macro to hold the preamble is dicey
at best. I've tested that it works with r, l, c, |, and
@, but fails when a * is included in the macro. And it
fails in all cases if the array package is loaded.

Good luck,
Dan
To reply by email, change LookInSig to luecking

 0

On Nov 18, 9:44=A0pm, Dan Luecking <LookIn...@uark.edu> wrote:

Hi Dan,

I find myself reinventing such macros over and over again. Of course,
I use more meaningful lisp-like names such as snoc. Are these useful
LaTeX internal commands explained/described somewhere, or does one
have to read source code for this?

Regards,

Marc van Dongen

 0

Le 18/11/2010 22:44, Dan Luecking a �crit :
> On Thu, 18 Nov 2010 09:49:01 -0800 (PST), bev<fbe2@eyremail.net>
> wrote:
>
> Note that using a macro to hold the preamble is dicey
> at best. I've tested that it works with r, l, c, |, and
> @, but fails when a * is included in the macro. And it
> fails in all cases if the array package is loaded.

The array package says in its doc, and I totally agree with them,
that the preamble token given by the user must not be expanded and
must be given as-is in the \halign preamble.

The OP is a bit cloth-ears for I explained him twice before that
what he wanted was the * token provided by array.sty.

>
> Good luck,

Yes ;-)

> Dan
> To reply by email, change LookInSig to luecking


 0

On Mon, 22 Nov 2010 15:07:12 +0100, GL <gouailles@gmail.com> wrote:

>Le 18/11/2010 22:44, Dan Luecking a �crit :
>
>> Note that using a macro to hold the preamble is dicey
>> at best. I've tested that it works with r, l, c, |, and
>> @, but fails when a * is included in the macro. And it
>> fails in all cases if the array package is loaded.
>
>The array package says in its doc, and I totally agree with them,
>that the preamble token given by the user must not be expanded and
>must be given as-is in the \halign preamble.
>
>The OP is a bit cloth-ears for I explained him twice before that
>what he wanted was the * token provided by array.sty.

You don't need the array package to use the * token. It is
provided in base latex.

Without array, a macro can be used in a tabular preamble, but
this is probably an accident of the implementation because
* inside the macro causes it to fail. I wouldn't advise anyone
to use them for anything.

Dan
To reply by email, change LookInSig to luecking

 0