TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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}
share|improve this question
up vote 4 down vote accepted

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}
share|improve this answer
    
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 3 at 19:07
    
@mico -- some other ways of adding more "air" to table rows: extrarowheight vs arraystretch – barbara beeton Feb 3 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 3 at 19:41
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 3 at 22:48
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 4 at 15:32

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

share|improve this answer
    
You're right. I didn't think of this aspect of thngs. I'll change my code in a moment. – Bernard Feb 3 at 20:56

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

share|improve this answer
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 3 at 22:17
1  
Hello @Adelyn I have edited my answer so you may see it again for both cases – Mist Feb 15 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 at 15:36
1  
Answer has been further updated. Hope it helps. Cheers – Mist Feb 16 at 16: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 at 8:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.