Document Classescreating a variable preamble for tabular env

Information and discussion about specific document classes and how to create your own document classes.
Post Reply
User avatar
bev
Posts: 20
Joined: Wed Nov 10, 2010 10:02 am

creating a variable preamble for tabular env

Post by bev »

The reason I am including this in the `Document Class' forum is because 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

Code: Select all

Code, edit and compile here:
\documentclass{article}
\begin{document}
\newcommand{\cols}{ccc}
\begin{tabular}[t]{\cols}
blah & blah & blah \\
blah & blah & blah \\
\end{tabular}
\end{document}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
------> this fails

Code: Select all

Code, edit and compile here:
\documentclass{article}
\begin{document}
\newcounter{n}
\newcommand{\repeatntimes}[2]{ % usage \repeatntimes{c}{3} -> ccc
\setcounter{n}{0}%
\loop\addtocounter{n}{1}{#1}%
\ifnum\value{n}<#2\repeat}
\newcommand{\cols}{\repeatntimes{c}{3}}
\begin{tabular}[t]{\cols}
blah & blah & blah \\
blah & blah & blah \\
\end{tabular}
\end{document}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
The macro \repeatntimes works in any class. But 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

Recommended reading 2024:

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

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

User avatar
frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

creating a variable preamble for tabular env

Post by frabjous »

I think the tabular environment's table specification argument is just really fussy in what it allows. Notice that you'll get an error if you throw any stray commands in there, even if those commands have no "output", so to speak. If you write, e.g.:

Code: Select all

\begin{tabular}[t]{\setcounter{n}{4}ccc}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Even though setting that counter there isn't affecting anything, you'd still get the "Illegal character in array arg." error. And your macro is expanding to something that has commands like that in it.

But you might not know that there's a built in method for specifying multiple columns at once. (See, e.g., here.) The syntax is *{n}{type} for n columns of the type. So, e.g.:

Code: Select all

\begin{tabular}{l*{6}{c}r}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Would give you an 8 column table with one left-aligned column, six centered ones, and a right-aligned one at the end.

And you can use counters, etc., with it, e.g.:

Code: Select all

Code, edit and compile here:
\documentclass{article}
\begin{document}
\newcounter{n}
\setcounter{n}{5}
\begin{tabular}[t]{*{\value{n}}{c}}
blah & blah & blah & blah & blah \\
blah & blah & blah & blah & blah \\
\end{tabular}
\end{document}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
User avatar
bev
Posts: 20
Joined: Wed Nov 10, 2010 10:02 am

Re: creating a variable preamble for tabular env

Post by bev »

Thanks for the time to answer this. I'll check it out when I have a moment (at work now).

I actually did know about the *{n}{type} construction, but didn't know you could use it with counters as you describe. I think that your solution will eminently solve my problem.

I appreciate your time on this. It was holding up progress on a package I'm working on for our lab: a easy, simple, and fast tabular env for figures (figarray.sty).

The other packages I've tried for this type of functionality are too complex and cause too many problems and conflicts with other packages. When I finish it I'm going to post it somewhere for comments and feedback from people who will have tried it (hopefully).

ta

- Bev
Post Reply