62

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
  • 1
    See: en.wikibooks.org/wiki/LaTeX/…
    – mythealias
    Nov 27 '12 at 0:17
  • 1
    @mythealias Yes, of course I've been there, but haven't found solution (or maybe haven't understood it), so I am asking here. :) Nov 27 '12 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
    Nov 27 '12 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. Nov 27 '12 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
    Nov 27 '12 at 0:55
61

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
  • 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 Jan 26 '17 at 20:14
  • 1
    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. Mar 25 '19 at 17:52
16

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
4

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

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

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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