6

When I put X in column specifier, how can I define column formatting? How can I get center alignment? Currently I am putting \centering, but that is not a proper solution.

This is my code:

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{patterns}
\usepackage{booktabs,tabularx}
\usepackage[margin=1in]{geometry}% http://ctan.org/pkg/geometry


\begin{document}

\begin{table}[t]
\caption{Case studies}
\centering
 \begin{tabularx}{\textwidth}{p{4cm}*{5}{X}}
    \toprule
\centering Nature of unreliability & \centering Sender SST & \centering Receiver SST & \centering Implementation SST &  Retransmission Bound \\


\midrule

Noisy (Single fixed error message) & DSST & DSST & DSST & Unbounded \\
\bottomrule
\end{tabularx}
\label{case-studies}
\end{table}



\end{document}
4
  • 2
    \centering is the proper solution. But if you use it in the last column you should use \tabularnewline to end the row of the tabular. Feb 7 '13 at 12:27
  • 2
    If you want it to apply to the whole column you can use >{\centering \arraybackslash}X as it suggests in the tabularx manual. Feb 7 '13 at 12:41
  • Please, clarify if you need to center align only one row or all of them.
    – egreg
    Feb 7 '13 at 13:41
  • @DavidCarlisle Please check my answer -- for whatever reason, >{\centering \arraybackslash}X does not suffice.
    – lockstep
    Feb 7 '13 at 14:20
12

My answer to How to create fixed width table columns with text raggedright/centered/raggedleft? can also be applied in slightly modified form to X columns. (Note that omitting \let\newline\\ will result in text that is not properly centered.)

\documentclass{article}

\usepackage{tabularx}

\usepackage{array}
\newcolumntype{Z}{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}X}

\begin{document}

\begin{tabularx}{\linewidth}{| c | c | Z |}
Abc & Bcd & Cde \\
Abc & Bcd & A long cell with text that is centered \newline
    and allows manual line breaks \\
\end{tabularx}

\end{document}

enter image description here

2
  • 1
    yes \centering only redefines \` not \newline` so if you need manual breaks you can do as here, or don't use \arraybackslash and use \` for centering break and \tabularnewline` for end of table row, or perhaps simplest you can use a blank line to force a new centred parapgraph Feb 7 '13 at 16:45
  • My god what a rubbish package that requires the definition of a new custom class just to center text horizontally.
    – SomJura
    Aug 2 at 20:04

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