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.

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}
share|improve this question
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. –  Ulrike Fischer Feb 7 '13 at 12:27
1  
If you want it to apply to the whole column you can use >{\centering \arraybackslash}X as it suggests in the tabularx manual. –  David Carlisle 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
add comment

1 Answer

up vote 6 down vote accepted

My answer to Fixed width column with text centered/raggedright/raggedleft in tabular? 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

share|improve this answer
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 –  David Carlisle Feb 7 '13 at 16:45
add comment

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.