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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have the following table:

\begin{table}[htb]
\begin{tikzpicture}
\node (table) [inner sep=0pt] {
\begin{tabular}{ l | l }
  {\bf Symptom} & {\bf Metric} \\
\hline
Class that has many accessor methods and accesses a lot of external data & ATFD is more than a few\\
Class that is large and complex & WMC is high\\
Class that has a lot of methods that only operate on a proper subset of the instance variable set & TCC is low\\
\end{tabular}
};
\draw [rounded corners=.5em] (table.north west) rectangle (table.south east);
\end{tikzpicture}
\caption{God class symptoms}
\label{tbl:god_class}
\end{table}

Now I want to force the width of the table to be the same as the \textwidth, either by linewrapping of table text or by scaling. How can I achieve that?

share|improve this question
10  
You should not use {\bf <text>} but \textbf{<text>} or \bfseries instead! Same is true for \it and \tt or how they are called. They are all deprecated. Please see the l2tabu document for this and other things. – Martin Scharrer Feb 8 '11 at 0:16
up vote 84 down vote accepted

You can use the tabularx package. It allows you to set the width of the table and provides the X column type, which fills out the rest of the space. It can be used for several columns, which then share the rest of the width equally.

Example:

\usepackage{tabularx} % in the preamble
% ....
\begin{tabularx}{\textwidth}{X|l}
  \textbf{Symptom} & \textbf{Metric} \\
\hline
Class that has many accessor methods and accesses a lot of external data & ATFD is more than a few\\
Class that is large and complex & WMC is high\\
Class that has a lot of methods that only operate on a proper subset of the instance variable set & TCC is low\\
\end{tabularx}

In general it is also possible to set the width of a column using p{<width>} instead of l as column type. Then it will be formatted as a paragraph and can include line breaks. Replace <width> with the required width.

share|improve this answer
5  
If you're considering tabularx you might also consider its less well known (but sometimes better behaved) sibling tabulary – David Carlisle May 19 '12 at 17:03
    
How can we adjust the width of the columns? – prince Dec 9 '14 at 5:47
4  
I think this method does work when the table is small and you need to adjust it to fit the text width. However, it does not work when you have a large table that is overflowing the margins and you need it to be compressed. – deps_stats Nov 14 '15 at 22:07

Here is a simple way:

\newlength\q
\setlength\q{\dimexpr .5\textwidth -2\tabcolsep}
\noindent\begin{tabular}{p{\q}p{\q}}
alfa & bravo \\
charlie & delta
\end{tabular}
share|improve this answer

After trying the suggestions made in this stackexchange I found a different and fancy solution (without loading any other packages etc.). The key is define the width of each column.

   \begin{table}[ht!]
     \centering
     \caption{Caption text} 
     \begin{tabular}{|p{5cm}|p{9cm}|}
        Text column 1 & long long long long long long long text that should break \\
        \hline
      \end{tabular}
    \end{table}
share|improve this answer
    
So for the above example you'd need a text block that has width 5cm+9cm+(a bunch of other tabular measurements to make sure the table fits exactly)... you don't really specify this latter measurement. There is no real guarantee this will fit within the text block. – Werner Jan 26 at 21:44

Nowadays there is a wonderful way to do this without complicating yourself too much

https://en.wikibooks.org/wiki/LaTeX/Tables#Text_wrapping_in_tables

For instance I start my table like this:

\begin{tabular}{\textwidth}{|p{\textwidth/5}|p{\textwidth/5}|p{\textwidth/5}|p{\textwidth/5}|p{\textwidth/5}|}
share|improve this answer
    
I think you mean tabularx. This syntax is not valid for tabular – Steven Penny Mar 18 at 22:44

With ConTeXt, from the reference manual, section 16.5 TeX-figures (svnversion 329, September 27, 2013) :

\startbuffer[table]
\startTABLE
% fill your table here
\stopTABLE
\stopbuffer

\placefigure[none]{}{\externalfigure[table.buffer][width=\textwidth]}
share|improve this answer

One 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). The part between to and {<cols>} can be any width, and the full part can be omitted to, i.e. \begin{tabu}{<cols>}.

tabu is compatible with longtable with the new environment {longtabu}.


Adding showframeand some text (lipsum) to the above example shows that the table has exactly the width of the text. On may notic that a table without a float enviroment is set inline and gets indented as every normal text, too. Use \nointend to prevent that.

table

\documentclass{article}

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

\begin{document}
\lipsum[4]

\noindent
\begin{tabu} to \textwidth {XXXX}
   \toprule
   xx & 1 & 2 & 3 \\
   \bottomrule
\end{tabu}
\end{document}
share|improve this answer
1  
Thanks! But I think the table is still somewhat wider than the text width of the page (compared with \hrulefill).. – Håkon Hægland Oct 22 '13 at 9:06
1  
@HåkonHægland: As you can see in my edit, the table has exactly the same width as the text … – Tobi Oct 22 '13 at 9:39
    
Great package ! Thanks Tobi :-) – madit Nov 1 '13 at 19:57
    
Indeed but read tex.stackexchange.com/q/121841/4918 before falling in love to deeply ;-) – Tobi Nov 1 '13 at 21:22

Just to mention an additional method: the tabular* environment. Suppose you have a table with 6 center-aligned columns. You can force it to take up the full width of the textblock by setting it up as follows:

\begin{tabular*}{\textwidth}{c @{\extracolsep{\fill}} ccccc}
...
\end{tabular*}

Unlike the tabularx and tabulary environments, which work by expanding the width of the columns, the tabular* environment works by expanding the intercolumn whitespace.

Personally, I suspect it's the need to remember to insert the directive @{\extracolsep{\fill}} that has kept the popularity of this approach quite subdued...

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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