6

I'm using the p column type in order to force the width of the columns of my table. However, the total width of the table becomes larger that the textwidth. Here is a MWE:

\documentclass{article}
\usepackage{lipsum}

\begin{document}
\lipsum[1-5]

\begin{tabular}{|p{.3\textwidth}|p{.45\textwidth}|p{.125\textwidth}|p{.125\textwidth}|}
\hline
a & b & c & d\\ \hline
\end{tabular}

\end{document}

Is the source of the problem that p set the content width and doesn't account for the space between columns?

4

There are three factors you're not taking into account.

  1. The paragraph indent

  2. The intercolumn space

  3. The rule widths (if you're using array)

Maybe

\usepackage{calc}

and then

\noindent
\begin{tabular}{
  |p{.3\textwidth-2\tabcolsep}|
   p{.45\textwidth-2\tabcolsep}|
   p{.125\textwidth-2\tabcolsep}|
   p{.125\textwidth-2\tabcolsep}|
}

would do. Or you may want to shrink more the wider columns. Remember that there's a \tabcolsep wide space at either side of each column.

enter image description here

Note that if you're using the array package, the above code should become

\noindent
\begin{tabular}{
  |p{.3\textwidth-2\tabcolsep-2\arrayrulewidth}|
   p{.45\textwidth-2\tabcolsep-\arrayrulewidth}|
   p{.125\textwidth-2\tabcolsep-\arrayrulewidth}|
   p{.125\textwidth-2\tabcolsep-\arrayrulewidth}|
}

There are five rules which have to be taken care of; according to the LaTeX manual, the leftmost rule logically belongs to the first column; the other rules belong to the column preceding them.

8
  • Why is there a 2\arrayrulewidth for the first column and not the last one? From the output I see there has to be n+1 \arrayrulewidth, and so I could also dispatch it across the columns... – s__C Jan 16 '15 at 12:55
  • @s__C Sorry, it shouldn't be there – egreg Jan 16 '15 at 12:59
  • if you try \usepackage[showframe]{geometry} then you'd see why you need it no? – s__C Jan 16 '15 at 13:02
  • @s__C - One could subtract 1.25\arrayrulewidth from each for the four columns (for n=4, 1.25=(n+1)/n), rather than subtract 2\arrayrulewidth from the first column and 1\arrayrulwidth from the remaining three. However, given the (rather small) value of \arrayrulewidth, you'd be hard pressed to notice any practical difference between the two approaches. – Mico Jan 16 '15 at 13:57
  • @Mico as long as you do not turn \usepackage[showframe]{geometry} ON... – s__C Jan 16 '15 at 13:59
4

Rather than compute the column widths yourself, you can let LaTeX handle the job if you load the tabularx package and use that package's X column type.

If the ratios of the column widths to the width of the text block are as 0.45, 0.3, 0.125, and 0.125, the following setup will achieve your purpose. The only "rule" to remember is that the sum of the \hsizes must equal the number of columns -- 4 in the example you gave. To set the relative column widths, then, just multiply the ratios given above by 4.

enter image description here

\documentclass{article}
\usepackage{tabularx}
\begin{document}
\noindent
\begin{tabularx}{\textwidth}{|%
   >{\hsize=1.8\hsize}X|
   >{\hsize=1.2\hsize}X|
   >{\hsize=0.5\hsize}X|
   >{\hsize=0.5\hsize}X|}
\hline
a & b & c & d \\
\hline
\end{tabularx}
\end{document}

In case you're curious about the details: If the table is supposed to take up the width of the text block (i.e., if it has width \textwidth), has n columns (here: n=4), and the columns are separated by vertical rules, the table's usable width is \textwidth-2n\tabcolsep-(n+1)\arrayrulewidth. The tabularx environment then apportions the usable width according to the ratios 0.45, 0.3, 0.125, and 0.125. (To obtain the actual width of a column, add 2\tabcolsep to the column's usable width.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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