# How can I reuse the same table alignment specifications in many tables?

\documentclass{article}
\usepackage{array}

\begin{document}

\begin{table}
\begin{tabular}{|>{$}c<{$}|>{$}c<{$}|}
1 & 2 \\
\end{tabular}
\end{table}

\begin{table}
\begin{tabular}{|>{$}c<{$}|>{$}c<{$}|}
3 & 4 \\
\end{tabular}
\end{table}

\end{document}


I am new to LaTeX. I have a LaTeX file containing many large tables, each containing large number of columns. All the tables have the same kind of columns, and need similar alignment and column widths. I am looking for an easy way to specify a common alignment instruction for all of them (|>{$}c<{$}|>{$}c<{$}| in the above example). Is there a way to specify and reuse the string |>{$}c<{$}|>{$}c<{$}| (using \newcommand or \let or \def etc), so that I can easily replicate the behavior in all my tables?

• \newcommand{\mytabs}{|>{$}c<{$}|>{$}c<{$}|} will reproduce the results, but without your \usepackege I get a lot of errors either way. – John Kormylo Feb 17 '14 at 15:12
• Try \newenvironment – Yossi Gil Feb 17 '14 at 16:16

The easiest way is to define a new column type and, for a large number of columns, exploit

*{<number>}{<col specs>}


Example

\documentclass{article}
\usepackage{array}
\newcolumntype{C}{>{$}c<{$}}

\begin{document}

\begin{table}
\begin{tabular}{|*{2}{C|}}
1 & 2 \\
\end{tabular}
\end{table}

\begin{table}
\begin{tabular}{|*{4}{C|}}
3 & 4 & 5 & 6\\
\end{tabular}
\end{table}

\end{document}


However, I don't recommend “ruled tables”: a table is not a worksheet screen. Look at the documentation of the booktabs package for some examples.

A column type, defined by \newcolumntype of package array can cover more than one column. Furthermore it can take arguments, e.g.:

\documentclass{article}
\usepackage{array}

\newcolumntype{A}[1]{|*{#1}{>{$}c<{$}|}}

\begin{document}

\begin{tabular}{A{2}}
1 & 2 \\
\end{tabular}

\bigskip

\begin{tabular}{A{4}}
1 & 2 & 3 & 4 \\
\end{tabular}

\end{document}


Since all of your tables would appear to contain math-mode material, you should consider switching from the tabular to the array environment. LaTeX array environments are supposed to be used in math-mode; their l, c, and r alignment specifiers work exactly as their counterparts in tabular environments do, except that because they're already in math mode, it's not necessary to resort to constructs such as >{$}c<{$}. The \hline and \cline commands work as expected in array environments.

\documentclass{article}
\begin{document}
\begin{table}
\caption{A first table}
$\begin{array}{|c|c|} \hline 1 & 2 \\ \hline \end{array}$
\end{table}

\begin{table}[h]
\caption{A second table}
$\begin{array}{|c|c|} \hline -3 & -4 \\ \hline \end{array}$
\end{table}
\end{document}


There's one other, fairly subtle difference between tabular and array environments: The default amount of intercolumn white space. These amounts are governed by the parameters \arraycolsep and \tabcolsep, whose default values are 5pt and 6pt, respectively. If you have lots of columns in a table, you might start to note the difference.

Here is an example of how one can make the fixed part of your tables into an environment. There's one optional argument, the number of columns – 4 by default:

        \documentclass[12pt]{article}

\usepackage[utf8]{inputenc}
\usepackage{array}

\pagestyle{empty}
\raggedbottom

\newenvironment{mytable}[1][4]{%
\begin{table}[!h]
\centering\renewcommand{\arraystretch}{1.25}
\begin{tabular}{|*{#1}{>{$}c<{$}|}}
\hline}%
{\\\hline\end{tabular}
\end{table}}%

\begin{document}

With the default number of columns:
\begin{mytable}
1 & 2 & 3 & 4 \\
5 & 6 & 7 & 8
\end{mytable}

and with 10:
\begin{mytable}[10]
1 & 2 & 3 & 4  & 5 & 6 & 7 & 8  & 9  & 10\\
2 & 3 & 5 & 7 & 11 & 13 & 17 & 19 & 23 & 29
\end{mytable}

\end{document}