I'm using the tabular
environment while specifying the width of each column, as shown below. But I wonder why the more columns, the wider the table even though the sum of column widths is always one full linewidth.
Is there any way to ensure the exact table width with all columns widths specified?
\documentclass{article}
\begin{document}
1-column table
\begin{tabular}{ | p{1\linewidth} |}
\hline
Day\\
\hline
\end{tabular}
\vspace{1cm}
2-column table
\begin{tabular}{|p{0.5\linewidth}|p{0.5\linewidth}|}
\hline
Day & Day\\
\hline
\end{tabular}
\vspace{1cm}
4-column table
\begin{tabular}{|p{0.25\linewidth}|p{0.25\linewidth}|p{0.25\linewidth}|p{0.25\linewidth}|}
\hline
Day & Day & Day & Day\\
\hline
\end{tabular}
\vspace{1cm}
Why is the table width lengthened as the number of columns increases even though the sum of column widths is one full linewidth alike?
\end{document}
Best Answer
One needs to distinguish between the usable width and the total width of a column of type
p
. If one writesthen the usable width of the one-and-only column is indeed
1\linewidth
. However, because of whitespace padding on either side of the column, the total width of the column alone -- not including the vertical bars on either side -- is1\linewidth+2\tabcolsep
. The total width of the entiretabular
environment will be1\linewidth+2\tabcolsep+2\arrayrulewidth
.Similarly, for
the combined usable widths of the two columns is
1\linewidth
, the combined total width of the two columns is1\linewidth+4\tabcolsep
, and the total width of the entiretabular
environment equals1\linewidth+4\tabcolsep+3\arrayrulewidth
.I suppose one could adjust the usable widths appropriately by hand, so that the total width of the
tabular
environment works out to be exactly1\linewidth
. By why trouble yourself with such tedious matters if one can use atabularx
environment and itsX
column type. For all intents and purposes, anX
column is ap
column for which LaTeX performs the calculations needed to keep the overall width to its target value (usually, but not necessarily,1\linewidth
).E.g., would you rather write
or
I trust the decision is anything but a close call.