# Tabular with p type columns to fill page width

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.

-

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

-
it isn't perfect (I think there is another separator unaccounted for somewhere) but it good enough for now. –  111111 Jul 9 '12 at 14:05
no, then you are doing something wrong, e.g. a missing \noindent before the table if you have by default an indentation before paragraphs –  Herbert Jul 9 '12 at 14:06
Wouldn't your code generate a tabular environment that's 5*0.4pt=2pt wider than \linewidth because of the five vertical bars (each of width 0.4pt)? –  Mico Jul 9 '12 at 14:10
no, only if you are using package array ... –  Herbert Jul 9 '12 at 14:13
If package array is loaded, then the width of a vertical line (\arrayrulewidth) also counts. –  Heiko Oberdiek Apr 14 at 4:24

You may be looking for something like the following, which requires loading the tabularx package:

\noindent % needed if the tabularx environment isn't encased in a table environment
\begin{tabularx}{\textwidth}{|*{4}{X|}}
...
\end{tabularx}


In the title of your posting, you mention you want a solution using columns of type p{<some width>}. The X column provided by the tabularx package is essentially a p column; the tabularx package just saves you the tedium of having to calculate the column widths explicitly. :-)

Addendum This can be generalized to (a) tables that take up a width of less than 1\textwidth -- just specify the intended width as the first argument of the tabularx environment, either as an absolute width statement (e.g., 10cm) or as a relative width statement (e.g., 0.85\textwidth) -- and (b) tables with unequal column widths. E.g., assume that the first and third columns, and the second and fourth columns in the first example should have equal widths, and that the second/fourth columns should be 50% wider than the first/third columns. This can be set up with the following (at first slightly bewildering) instruction

\begin{tabularx}{\textwidth}{|>{\hsize=0.8\hsize}X| >{\hsize=1.2\hsize}X |
>{\hsize=0.8\hsize}X| >{\hsize=1.2\hsize}X |}


Of course, this can be expressed slightly more succinctly as

\begin{tabularx}{\textwidth}{| *{2}{>{\hsize=0.8\hsize}X| >{\hsize=1.2\hsize}X |}}


The point to note is that the sum of the fractions of \hsize must equal the number of columns of type X (in this example, 4).

-
Thanks, I'll try that now, any ideas as to why my attempted didn't (to help my understanding of what is going on). –  111111 Jul 9 '12 at 13:40
@111111 -- I believe your trial code is wider than \linewidth because you have (a) 5 vertical bars (each of default width 0.4pt), (b) 3 intercolumn whitespaces (of default width 2\tabcolsep), and (c) and 1 vertical whitespace each at the beginning and end of the table (each of default width 1\tabcolsep). Finally, \tabcolsep=5pt by default. All this adds up to 2pt+40pt = 42pt, or almost 15mm. –  Mico Jul 9 '12 at 13:55
I have tried using tabularx with the columns being equal fraction of space I want them to take up (using \hsize=frac\hsize) as you have shown, but the enumerations are forcing a box to be larger for sum reason –  111111 Jul 9 '12 at 13:58
@111111 -- please edit your question to post a MWE that generates the problem behavior you're encountering. Without having access to the precise code you're executing, it's well-nigh impossible to diagnose (let alone fix) the problem. Also, depending on the complexity of the tabularx setup, you may have to run latex more than once in order for the algorithm to "converge". –  Mico Jul 9 '12 at 14:01
Sorry for being vague and thanks for your help. I went for accounting for the colsep in the size rather than tab'x because I need to get it done for now. But I will bear it in mind in the future. –  111111 Jul 9 '12 at 14:05

I would like to present an answer for beginners with Latex.

With the package tabularx´ it's easy to set custom column widths.

\documentclass[12pt]{article}

\usepackage{tabularx}

\usepackage{fontspec}
\defaultfontfeatures{Mapping=tex-text}
\setmainfont[Ligatures=TeX]{Arial}

\usepackage[textwidth=16cm]{geometry}

\begin{document}

\begin{table}[htb]
%\centering
\begin{tabularx}{\textwidth}{|>{\hsize=.8\hsize}X|>{\hsize=.5\hsize}X|>{\hsize=1.6\hsize}X|>{\hsize=1.5\hsize}X|>{\hsize=.6\hsize}X|}
\hline
\textbf{Framework} & \textbf{License} & \textbf{Functionality} & \textbf{Others} & \textbf{Support} \\
\hline
etc. & etc. & etc. & etc. & etc \\
\end{tabularx}
\caption{Our Framework}
\label{tab:framework}
\end{table}
\end{document}
`

Attention: hsize=.8\hsize is not the percentage of the whole textwidth.

It's a decleration for increase and decrease.

You have just to make sure that the sum of increase and decrease are the same.

-0.2 - 0.5 - 0.4 = - 1,1

0.6 + 0.5 = 1,1

That's it.

-