0

Is there a neat way to define all column widths in a table as the same value and adapted to the page size? For example I would like the following 32 columns to have all equal width of 1/32 of the maximum page width.

\begin{tabular}{p{0.2}p{0.2}p{0.2}p{0.2}p{0.2}p{0.2}p{0.2}p{0.2}p{0.2}p{0.2}p{0.2}p{0.2}p{0.2}p{0.2}p{0.2}p{0.2}p{0.2}p{0.2}p{0.2}p{0.2}p{0.2}p{0.2}p{0.2}p{0.2}p{0.2}p{0.2}p{0.2}p{0.2}p{0.2}p{0.2}p{0.2}p{0.2}}
            1& 1 & 1 & 1 & 1 & 0 & 1 & 1 & 1 & 1 & 0 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 0 \\
\end{tabular}
        \captionof{table}{The solution to the backpacker problem of the first matrix obtained with analytical considerations}

Using this code I get a table that gets beyond the leftmost bound of the page. How can I fix this issue?

enter image description here

  • 1
    See the tabularx package and its X type columns. – Skillmon Jan 17 at 13:35
  • Without the usage of tabularx: p{\textwidth/32-2\tabcolsep} (needs the calc package instead). – leandriis Jan 17 at 16:05
1

The following MWE contains two possible solutions. The first uses X columns from tabularx to automatically make the table fit into the textwidth. (The widths of X columns are calculated automatically). The second version manually calculates the widths for the columns with respect to the textwidth taking the length \tabcolsep(Distance that is added before and after the contents of each cell = half the distance between the contents of two adjacent cells) into account.

In both cases you might still need to adjust the above mentioned distance. This is done via \setlength{\tabcolsep}{4pt} (default is 6pt).

\documentclass{article}

\usepackage{geometry}
\usepackage{capt-of}
\usepackage{tabularx}

\usepackage{calc}

\begin{document}
\setlength{\tabcolsep}{4pt}
\noindent
\begin{tabularx}{\textwidth}{*{32}{X}}
            1& 1 & 1 & 1 & 1 & 0 & 1 & 1 & 1 & 1 & 0 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 0 \\
\end{tabularx}
\captionof{table}{With X type columns from tabularx}

\vspace{1cm}

\noindent
\begin{tabular}{*{32}{p{\textwidth/32-2\tabcolsep}}}
            1& 1 & 1 & 1 & 1 & 0 & 1 & 1 & 1 & 1 & 0 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 0 \\
\end{tabular}
\captionof{table}{With p type columns}

\end{document}

enter image description here

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.