Take the 2-minute tour ×
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.

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.

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

2 Answers 2

up vote 13 down vote accepted

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}
share|improve this answer
    
Great, thanks! That is what I wanted. –  Edward Ruchevits Nov 27 '12 at 0:28
    
@EdwardRuchevits you're very welcome, happy TeXing :) –  cmhughes Nov 27 '12 at 0:32

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.

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.