[Tex/LaTex] the difference between tabular, tabular* and tabularx environments

tablestabularx

There have been quite a few hit on Google, including one in Tex SE, but none of those made a complete sense to me. Please don't write something like:

tabular* adjusts the space between text of adjacent columns to get a
given table width, tabularx leaves this intercolumn space fixed,
instead adjusts the text width within the "X" columns for same
purpose.

Can anyone provide an MWE that makes sense? Code speaks better than words.

Best Answer

Check out the following MWE:

\documentclass{article}

\usepackage{tabularx}

\begin{document}

\begin{tabular}{|l|c|r|}
  \hline
  foo   & bar    & fubar \\
  fubar & toobar & foo \\
  \hline
\end{tabular}

\vspace{1cm}

\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}|l|c|r|}
  \hline
  foo   & bar    & fubar \\
  fubar & toobar & foo \\
  \hline
\end{tabular*}

\vspace{1cm}

\begin{tabularx}{\textwidth}{|X|X|X|}
  \hline
  foo   & bar    & fubar \\
  fubar & toobar & foo \\
  \hline
\end{tabularx}

\end{document}

enter image description here

As you notice, with the default tabular environment, columns are as wide as they need to be in order to accommodate their content.

In the tabular* version, LaTeX will add supplementary space between the columns in order to make the table as wide as specified (i.e. \textwidth). However, the columns' widths do not change. You can see that the content of the second column is not centered relatively to the two vertical bars.

If the design objective is to produce a table with equal-width columns that contain left-justified material, best results are obtained by using tabularx. The new X column type will have LaTeX automatically calculate the width of the column in order to make the table as wide as specified. You might wonder why the contents are not centered in my example. You can define centered columns with automatic width calculation, I just wanted to give a quick example without too much overhead. The package's documentation is very good, so you can easily adapt my example to suit your needs.