1

I have a table in which the header is making the last column too wide. How do I avoid this situation?

\documentclass{amsart}
\usepackage{amsmath}
\usepackage{amsfonts}


\usepackage{array}
\usepackage{makecell}


\setlength{\oddsidemargin}{0.0in}
\setlength{\evensidemargin}{0.0in} \setlength{\textwidth}{6.1in}
\setlength{\topmargin}{0.0in} \setlength{\textheight}{9in}


\begin{document}
\setlength\extrarowheight{2pt}
\begin{tabular}{|| c !{\vrule width0.8pt}c | c | c | c | c | c | c | c | c ||} \hline
\multicolumn{10}{|| c ||}{{\textbf{Force in newtons required to move an object \boldmath$x$ meters\unboldmath}}} \\ \Xhline{0.8pt}
    $x$     &   4   &   6   &   8   &   10  &   12  &   14  &   16  &   18  &   20  \\ \hline
    $f(x)$  &   5   &   5.8 &   7   &   8.8 &   9.6 &   8.2 &   6.7 &   5.2 &   4.1 \\ \hline
\end{tabular}

\end{document}
Adelyn
  • 3,373

3 Answers3

4

I suggest you use a tabularx environment instead of the basic tabular environment. Moreover, I think the nine data columns should have equal widths. Use a \settowidth instruction to calculate the required width of the tabularx. You may also want to get rid of all vertical lines, to give the table a more "open" look.

enter image description here

\documentclass{amsart}
\usepackage{bm,booktabs,tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}

\newcommand\myheader{\bfseries Force in Newton required to move an object $\bm x$ meters}

\newlength\mylength
\settowidth{\mylength}{\myheader}

\begin{document}

\noindent
\begin{tabularx}{\mylength}{@{} l *{9}{C} @{}} 
\toprule
\multicolumn{10}{@{}c@{}}{\myheader} \\ % re-use the \myheader macro
\midrule
$x$    & 4 & 6   & 8 & 10  &  12 & 14  & 16  & 18  & 20  \\ 
$F(x)$ & 5 & 5.8 & 7 & 8.8 & 9.6 & 8.2 & 6.7 & 5.2 & 4.1 \\
\bottomrule
\end{tabularx}

\end{document}

Addendum to address the OP's follow-up comment: If you do want the vertical bars, you can't use the line-drawing macros of the booktabs package. Instead, I suggest you insert (typographic) struts to provide a bit more vertical whitespace.

enter image description here

\documentclass{amsart}
\usepackage{bm,tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}

\newcommand\myheader{\bfseries Force in Newton required to move an object $\bm x$ meters}

\newlength\mylength
\settowidth{\mylength}{\myheader}
\addtolength{\mylength}{2\tabcolsep}
\addtolength{\mylength}{2\arrayrulewidth}

%% define a few struts
%% (from code by Claudio Beccari in TeX and TUG News, Vol. 2, 1993)
\newcommand\Tstrut{\rule{0pt}{2.9ex}}         % "top" strut
\newcommand\Bstrut{\rule[-1.2ex]{0pt}{0pt}}   % "bottom" strut
\newcommand\TBstrut{\Tstrut\Bstrut}           % "top-and-bottom" strut

\begin{document}

\noindent
\begin{tabularx}{\mylength}{ |l| *{9}{C|} } 
\hline
\multicolumn{10}{|c|}{\myheader\TBstrut} \\ % re-use the \myheader macro
\hline
$x$\TBstrut    & 4 & 6   & 8 & 10  &  12 & 14  & 16  & 18  & 20  \\
\hline 
$F(x)$\TBstrut & 5 & 5.8 & 7 & 8.8 & 9.6 & 8.2 & 6.7 & 5.2 & 4.1 \\
\hline
\end{tabularx}

