Graphics, Figures & Tablesarray | Table Preamble defined by Command causes Error

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
GordonW
Posts: 5
Joined: Sun Aug 12, 2012 7:14 pm

array | Table Preamble defined by Command causes Error

Post by GordonW »

Hi,

I'd appreciate any help with the following. I have a sample code that look like this:

Code: Select all

\documentclass{article}
%\usepackage{array}

\newcommand{\Format}{r}

\begin{document}
  \begin{tabular}{\Format}
    1 \\
    2 \\
  \end{tabular}
\end{document}
This works as I expect if I don't include any packages. However, if I uncomment the array package I get this sort of error message:

Code: Select all

! Package array Error:  Illegal pream-token (\Format): `c' used.

See the array package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.8 \begin{tabular}{\Format}
How can I get the package to behave in this respect in the same way as the default implementation of {tabular}?

Thanks,

GordonW
Last edited by localghost on Sun Sep 16, 2012 10:41 am, edited 2 times in total.

Recommended reading 2024:

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

NEW: TikZ book now 40% off at Amazon.com for a short time.

Stefan Kottwitz
Site Admin
Posts: 10345
Joined: Mon Mar 10, 2008 9:44 pm

array | Table Preamble defined by Command causes Error

Post by Stefan Kottwitz »

Hi Gordon,

welcome to the board!

It seems, that the array package doesn't expand the argument before parsing. You could force it. Your example works with array, when I change it this way:

Code: Select all

\expandafter\tabular\Format
  1111 \\
  2 \\
\endtabular
Stefan
LaTeX.org admin
GordonW
Posts: 5
Joined: Sun Aug 12, 2012 7:14 pm

array | Table Preamble defined by Command causes Error

Post by GordonW »

Hi Stefan,

Thanks for your response.

I'm having trouble generalizing your solution to tables with more than one column. Here is an example with two columns. The closest I've managed is this:

Code: Select all

\documentclass{article}
\newcommand{\FormatOne}{r}
\newcommand{\FormatTwo}{l}

\begin{document}
\expandafter\tabular\expandafter{\FormatOne l}
100 & 200 \\
30  &  40 \\
\endtabular
\end{document}
Note that I haven't been able to give the format for the second column by \FormatTwo, and I've had to give it explicitly.

How do I generalize the use of \expandafter to reach over an arbitrary number of columns, say 30 columns?

If it's relevant, I should note that I'll be aiming to use the colortab package once I jump this hurdle.

Thanks,

GordonW
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

array | Table Preamble defined by Command causes Error

Post by cgnieder »

Here is an idea (or rather a shameless copy of expl3's \exp_args:Nnx):

Code: Select all

\documentclass{article}

\makeatletter
\long\def\long@firstofone#1{#1}
\long\def\@getnextbraced#1#2#3{#2\long@firstofone{#3{#1}}}
\long\def\@braced@unexpanded#1\long@firstofone#2#3{%
  #1\long@firstofone{#2{#3}}}
\protected\long\def\@braced@expandedfully#1\long@firstofone#2#3{%
  \edef\@expandargs@internal{{#3}}%
  \expandafter\@getnextbraced\@expandargs@internal{#1}{#2}%
}
\protected\long\def\expandtabularargs{%
  \@braced@unexpanded
  \@braced@expandedfully
  \long@firstofone
}
\makeatother

\newcommand*\FormatOne{r}
\newcommand*\FormatTwo{c}
\newcommand*\FormatThree{l}
\newcommand*\FormatFour{l}
\newcommand*\FormatFive{r}
\newcommand*\FormatSix{c}

\begin{document}

\expandtabularargs
\begin{tabular}{\FormatOne\FormatTwo\FormatThree\FormatFour\FormatFive\FormatSix}
 100 & 200 & 100 & 200 & 100 & 200 \\
 30  &  40 & 30  &  40 & 30  &  40 \\
\end{tabular}

\end{document}
This fully (!) expands the argument of the tabular environment but requires to stick with the syntax. If your actual use-case has an optional argument, too, we'll have to change the code.

Regards
site moderator & package author
GordonW
Posts: 5
Joined: Sun Aug 12, 2012 7:14 pm

array | Table Preamble defined by Command causes Error

Post by GordonW »

Hi Clemens,

Thanks for your solution. It works that much better. However, how do I control how much expansion is done? Here's a case where I think it's expanding too much:

Code: Select all

\documentclass{article}

\usepackage{array}
\usepackage{colortbl}

\definecolor{gray90}{gray}{0.9}

\newcommand{\FormatOne}{>{\columncolor{gray90}}r}
\newcommand{\FormatTwo}{l}
\newcommand{\Columns}{\FormatOne\FormatTwo}

% Clemens' solution
\makeatletter
\long\def\long@firstofone#1{#1}
\long\def\@getnextbraced#1#2#3{#2\long@firstofone{#3{#1}}}
\long\def\@braced@unexpanded#1\long@firstofone#2#3{%
  #1\long@firstofone{#2{#3}}}
\protected\long\def\@braced@expandedfully#1\long@firstofone#2#3{%
  \edef\@expandargs@internal{{#3}}%
  \expandafter\@getnextbraced\@expandargs@internal{#1}{#2}%
}
\protected\long\def\expandtabularargs{%
  \@braced@unexpanded
  \@braced@expandedfully
  \long@firstofone
}
\makeatother

\begin{document}

% Try with the next two commented lines in place of the one following
%\expandtabularargs
%\begin{tabular}{\FormatOne\FormatTwo}
\begin{tabular}{>{\columncolor{gray90}}rr}
100 & 200 \\
100 & 200 \\
100 & 200 \\
100 & 200 \\
100 & 200 \\
\end{tabular}

\end{document}
This code is processed as expected. However, try this:

Code: Select all

\expandtabularargs
\begin{tabular}{>{\columncolor{gray90}}rr}
or

Code: Select all

\expandtabularargs
\begin{tabular}{\FormatOne\FormatTwo}
Thanks,

GordonW
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

array | Table Preamble defined by Command causes Error

Post by cgnieder »

You can combine it with etoolbox's \expandonce:

Code: Select all

\documentclass{article}
\usepackage{xcolor,colortbl,etoolbox}

\makeatletter
\long\def\long@firstofone#1{#1}
\long\def\@getnextbraced#1#2#3{#2\long@firstofone{#3{#1}}}
\long\def\@braced@unexpanded#1\long@firstofone#2#3{%
  #1\long@firstofone{#2{#3}}}
\protected\long\def\@braced@expandedfully#1\long@firstofone#2#3{%
  \edef\@expandargs@internal{{#3}}%
  \expandafter\@getnextbraced\@expandargs@internal{#1}{#2}%
}
\protected\long\def\expandtabularargs{%
  \@braced@unexpanded
  \@braced@expandedfully
  \long@firstofone
}
\makeatother

\def\FormatOne{>{\columncolor{gray!90}}r}
\def\FormatTwo{>{\columncolor{yellow!90}}r}

\begin{document}

\expandtabularargs
\begin{tabular}{\expandonce\FormatOne\expandonce\FormatTwo}
100 & 200 \\
100 & 200 \\
100 & 200 \\
100 & 200 \\
100 & 200 \\
\end{tabular}

\end{document}
Regards
site moderator & package author
GordonW
Posts: 5
Joined: Sun Aug 12, 2012 7:14 pm

Re: array | Table Preamble defined by Command causes Error

Post by GordonW »

Clemens,

Thanks!

GordonW
Post Reply