83

This code produces a table with three centered column:

\begin{table}[htbp]
    \centering
    \begin{tabularx}{\textwidth}{| c | c | c |}
        \hline
        Alpha     & Beta     & Gamma     \\ \hline
        0         & 2        & 4         \\ \hline
        1         & 3        & 5         \\ \hline
    \end{tabularx}
\end{table}

This code produces a full-width table with columns of equal width:

\begin{table}[htbp]
    \centering
    \begin{tabularx}{\textwidth}{| X | X | X |}
        \hline
        Alpha     & Beta     & Gamma     \\ \hline
        0         & 2        & 4         \\ \hline
        1         & 3        & 5         \\ \hline
    \end{tabularx}
\end{table}

I can't get how to produce full-width table, with centered header (~ first row) and columns of width |50%|25%|25%| respectively?

Would be nice if I could define these styles somewhere at the beginning of the document, so that all further tables will be able to use it.

5
  • 2
    See: en.wikibooks.org/wiki/LaTeX/…
    – mythealias
    Commented Nov 27, 2012 at 0:17
  • 2
    @mythealias Yes, of course I've been there, but haven't found solution (or maybe haven't understood it), so I am asking here. :) Commented Nov 27, 2012 at 0:22
  • It is the third example for tabularx, but for some reason they do it in a convoluted manner by combining two columns. Also no harm in asking, I was just being lazy to write an answer :)
    – mythealias
    Commented Nov 27, 2012 at 0:34
  • OK, thank you. I think, reading convoluted code is often useful for general understanding. So, I'll try to figure out example from your link. Commented Nov 27, 2012 at 0:41
  • 1
    it is actually similar to the answer posted by cmhughes below. only that the space between second and third column is removed and the columns are right and left justified instead of being centered.
    – mythealias
    Commented Nov 27, 2012 at 0:55

6 Answers 6

78

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

\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}
2
  • 1
    the total with has to be 3\hsize so if you have 2 s then b should be \hsize=2\size so you have 2+.5+.5=3 Commented Jan 26, 2017 at 20:14
  • 4
    For me, the b and s approach didn’t quite work, when I needed {|sb|}. The right border went a centimeter or so inside the table. So I used \newcolumntype{s}{>{\hsize=.25\hsize \raggedright\arraybackslash}X} \newcolumntype{b}{>{\hsize=.75\hsize}X} and \begin{tabularx}{\textwidth}{|sb|} which looks fine, though I don’t really know what all of the commands are doing, so I’m unaware of a better approach. Commented Mar 25, 2019 at 17:52
18

Knowing that each column has two "tab separators" and 4/3's of an \arrayrulewidth, you can subtract that from a regular p-column specifier:

enter image description here

\documentclass{article}
\usepackage{tabularx}% http://ctan.org/pkg/tabularx
\begin{document}

\noindent
\begin{tabularx}{\textwidth}{| X | X | X |}
  \hline
   Alpha     & Beta     & Gamma     \\ \hline
   0         & 2        & 4         \\ \hline
   1         & 3        & 5         \\ \hline
\end{tabularx}

\noindent
\begin{tabular}{
  |p{\dimexpr.5\linewidth-2\tabcolsep-1.3333\arrayrulewidth}% column 1
  |p{\dimexpr.25\linewidth-2\tabcolsep-1.3333\arrayrulewidth}% column 2
  |p{\dimexpr.25\linewidth-2\tabcolsep-1.3333\arrayrulewidth}|% column 3
  }
  \hline
  \centering Alpha     & \centering Beta     & \centering\arraybackslash Gamma     \\ \hline
  0         & 2        & 4         \\ \hline
  1         & 3        & 5         \\ \hline
\end{tabular}
\end{document}

Centering is obtained using \centering within the required cell (knowing that you're dealing with a p-column, this is possible). The modification of this alignment requires a small correction (via \arraybackslash) to re-establish the use of \\ in a tabular.

0
14

My new LaTeX3 package tabularray is an alternative to the outdated tabu package:

\documentclass{article}
\usepackage{tabularray}
\begin{document}

\begin{table}[htbp]
    \centering
    \begin{tblr}{
      colspec={|X[2]|X|X|}, row{1} = {c}, hlines,
    }
      Alpha & Beta & Gamma \\
        0   &  2   &   4   \\
        1   &  3   &   5   \\
    \end{tblr}
\end{table}

\end{document}

enter image description here

4

As tabu (outdated) and tabularray, the package nicematrix provides weight of the X columns of its environment {NiceTabular}.

\documentclass{article}
\usepackage{nicematrix}
\begin{document}

\begin{table}[htbp]
    \centering
    \begin{NiceTabular}{|X[2]|X|X|}[hlines]
      Alpha & Beta & Gamma \\
        0   &  2   &   4   \\
        1   &  3   &   5   \\
    \end{NiceTabular}
\end{table}

\end{document}

Output of the above code

3

An alternative is to use the tabu environment. You can give "weights" (width coefficients) to X columns (X[2]):

\documentclass{article}
\usepackage{tabu}

\begin{document}

\begin{table}[htbp]
    \centering
    \begin{tabu} to \textwidth {| X[2] | X | X |}
        \hline
        \centering Alpha & \centering Beta & \centering Gamma \\ \hline
        0                & 2               & 4                \\ \hline
        1                & 3               & 5                \\ \hline
    \end{tabu}
\end{table}

\end{document}
1
1

Simple like that

\documentclass{article}
% table
\usepackage{tabularx}
\begin{document}
\begin{table}[htbp]
    \centering
    \begin{tabularx}{\textwidth}{|>{\hsize=.5\hsize}X|>{\hsize=.25\hsize}X|>{\hsize=.25\hsize}X|}
        \hline
        Alpha     & Beta     & Gamma     \\ \hline
        0         & 2        & 4         \\ \hline
        1         & 3        & 5         \\ \hline
    \end{tabularx}
\end{table}
\end{document}
1
  • 1
    Works by chance. It should be \hsize=1.5\hsize for the first column and \hsize=0.75\hsize for the other two.
    – egreg
    Commented Nov 6, 2022 at 23:25

You must log in to answer this question.

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