How can I center the column when using X in tabularx environment like in this example?
\begin{tabularx}{\textwidth}{@{}lXXXXX@{}}
1 & 2 & 3 & 4 & 5 \\
\end{tabularx}
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It only takes a minute to sign up.
Sign up to join this communityI recently dealt with the same task, so here I present my solution: I defined a new columntype Y
to center the cells in a tabularx
environment.
In the preamble define:
\newcolumntype{Y}{>{\centering\arraybackslash}X}
to typeset the example simply enter
\begin{tabularx}{\textwidth}{@{}lYYYYY@{}}
1 & 2 & 3 & 4 & 5 & 6\\
\end{tabularx}
You have two solutions (well at least)
If you are bound to tabularx
then you can use
\begin{tabularx}{\textwidth}{@{}l *5{>{\centering\arraybackslash}X}@{}}
1 & 2 & 3 & 4 & 5 & 6\\
\end{tabularx}
Alternatively you can use the tabu
package which offers you a lot more flexibility:
\begin{tabu} to \textwidth {@{} l *5{X[c]}@{}}
1 & 2 & 3 & 4 & 5 & 6\\
\end{tabu}
\raggedright
! However, a solution to this question (and this really should be a separate question rather than a comment to a vaguely related one), is given in this answer which uses the same package as suggested by herbert below
Sep 27, 2014 at 0:45
\usepackage{tabularx,ragged2e}
\renewcommand\tabularxcolumn[1]{>{\Centering}p{#1}}
with ragged2e
you do not need the \arraybackslash
It’s easy peasy to set cell alignment with tblr
environment of the new LaTeX3 package tabularray
:
\documentclass{article}
\usepackage{tabularray}
\begin{document}
\begin{tblr}{
colspec = {@{}X[c]X[l]X[c]X[r]X[c]@{}},
hlines, vlines,
}
1 & 2 & 3 & 4 & 5 \\
\end{tblr}
\end{document}
c
instead ofX
.