5

I want to draw the following table in LaTeX. In the first column the text should be left aligned. The text in all other cells should be centered. The first problem occurs with D in the first row. If I write a \centering in front of D, I get a LaTeX error. The second problem is, that I want to vertically align the text in all cells but I don't know how. I have found a solution but it doesn't work if the width of the first column is specified.

\documentclass{article}
\usepackage{blindtext}
\usepackage{tabularx}

\begin{document}
  \begin{tabularx}{\textwidth}{|p{4cm}|X|X|X|X|}
    \hline
               & \centering A & \centering B & \centering C & D\\
    \hline
    \blindtext & 123 & 123 & 123 & 123\\
    \hline
  \end{tabularx}
\end{document}
  • 1
    Add \arraybackslash to \centering or use \tabularnewline instead of \\. – Qrrbrbirlbel Apr 13 '13 at 0:55
6

For horizontally and vertically centered text redefine the X column definition:

\documentclass{article}
\usepackage{ragged2e}
\usepackage{tabularx}
\renewcommand\tabularxcolumn[1]{>{\Centering}m{#1}}

\newcommand\TEXT{%
I want to draw the following table in Latex. In the first column the text should be left aligned. The text in all other cells should be centered.}% only for demo


\begin{document}
\begin{tabularx}{\textwidth}{|m{4cm}*4{|X}|}\hline
           &  A  &   B &   C & D         \\\hline
\TEXT\TEXT & 123 & 123 & 123 & \TEXT\\\hline
\end{tabularx}
\end{document}

enter image description here

  • Perfect! This is exactly what I want... – user4811 Apr 13 '13 at 12:22
5

You can do the alignment with standard TeX methods but better with LaTeX's ragged2e package:

\documentclass{article}
\usepackage{blindtext}
\usepackage{tabularx}

\usepackage{ragged2e}


\begin{document}
  \begin{tabularx}{\textwidth}{|>{\RaggedRight}p{4cm}*{5}{|>{\Centering}X}|}
    \hline
               &  A & B & C & D\\
    \hline
    \blindtext & 123 & 123 & 123 & 123\\
    \hline
  \end{tabularx}
\end{document}

An X column is basically a p{...} column. Therefore you can use the commands like \RaggedRight at the beginning of each cell. To do that automatically for each cell, just use >{..}, which inserts the contents of the curly braces at the beginning of each cell in that row.

To avoid the repetition of the last five cells, I have inserted the construct *{5}{...}.

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.