[Tex/LaTex] Tabular with p type columns to fill page width

columnstableswidth

I wish to be able to have a table (using tabular) that fills the page horizontally with each column using a fraction of that space, for now assume that they are equally sized.

I wish to be able to use enumerations inside of the tabular, so I am using parabox's to achieve this.

Currently I have got:

\begin{tabular}{ | p{0.25\linewidth} |
                   p{0.25\linewidth} |
                   p{0.25\linewidth} |
                   p{0.25\linewidth} | } 
%content here after

except this uses all the space from the first margain to the end of the page. Which looks awful. I really want to avoid hard coding in any dimensions because I will want to play around with margins and orientation later.

If I could get it so that I can use a proportion of the text width for each column that would be great.

Best Answer

each tabular column has on the left and right a separation of \tabcolsep which must be substract from the column width if you want it to be of the length \linewidth

\documentclass{article}
\parindent=0pt
\begin{document}

\hrulefill

\begin{tabular}{ | p{\dimexpr 0.25\linewidth-2\tabcolsep} |
                   p{\dimexpr 0.25\linewidth-2\tabcolsep} |
                   p{\dimexpr 0.25\linewidth-2\tabcolsep} |
                   p{\dimexpr 0.25\linewidth-2\tabcolsep} | } \hline
foo & bar & baz & foobar \\\hline
\end{tabular}                

\end{document}

However, using tabularx makes more sense:

\documentclass{article}
\usepackage{tabularx}
\parindent=0pt
\begin{document}

\hrulefill

\begin{tabularx}{\linewidth}{ | X | X | X | X | } \hline
foo & bar & baz & foobar \\\hline
\end{tabularx}                

\end{document}
Related Question