\end{document}
Mico
  • 506,678
  • I understand that it is more common to have tables without the vertical bars separating the columns. I do want the vertical bars. I do agree with your comment that " the nine data columns should have equal widths." – Adelyn Feb 03 '16 at 19:07
  • @mico -- some other ways of adding more "air" to table rows: extrarowheight vs arraystretch – barbara beeton Feb 03 '16 at 19:35
  • @barbarabeeton - Thanks. The nice thing about the strut method is that it makes it easy to have equal amounts of whitespace above and below the numbers in the cells. – Mico Feb 03 '16 at 19:41
  • @Mico -- good point. when a table contains mostly numbers, that should be catered for. – barbara beeton Feb 03 '16 at 19:46
  • @Mico This is a very nice table - the one with the vertical bars. I could use some clarification on the code. (I am unfamiliar with some commands you have in the preamble. It is not that your code is "messy.") Do you define the command \myheader with \newcommand\myheader{\bfseries Force in Newton... $\bm x$ meters}? Could you have avoided this and just had the commands \newlength\mylength and \settowidth{\mylength}{\bfseries Force in Newton... $\bm x$ meters}? – Adelyn Feb 03 '16 at 22:38
  • @Mico Is $\bm x$ just a more efficient code for \boldmath$x$\unboldmath? – Adelyn Feb 03 '16 at 22:39
  • 1
    @Adelyn - The reason I set up the macro \myheader is so that the string \bfseries Force in Newton required to move an object $\bm x$ meters needn't be typed twice (first when the width of the string is measured, and later when it's typset). And, should you choose later on to use a different header string, you'll only need to modify it once. The macro \bm not only is more efficient, it also avoids the bad spacing that arises from ...\boldmath$x$\unboldmath.... – Mico Feb 03 '16 at 22:48
  • @Mico If I had both f(x) and x in the header, would you code something like \bfseries Force $bm f(x)$ in Newton required to move an object $\bm x$ meters? Is \newcolumntype{C}{>{\centering\arraybackslash}X} in the preamble used? – Adelyn Feb 03 '16 at 22:58
  • @Adelyn - Be sure to write \bm, not bm (note the backslash character that precedes bm). I believe that in physics, the universally used symbol for "force" is F, so I'd write F(x), not f(x). Finally, the instruction \newcolumntype{C}{>{\centering\arraybackslash}X} gets used in the final argument of the tabularx environment setup, viz., in the string |l| *{9}{C|}. – Mico Feb 03 '16 at 23:02
  • @Mico The code you provided gives me the table that I wanted. I think that I should read about the tabularx package in case I want to include other tables in a file. May you suggest a web site containing its manual? If it is not too inconvenient, please explain the syntax of \newcolumntype{C}{>{\centering\arraybackslash}X} and what it does to { |l| *{9}{C| }. – Adelyn Feb 04 '16 at 15:21
  • 1
    @Adelyn - The \newcolumntype instruction creates a column type named C, which centers its contents (because of \centering) but otherwise inherits the properties of the X column type, which is defined by the tabularx package. (See the manual of the array package for an explanation of what \arraybackslash does.) And, please read the manual of the tabularx package. The string *{9}{C|} is shorthand for "9 columns of type C, each one followed by | (a vertical bar)". – Mico Feb 04 '16 at 15:32
2

The last column is too wide because the ‘title’ is too wide w.r.t. the natural width of the rest of the table. I propose a solution with equal cell widths: it uses the eqparbox and tabularx packages and ensures the tabularx table has minimal width. Furthermore, I use hhlines to have correct intersections of horizontal rules and double vertical rules.

\documentclass{amsart}
\usepackage{amsmath}
\usepackage{amsfonts}

\usepackage{makecell}
\renewcommand\theadfont{\normalsize\bfseries\boldmath}
\usepackage{tabularx, eqparbox, hhline}

\setlength{\oddsidemargin}{0.0in}
\setlength{\evensidemargin}{0.0in} \setlength{\textwidth}{6.1in}
\setlength{\topmargin}{0.0in} \setlength{\textheight}{9in}


\begin{document}

\centering
\setcellgapes{4pt}\makegapedcells
\begin{tabularx}{\dimexpr\eqboxwidth{H}+2\tabcolsep+2\doublerulesep + 4\arrayrulewidth\relax}
  {||c !{\vrule width0.8pt}*{9}{>{\centering\arraybackslash}X|}|}
  \hline
  \multicolumn{10}{||c||}{\eqmakebox[H]{\bfseries\boldmath Force required to move an object $x$ meters (in newtons)}} \\
  \hhline{||*{10}{-}||}
  \hhline{||*{10}{-}||}
  $x$ & 4 & 6 & 8 & 10 & 12 & 14 & 16 & 18 & 20 \\
  \hhline{||*{10}{-}||}
  $f(x)$ & 5 & 5.8 & 7 & 8.8 & 9.6 & 8.2 & 6.7 & 5.2 & 4.1 \\
  \hline
\end{tabularx}

\end{document} 

enter image description here

Bernard
  • 271,350
1

It seems you are new with latex. Your primary purpose of labeling the table could be fulfilled by this simple code instead.

\documentclass{amsart}
\usepackage{makecell}
\begin{document}
\begin{table}
    \centering
    \caption{Force in newtons required to move an object \boldmath$x$ meters\unboldmath}
    \begin{tabular}{|| c !{\vrule width0.8pt}c | c | c | c | c | c | c | c | c ||} \hline
        $x$     &   4   &   6   &   8   &   10  &   12  &   14  &   16  &   18  &   20  \\ \hline
        $f(x)$  &   5   &   5.8 &   7   &   8.8 &   9.6 &   8.2 &   6.7 &   5.2 &   4.1 \\ \hline
    \end{tabular}
\end{table}
\end{document}

enter image description here

Edit: However, if you insist on enclosing the caption inside the table, this is yet another alternative derived from the source:An old question on tex

\documentclass{article}
\usepackage{bm,array}
\begin{document}
\newcolumntype{C}{>{\centering\arraybackslash}p{2em}}
\begin{table}
\centering
\begin{tabular}{|| c !{\vrule width0.8pt}C|C|C|C|C|C|C|C|C||}
\hline
\multicolumn{10}{|c|}{Force in newtons required to move an object \boldmath$x$ meters\unboldmath} \\
\hline
$x$     &   4   &   6   &   8   &   10  &   12  &   14  &   16  &   18  &   20  \\ \hline
$f(x)$  &   5   &   5.8 &   7   &   8.8 &   9.6 &   8.2 &   6.7 &   5.2 &   4.1 \\ \hline
\end{tabular}
\end{table}
\end{document}

Here is the output of this code ... enter image description here

Mist
  • 129
  • 1
    I do not code tables much. How can you modify your code to remove "Table 1" and to put the title within a rectangle - as I had in my code - on top of the table? – Adelyn Feb 03 '16 at 22:17
  • 1
    Hello @Adelyn I have edited my answer so you may see it again for both cases – Mist Feb 15 '16 at 15:57
  • Except for the first column, the width of the columns should be all the same. These columns all contain just data points. Can you edit your code to do this? – Adelyn Feb 16 '16 at 15:36
  • 1
    Answer has been further updated. Hope it helps. Cheers – Mist Feb 16 '16 at 16:49
  • This is the display of the table that I wanted. What did you edit in your code so that, except for the first column, all columns had the same width? – Adelyn Feb 16 '16 at 18:49
  • 1
    If you refer to my reply and click on the last highlighted "An old question on tex", you will be forwarded to source and you can read more details too. Glad, I could be of some help. – Mist Feb 17 '16 at 08:39