[Tex/LaTex] How to set exact “tabular “ width

tables

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?

enter image description here

\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 writes

\begin{tabular}{ | p{1\linewidth} | }

then 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 -- is 1\linewidth+2\tabcolsep. The total width of the entire tabular environment will be 1\linewidth+2\tabcolsep+2\arrayrulewidth.

Similarly, for

\begin{tabular}{|p{0.5\linewidth}|p{0.5\linewidth}|}

the combined usable widths of the two columns is 1\linewidth, the combined total width of the two columns is 1\linewidth+4\tabcolsep, and the total width of the entire tabular environment equals 1\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 exactly 1\linewidth. By why trouble yourself with such tedious matters if one can use a tabularx environment and its X column type. For all intents and purposes, an X column is a p 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

\begin{tabular}{| *{2}{p{\dimexpr0.5\linewidth-2\tabcolsep-1.5\arrayrulewidth\relax}|}}

or

\begin{tabularx}{\textwidth}{|*{2}{X|}}

I trust the decision is anything but a close call.


enter image description here

\documentclass{article}
\usepackage{tabularx}
\begin{document}
\setlength\parindent{0pt} % just for this example

\subsubsection*{Incorrectly constructed 1-, 2-, and 4-column tabular environments}

\begin{tabular}{ | p{1\linewidth} |}
\hline Day \\ \hline
\end{tabular}

\smallskip

\begin{tabular}{|*{2}{p{0.5\linewidth}|}}
\hline Day & Day \\ \hline
\end{tabular}

\smallskip

\begin{tabular}{|*{4}{p{0.25\linewidth}|}}
\hline Day & Day & Day & Day \\ \hline
\end{tabular}


\subsubsection*{Correctly constructed 1-, 2-, and 4-column tabular environments}

\begin{tabular}{|p{\dimexpr1\linewidth-2\tabcolsep-2\arrayrulewidth\relax}|}
\hline Day \\ \hline
\end{tabular}

\smallskip

\begin{tabular}{|*{2}{p{\dimexpr0.5\linewidth-2\tabcolsep-1.5\arrayrulewidth\relax}|}}
\hline Day & Day \\ \hline
\end{tabular}

\smallskip

\begin{tabular}{|*{4}{p{\dimexpr0.25\linewidth-2\tabcolsep-1.25\arrayrulewidth\relax}|}}
\hline Day & Day & Day & Day \\ \hline
\end{tabular}


\subsubsection*{1-, 2-, and 4-column tabularx environments}

\begin{tabularx}{\textwidth}{ | X |}
\hline Day \\ \hline
\end{tabularx}

\smallskip

\begin{tabularx}{\textwidth}{|*{2}{X|}}
\hline Day & Day \\ \hline
\end{tabularx}

\smallskip

\begin{tabularx}{\textwidth}{|*{4}{X|}}
\hline Day & Day & Day & Day \\ \hline
\end{tabularx}

\end{document}