The code below defines two new columntypes
: b
for 'big' and s
for 'small'
I followed the details of section 4.3 of the documentation
to create these.
![screenshot](https://i.stack.imgur.com/O6rMt.png)
\documentclass{article}
\usepackage{tabularx}
\newcolumntype{b}{X}
\newcolumntype{s}{>{\hsize=.5\hsize}X}
\begin{document}
\begin{table}[htbp]
\centering
%\begin{tabularx}{\textwidth}{| X | X | X |}
\begin{tabularx}{\textwidth}{bss}
\hline
Alpha & Beta & Gamma \\ \hline
0 & 2 & 4 \\ \hline
1 & 3 & 5 \\ \hline
\end{tabularx}
\end{table}
\end{document}
If you'd like to center the heading, then you can use something like the following, which defines a \heading
command to center its argument
\documentclass{article}
\usepackage{tabularx}
\newcolumntype{b}{X}
\newcolumntype{s}{>{\hsize=.5\hsize}X}
\newcommand{\heading}[1]{\multicolumn{1}{c}{#1}}
\begin{document}
\begin{table}[htbp]
\centering
%\begin{tabularx}{\textwidth}{| X | X | X |}
\begin{tabularx}{\textwidth}{bss}
\hline
\heading{Alpha} & \heading{Beta} & \heading{Gamma} \\ \hline
0 & 2 & 4 \\ \hline
1 & 3 & 5 \\ \hline
\end{tabularx}
\end{table}
\end{document}
Best Answer
@1: You can make use of the
@{}
notation to insert spaces between columns. Setting up atabular
with columns defined as{l@{\hspace{1cm}}l}
would insert 1 cm of space between the two left-aligned columns, which, due to their nature, have the width of their respective longest entry each.@2: There's an easy solution for the case "may not be user-specified"* using the
tabu
package. Atabu
allows to specify the table width and accordingly adjusts the width of columns of typeX
. The column width does indeed _not_ depend on the lenght of the longest entry, but if the table width (.7\textwidth
in the example) is of a sufficient amount, the epsilon, as you call it, will be 1/nth of the space necessary to stretch the width of n columns to the specified width of the table. Hence, as long as you ensure that the table width is sufficient (e.g. by using the default of `\textwidth, which you'd usually not want to exceed anyway), you get the desired result. The drawback is that the table doesn't get _less_ wide than specified.Remarks
X
columns can be prevented e.g. by inserting the text into an\mbox
.\hspace
is used purely for demonstration purposes; in case of 0 cm,@{}
could simply be used. Since 0 cm of intercolumn space are specified, the distance between two columns is entirely due to their width (to be observed by replacing\hspace
with a vertical line@{}|@{}
in the column definitions).tabu
, you could moreover specify that rather than all columns being equal in width, e.g. the first should be twice as wide as the others. This can be achieved by accordingly setting the optional parameter in the column definition, e.g. toX[2]
for the first column.Example
Code
Notes
*Your case "epsilon is user-specified" requires the table width to be not specified but dynamically determined. Currently I don't see an easy way of setting up equal-width columns in a table of variable width, but probably someone else will (cf. Addendum).
**A similar result can be achieved with
tabularx
.tabu
however has a more convenient user interface.***You can specify the column widths to be maximum values by adding a minus as prefix. Column widths specified this way however are no longer synchronously adjusted, and as far as I know, you cannot specify a maximum width for the complete table.
Addendum
I should add that the comment given in the first footnote was based on my implicit, but maybe false assumption that you would not know the length of the longest entry when setting up the table. Should this not be a requirement, then the solution is straightforward: You just need to measure the length of a box containing the longest entry and then define the columns based on this length.
In the code given below, a command
\setwidth
is called with a text and a dimension as parameters. It sets\width
, which then is used as width of each column. Except the text of the entry, the two table definitions shown in the example are identical.