Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

This question already has an answer here:

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.

share|improve this question

marked as duplicate by Tobi, Harish Kumar, Red, percusse, Benedikt Bauer Oct 22 '13 at 10:20

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3 Answers 3

up vote 1 down vote accepted

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

share|improve this answer
    
Seems to work perfectly! Thanks a lot. –  Håkon Hægland Oct 22 '13 at 9:18

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?

share|improve this answer
    
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! –  Håkon Hægland Oct 22 '13 at 9:21
    
I'd like to accept your answer too, but I had to choose one.. –  Håkon Hægland Oct 22 '13 at 10:12

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).

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.