# Error when using tabular environment

I'm trying to create a simple table, but it just won't compile. Here's a sample bit of code:

\documentclass[11pt]{article}
\usepackage{amsmath, amssymb}
\usepackage{calc}
\usepackage{array}
\usepackage{booktabs}

\newcommand\Q{\ensuremath{\mathbb{Q}}}

\begin{document}

\begin{table}
\begin{center}
\begin{tabular}{l@{\hspace{28pt}}l@{\hspace{28pt}}p{\widthof{$\Q^2$}}@{\hspace{5pt}}p{\widthof{$\Q^2$}}@{\hspace{5pt}}p{\widthof{$\Q^2$}}@{\hspace{5pt}}p{\widthof{$\Q^2$}}@{\hspace{5pt}}p{\widthof{$\Q^2$}}@{}}
$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{center}
\end{table}

\end{document}


When I try to compile this, I get a sequence of errors that look like the following:

! Missing control sequence inserted.
<inserted text>
\inaccessible
l.14 $2$ & $1$  & $0$ &     &      &     &        \\


What am I doing wrong?

-

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}


1. I used >{$}...<{$} to indicate math mode in the column format, so you don't have to explicitly use $...$ in every cell.

2. Instead of using the center environment I used \centering to prevent adding undesired vertical space.

-
Great, this fixes it! What exactly was going wrong? –  Thomas Apr 19 '12 at 3:17
@Thomas to be honest, I don't exactly understand why the \inaccessible message appeared here; this message usually indicates that a backslash was somewhere forgotten when defining a macro, but I don't see why it occurred here and,as far as I could track it, \mathbb was the culprit. I'll let you know if I can detect what the cause of the problem was. –  Gonzalo Medina Apr 19 '12 at 3:34
Thanks for your help! –  Thomas Apr 19 '12 at 4:02

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}


-
But the intercolumn space is not always 5pt. Also, the change to \tabcolsep should be made inside a group to keep it local. –  Gonzalo Medina Apr 19 '12 at 3:38
@GonzaloMedina -- second point taken, will include it in the MWE. The first point, though, is not a problem: the MWE automatically takes care of setting the intercolumn spacing to non-default values. –  Mico Apr 19 '12 at 3:40