6

Tell me please, why the sum of columns width in a table is less than 1? That is my code:

\begin{tabular}
{p{0.15\textwidth}p{0.12\textwidth}p{0.17\textwidth}p{0.17\textwidth}p{0.27\textwidth}}
...
\end{tabular}

0.15 + 0.12 + 0.17 + 0.17 + 0.27 = 0.88 Only at that case I have a textwidth table. If the sum is equal one I have a more width table.

3 Answers3

9

A tabular specification such as

\begin{tabular}{
  p{0.15\textwidth}
  p{0.12\textwidth}
  p{0.17\textwidth}
  p{0.17\textwidth}
  p{0.27\textwidth}
}

means reserving 0.88\textwidth for the columns. However, each column is surrounded on either side by spaces as wide as the value of \tabcolsep (default 6pt).

If you really need columns with those widths, you can locally set \tabcolsep; assuming that the first column has no space on its left and the last column no space at its right, you still have eight spaces to take care of. So we want to divide 0.12\textwidth equally among eight chunks, and the right width is 0.015\textwidth; here's an example

\documentclass{article}
\usepackage{booktabs}

\usepackage{showframe,lipsum} % just for the example

\begin{document}

\lipsum[2]

\begin{table}[htp]

\setlength{\tabcolsep}{0.015\textwidth}

\begin{tabular}{
  @{}% remove space at the left
  p{0.15\textwidth}
  p{0.12\textwidth}
  p{0.17\textwidth}
  p{0.17\textwidth}
  p{0.27\textwidth}
  @{}% remove space at the right
}
\toprule
aa aa aa aa aa & bb bb bb bb bb & cc cc cc cc cc cc cc cc &
  dd dd dd dd dd dd dd dd & ee ee ee ee ee ee ee ee ee ee ee ee \\
\bottomrule
\end{tabular}

\caption{A table}

\end{table}

\lipsum[3]

\end{document}

enter image description here

egreg
  • 1,121,712
7

Using TeX or LaTeX ways to estimate the length of the columns without the additional column space and taking that into account again.

This shows, that 0.88\textwidth is already too large!

\documentclass{article}
\newlength{\dummylength}

\newbox\mybox

\setbox\mybox=\hbox{%
  \begin{tabular}{p{0.15\textwidth}p{0.12\textwidth}*{2}{p{0.17\textwidth}}p{0.27\textwidth}}
    Foo & Foo & Foo & Foo & Foo%
  \end{tabular}%
}%

\def\numofcolumns{5}

\begin{document}

Textwidth is: \the\textwidth

Width of the tabular box: \the\wd\mybox

\setlength{\dummylength}{0.88\textwidth}

\numofcolumns\ columns introduce an additional length of \the\dimexpr\tabcolsep * \numofcolumns*2


0.88 textwidth is \the\dummylength 

Now let's add up the column textwidths again: \the\dimexpr0.17\textwidth*2+0.27\textwidth*2

\setlength{\dummylength}{\dimexpr\dummylength+\tabcolsep*\numofcolumns*2}

Estimated total width:  \the\dummylength

There's a difference of \the\dimexpr\wd\mybox - \dummylength

\noindent\unhbox\mybox%
\end{document}

enter image description here

  • What puzzles me: Why is there this small (tiny!) difference? –  Jul 01 '16 at 21:23
  • Why 0.88\textwidth is too large since \the\dimexpr\wd\mybox < \dummylength ? is it supposed to be even smaller – Cfun Jul 01 '16 at 21:30
  • @SAM: The reported box width is smaller than what I expected! –  Jul 01 '16 at 21:36
  • @ChristianHupfer: I think that the small discrepancy of -0.00526pt is entirely due to rounding errors within your \dimexpr; indeed, adding up the summands in a different way I get 363.59642pt, as expected. While we are at it: remember to say \noindent before \unhbox\mybox. – GuM Jul 02 '16 at 00:07
  • @GustavoMezzetti: I suspected rounding errors too initially, but then thought: 'hey, it's \dimexpr, that should be able to cope with such values' ;-) –  Jul 02 '16 at 08:51
7

The width of your tabular environment is 0.88\textwidth plus 10\tabcolsep. The default value of \tabcolsep is 6pt. If and only if the width of the text block happens to be 500pt (ca. 6.92" or 17.57cm) will it be the case that the width of this tabular environment equals \textwidth. (Why 500pt? Hint: solve (1-0.88)*\textwidth=60pt for \textwidth.)

To simplify the setting up of a table whose overall width is equal to \textwidth and which has five columns whose proportional widths are 15 : 12 : 17 : 17 : 27, I suggest you use a tabularx environment. Note that the relative column widths -- as a fraction of the width if all columns were equally wide -- may be expressed as 0.852\hsize, 0.682\hsize, 0.966\hize (twice), and 1.534\hsize, respectively. (Observe that 0.852+0.682+0.966+0.966+1.534=5, the number of columns.) Hence, define the tabularx environment as follows:

\documentclass{article}
\usepackage{tabularx}
\newcolumntype{Z}[1]{>{\hsize=#1\hsize}X}
\begin{document}
\noindent
\begin{tabularx}{\textwidth}{ Z{0.852} Z{0.682} *{2}{Z{0.966}} Z{1.534} }
\hline
a & b & c & d & e \\
\hline
\end{tabularx}
\smallskip
\hrule % just to illustrate the width of the textblock
\end{document}
Mico
  • 506,678