5

Consider the following code:

\documentclass{article}
\usepackage{booktabs,pgf}
\begin{document}
\pgfmathsetlengthmacro{\ratio}{\the\textwidth/4}

\hrulefill \\

\begin{tabular}{@{}llll@{}}
  \toprule
  \makebox[\ratio][l]{xx}&\makebox[\ratio][l]{1} &\makebox[\ratio][l]{2}&\makebox[\ratio][l]{3}\\
  \makebox[\ratio][l]{xx}&\makebox[\ratio][l]{1} &\makebox[\ratio][l]{2}&\makebox[\ratio][l]{3}\\
  \bottomrule
\end{tabular}
\end{document}

with output

enter image description here

How can I determine \ratio such that the table fills (exactly) the textwidth of the page? As seen, currently the \ratio is too large such that the table becomes wider than the text width.

0

3 Answers 3

8

Here you have two options, second one uses tabularx and I've found it in "The LaTeX Companion, 2e" (page 251).

First one uses a p{.25\textwidth} format for every column and suppresses inter-column space width @{}. No need for pgfmath. Don't forget that if you leaves a blank line before \begin{tabular} it will start a new indented paragraph. You can suppress indentation with \noindent command.

\documentclass{article}
\usepackage{booktabs}
\usepackage{lipsum}
\usepackage{tabularx}
\newcolumntype{Y}{>{\raggedright\arraybackslash}X}

\begin{document}

\lipsum[1]

\noindent\begin{tabular}{@{}*{4}{p{.25\textwidth}@{}}}
  \toprule
  xx&1&2&3\\
  xx&1&2&3\\
  \bottomrule
\end{tabular}

\lipsum[1]

\noindent\begin{tabularx}{\textwidth}{YYYY}
  \toprule
  xx&1&2&3\\
  xx&1&2&3\\
  \bottomrule
\end{tabularx}

\end{document}

enter image description here

0
5

There is no reason to force tables to be as wide as the normal text width; I know it's easy to do it with a word processor, but it doesn't make for prettier tables. To the contrary, expanded tables are usually uglier and less readable because the items are spread too far apart from each other.

You can use tabularx, if you really want to do it:

\documentclass{article}
\usepackage{booktabs,tabularx}
\begin{document}

\noindent X\dotfill X

\bigskip

\noindent
\begin{tabularx}{\textwidth}{@{}XXXX@{}}
\toprule
xx & 1 & 2 & 3 \\
xx & 1 & 2 & 3 \\
\bottomrule
\end{tabularx}
\end{document}

enter image description here

The tabu package enhances the possibilities, but its current version is unmaintained and the author has announced that the next version will be largely incompatible with the current one. See Is the tabu package obsolete?

1
  • Yes I agree, but I plan to change the number of columns dynamically in a script based on some input from the user, so I think I would like to have the table fill the page for now. Anyway, thanks for the good answer! Commented Oct 22, 2013 at 9:21
3

You can use tabu (e.g). It will set the table to a given width without needing to calc the ration by hand.

\documentclass{article}

\usepackage{tabu}
\usepackage{booktabs}% for better rules in the table

\begin{document}
\begin{tabu} to \textwidth {XXXX}
   \toprule
   xx & 1 & 2 & 3 \\
   \bottomrule
\end{tabu}
\end{document}

tabu comes with the new column type X which sets it’s width automatically, it has an optional argument taking l, r, c to adjust the alignment inside the cell or a numer to set uneven widths of columns. For example two columns, the first on right, the second one left aligned and twice the width of the first one, will be X[r]X[2] (l and 1 will be set by default).

Not the answer you're looking for? Browse other questions tagged .