Error when using tabular environment
Use a length to store the value given by \widthof
; here's your code with some modifications:
\documentclass[11pt]{article}
\usepackage{amsmath, amssymb}
\usepackage{calc}
\usepackage{array}
\usepackage{booktabs}
\newcommand\Q{\mathbb{Q}}
\begin{document}
\newlength\mylen
\setlength\mylen{\widthof{$\Q^2$}}
\begin{table}
\centering
\begin{tabular}{%
>{$}l<{$}@{\hspace{28pt}}%
>{$}l<{$}@{\hspace{28pt}}%
>{$}p{\mylen}<{$}@{\hspace{5pt}}%
>{$}p{\mylen}<{$}@{\hspace{5pt}}%
>{$}p{\mylen}<{$}@{\hspace{5pt}}%
>{$}p{\mylen}<{$}@{\hspace{5pt}}%
>{$}p{\mylen}<{$}@{}%
}
2 & 1 & 0 & & & & \\
3 & 3 & 0 & 0 & 0 & & \\
4 & 6 & 0 & 0 & \Q & 0 & 0 \\
5 & 10 & 0 & 0 & 0 & 0 & \Q \\
6 & 15 & 0 & 0 & 0 & 0 & \Q^2 \\
7 & 21 & 0 & 0 & 0 & 0 & \Q \\
\end{tabular}
\end{table}
\end{document}
I used
>{$}...<{$}
to indicate math mode in the column format, so you don't have to explicitly use$...$
in every cell.Instead of using the
center
environment I used\centering
to prevent adding undesired vertical space.
While a syntax such as p{3cm-2\tabcolsep}
is allowed by the calc
package, \widthof
cannot be used there as it needs setting a box, an activity that LaTeX doesn't perform when building the alignment preamble: it requires typesetting, but at that moment LaTeX is only accumulating tokens for transforming the tabular
specifications into what's understood by the lower level TeX instructions.
The right way is to precompute the width and store it in a register:
\newcommand{\Q}{\mathbb{Q}}
\newlength\qlen
(You gain nothing with \ensuremath
, really.)
Then you can set the length in the vicinity of the table you want to build:
\begin{table}
\centering
\settowidth{\qlen}{$\Q^{2}$}
$\begin{array}{
*{2}{l@{\hspace{28pt}}}
*{4}{p{\qlen}@{\hspace{5pt}}
p{\qlen}
}
2 & 1 & 0 & & & & \\
3 & 3 & 0 & 0 & 0 & & \\
4 & 6 & 0 & 0 & \Q & 0 & 0 \\
5 & 10 & 0 & 0 & 0 & 0 & \Q \\
6 & 15 & 0 & 0 & 0 & 0 & \Q^2 \\
7 & 21 & 0 & 0 & 0 & 0 & \Q \\
\end{array}$
\end{table}
Using array
rather than tabular
frees you from all those $
symbols.
Why setting \qlen
there? Simple: you might decide to typeset the table in a different type size, so a fixed \qlen
is not what you need.
In addition to defining a length variable, say \Qlen
, that'll store the value of the column width, your code can be simplified further by (i) setting the default amount of intercolumn whitespace to 5pt
(by setting \tabcolsep
to 2.5pt), (ii) grouping the various column definitions, (iii) deleting most of the unneeded math mode specifiers in the table, and (iv) replacing the pair of \begin{center}
... \end{center}
instructions with a single \centering
macro.
\documentclass[11pt]{article}
\usepackage{amsmath, amssymb, calc, array, booktabs}
\newcommand\Q{\ensuremath{\mathbb{Q}}}
\newlength\Qlen
\settowidth\Qlen{\ensuremath{\Q^2}}
\begin{document}
\begin{table}
\setlength\tabcolsep{2.5pt} % set default intercolumn whitespace width to 5pt
\centering
\begin{tabular}{*{2}{l@{\hspace{28pt}}} % override default width
*{5}{p{\Qlen}} @{}}
2 & 1 & 0 \\
3 & 3 & 0 & 0 & 0 \\
4 & 6 & 0 & 0 & \Q & 0 & 0 \\
5 & 10 & 0 & 0 & 0 & 0 & \Q \\
6 & 15 & 0 & 0 & 0 & 0 & $\Q^2$ \\
7 & 21 & 0 & 0 & 0 & 0 & \Q \\
\end{tabular}
\end{table}
\end{document}