# Making a table so that its title does not widen the columns

I have a table with two columns. Since the entries are single-digit integers, I would like the columns to be only wide enough to contain the entries. With my code, the title makes the columns too wide.

Also, I would like to have the title typeset on two lines. How do I put "Certain Function" on the first line and "Values of f" on the second line?

\documentclass{amsart}
\usepackage{mathtools}

\usepackage{makecell}
\usepackage{array}

\begin{document}

\noindent \hspace*{\fill}
\setlength\extrarowheight{2pt}
\begin{tabular}{|| c | c ||} \hline
\multicolumn{2}{|| c ||}{{\bf Certain Function Values of \boldmath$f$\unboldmath}} \\ \Xhline{0.8pt}
$x$ &   $f(x)$ \\ \Xhline{0.8pt}
2   &   -1 \\ \hline
3   &   0 \\ \hline
4   &   2 \\ \hline
5   &   -3 \\ \hline
6   &   4 \\ \hline
7   &   -5 \\ \hline
8   &   6 \\ \hline
\end{tabular}

\end{document}


It is tough to meet the constraints of the OP while leaving the top row as part of the tabular, so I here instead stack the heading above the tabular, using my stackengine package (http://ctan.org/pkg/stackengine).

The \stackon{A}{B} syntax places "A" in its normal location relative to the baseline, while "B" is placed above "A". By default the gap between the top of "A" and bottom of "B" is 3pt, though an optional argument can alter that length. Also, the default alignment is centered, but that, too, can be altered with the setting of a mode parameter. The package provides other commands, such as \stackunder{A}{B} for placing "B" under the normally located "A". I also use a \Longstack{} in which a series of objects separated by an end-of-line (EOL) separator are stacked with a constant baselineskip separation between each row.

I also changed your dashes to minus signs.

\documentclass{amsart}
\usepackage{mathtools}

\usepackage{makecell}
\usepackage{array}
\usepackage{stackengine}\setstackEOL{\cr}
\begin{document}

\noindent \hspace*{\fill}
\setlength\extrarowheight{2pt}
\stackon{%
\begin{tabular}{|| c | c ||} \hline
$x$ &   $f(x)$ \\ \Xhline{0.8pt}
2   &   $-$1 \\ \hline
3   &   0 \\ \hline
4   &   2 \\ \hline
5   &   $-$3 \\ \hline
6   &   4 \\ \hline
7   &   $-$5 \\ \hline
8   &   6 \\ \hline
\end{tabular}%
}{\bfseries\Longstack{Certain Function\cr Values of \boldmath$f$}}
\end{document}


• Please explain the syntax. You have \stackon{ followed by the table, and the table is followed by } and {\bfseries\Longstack{Certain Function\cr Values of $f$}}. May 13 '15 at 17:25
• I have not seen \stackon and \Longstack and \cr. (I guess \cr is an abbreviation for "carriage return.") May 13 '15 at 17:27
• @user74973 I have provided some explanation. Yes, \cr is a LaTeX carriage return character, which I chose as the end-of-line indicator with my earlier \setstackEOL{\cr}. I could have said \setstackEOL{\#} and used \# instead of \cr for example. The default EOL separator is a space. May 13 '15 at 17:33
• @user74973 In general, one can use \setstackEOL{\\} to allow the syntax of {text\\2nd line}. That will work in my example. However, since tabular itself uses \\ as the end-of-line, I try to avoid getting things confused using the same EOL when a tabular is part of a stack. But in this particular case, you can do that. May 13 '15 at 17:44
• @user74973 The default EOL is a space, so if one did \Longstack{Certain Function Values of f}, each word would be on a different line. To leave the default EOL, one could say \Longstack{{Certain Function} {Values of f}}, using nested braces. May 13 '15 at 17:57

This is a clear example of how to not build tables, sorry. The double vertical lines at the side serve no purpose and neither does the thick rule below the header.

If you really want the title all on one line, there's very little to do; a possibility would be to use nested tables:

\documentclass{amsart}
\usepackage{siunitx,booktabs}

\begin{document}

\begin{tabular}{c}
\toprule
\bfseries\boldmath Certain Function Values of $f$ \\
\midrule
\begin{tabular}{S[table-format=1.0] S[table-format=-1.0]}
{$x$} & {$f(x)$} \\
\midrule
2   &   -1 \\
3   &    0 \\
4   &    2 \\
5   &   -3 \\
6   &    4 \\
7   &   -5 \\
8   &    6
\end{tabular}\\
\bottomrule
\end{tabular}
\end{document}


Another possibility is to split the header into two lines, adding two mock columns at the sides:

\documentclass{amsart}
\usepackage{siunitx,booktabs}

\begin{document}

\begin{tabular}{cS[table-format=1.0] S[table-format=-1.0]c}
\toprule
\multicolumn{4}{c}{\bfseries Certain Function} \\
\multicolumn{4}{c}{\bfseries\boldmath Values of $f$} \\
\midrule
\hspace*{2em} & {$x$} & {$f(x)$} & \hspace*{2em} \\
\midrule
&  2   &   -1 \\
&  3   &    0 \\
&  4   &    2 \\
&  5   &   -3 \\
&  6   &    4 \\
&  7   &   -5 \\
&  8   &    6 \\
\bottomrule
\end{tabular}
\end{document}


Or simply using a floating table:

\documentclass{amsart}
\usepackage{siunitx,booktabs}

\begin{document}

\begin{table}
\centering
\caption{Certain Function Values of $f$}

\begin{tabular}{S[table-format=1.0] S[table-format=-1.0]}
\toprule
{$x$} & {$f(x)$} \\
\midrule
2   &   -1 \\
3   &    0 \\
4   &    2 \\
5   &   -3 \\
6   &    4 \\
7   &   -5 \\
8   &    6 \\
\bottomrule
\end{tabular}
\end{table}

\end{document}


• Many of the textbooks that I use have rows and columns in tables separated by vertical bars. I am just mimicking the tables in these books. May 13 '15 at 18:12
• The tables you display are nice. I am going to try to code for tables that do not require packages to be loaded in the preamble. May 13 '15 at 18:13
• @user74973 That's the wrong approach: packages are useful. May 13 '15 at 19:36