4

Consider a simple table in the document:

\begin{tabular}{ l c r }
  1 & 2 & 3 \\
  4 & 5 & 6 \\
  7 & 8 & 9 \\
\end{tabular}

How we can define this table within the documentclass as users only put the values of each cell. I mean how to set variables in the documentclass, as we have, for example, for sections:

\section*{Section Header}
Section body

How to define the table as users can fill the variable in the documents as something like:

\table{header1,header2,header3}
row1: cell1, cell2, cell3
...
David Carlisle
  • 757,742
Googlebot
  • 4,035
  • Are all the tables supposed to be like that? And where's the advantage in inputting the table in that way? – egreg Apr 17 '12 at 07:58
  • 1
    Yes, consider that all tables have the same number of columns. The advantage is designing the table with additional packages like tikz the end user does not mess with a long list of codes; just put what he has. I think this is the main purpose of documentsclasses in general (to keep users from mess of codes). – Googlebot Apr 17 '12 at 08:03
  • 1
    @Ali To some extent, but the tablular environment is part of the standard LaTeX kernel. As such, any LaTeX user should be okay with tablular. All you will do with your idea is break things! – Joseph Wright Apr 17 '12 at 08:07
  • @JosephWright But sometimes the tabular is mixed with less known packages for better design. In any case, there is no downside (if there's no advantage) for pulling the table codes into documentsclass to keep the main document tidy. – Googlebot Apr 17 '12 at 08:10

2 Answers2

3

There are several ways to achieve what you want using \newcommand and \newenvironment However, we must remember that both of these commands can accept at most 9 arguments.

So, borrowing the idea from Yiannis in

Definitive guide to trivlists

we have the following code

\documentclass{article}

\newenvironment{mytabular}
  {\trivlist\item
   \tabular{lcr}}
  {\endtabular\endtrivlist}

\newcommand{\row}[3]{#1&#2&#3\\}

\begin{document}

\begin{mytabular}
   \row{1}{2}{3}
   \row{4}{5}{6}
   \row{7}{8}{9}
\end{mytabular}

\end{document}

You might also like to look at

How to separate table content and table style

cmhughes
  • 100,947
3

Define this environment

\RequirePackage{booktabs}
\newenvironment{commontabular}[3]
  {\begin{tabular}{lcr}
   \toprule
   \bfseries #1 & \bfseries #2 & \bfseries #3 \\
   \midrule}
  {\bottomrule
   \end{tabular}}

and then tell users to input their tables as

\begin{commontabular}{header1}{header2}{header3}
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9 \\
\end{commontabular}

There's no point in using a non standard syntax for tables, as this hinders code reusability.

But I see no point in providing such shortcuts either.

David Carlisle
  • 757,742
egreg
  • 1,121,712