3

I want to center a tabularx, not the cells content, but the table itself.  Reading things about tabular, I tried \centering.

Here is my mwe:

% arara: xelatex
% arara: xelatex

\documentclass{scrartcl}

\usepackage{array}
\usepackage{tabularx}

\newcolumntype{°}{>{\global\let\currentrowstyle\relax}}
\newcolumntype{^}{>{\currentrowstyle}}

\begin{document}

{\small
\centering
\begin{tabularx}{200pt}{@{}°c^c^c@{}}
a & b & c
\\
d & e & f
\\
\end{tabularx}
}

\end{document}

It normally renders the tabularx, but not centered. I also tried \begin{center}\end{center} but without any success.

How do I center it?

  • 2
    Welcome to TeX SX! Since it is \linewidth wide, just using \noindent should be enough. – Bernard Mar 8 '18 at 13:53
  • Thanks. Where do I put the \noindent? – Marc Sharma Mar 8 '18 at 13:54
  • 1
    Just before \begin{tabularx}. – Bernard Mar 8 '18 at 13:57
  • It didn't work. – Marc Sharma Mar 8 '18 at 14:07
  • Why were you in such a hurry to "accept" one of the answers? On this site, it's usually a good idea to wait a while, but at least several hours, before "accepting" the best of (hopefully) several answers. – Mico Mar 8 '18 at 14:11
1

You use tabularx that needs at least one column of type X wich adjusts its width to fill and fit the width you gave initially as parameter.

Secondly you ask for a tabular of all linewidth. That means already centered as using all free space.

Here is something centered with one X column.

enter image description here

\documentclass{scrartcl}

\usepackage{array}
\usepackage{tabularx}

\newcolumntype{°}{>{\global\let\currentrowstyle\relax}}
\newcolumntype{^}{>{\currentrowstyle}}

\begin{document}

Left\dotfill

\begin{center}
\small
\begin{tabularx}{.5\linewidth}{@{}°c^Xc@{}}\hline
a & b & c
\\
d & e & f
\\\hline
\end{tabularx}
\end{center}

\dotfill Right

{\small
\hfill\begin{tabularx}{.5\linewidth}{@{}°c^Xc@{}}\hline
a & b & c
\\
d & e & f
\\\hline
\end{tabularx}\hfill\strut
}

Left\dotfill
\end{document}
|improve this answer|||||
  • I've edited my example for a more generic case, but the solution you provided works for any width, thanks! Do you know why \centering doesn't work even with a X column? – Marc Sharma Mar 8 '18 at 14:10
  • @MarcSharma - \centering works just fine with X-type columns; see my answer for an example. – Mico Mar 8 '18 at 14:12
  • @Mico I think he OP wants to center the tabular not the comlum. – Tarass Mar 8 '18 at 14:17
  • @MarcSharma In don't know why centering nor Centering don't work. To center something without the loss of vertical space of \begin{center} I use hfill. See my edit. You can upvote the different answers if they a glad to you. – Tarass Mar 8 '18 at 14:29
  • 1
    @Tarass - Thanks! I've upvoted your answer too in the meantime. :-) – Mico Mar 8 '18 at 15:38
2

Here's a solution that (a) uses three centered "X"-type columns and (b) uses \noindent before \begin{tabularx}... to assure that the tabularx environment occupies the full width of the textblock. I've also removed the ° and ^ column specifications as they don't seem to do anything. (However, I may be wrong...)

enter image description here

\documentclass{scrartcl}
%\usepackage{array} %% 'array' is loaded automatically by 'tabularx'
\usepackage{tabularx,booktabs}
\newcolumntype{C}{>{\centering\arraybackslash}X} % centered version of 'X' col. type

\begin{document}

\noindent
\begin{tabularx}{\linewidth}{@{} CCC @{}}
\toprule
a & b & c \\
d & e & f \\
\bottomrule
\end{tabularx}

\end{document}
|improve this answer|||||
  • Altough in my actual document I use a similar column than the one you defined, my question was about centering the table, not the columns content. Still, thanks for your example. – Marc Sharma Mar 8 '18 at 14:47
  • @MarcSharma - In the original version of your query, you specified the desired width of the tabular material to be \textwidth. That's the query my answer was designed to anwer. I've notice that you've changed the width specification drastically in the meantime. – Mico Mar 8 '18 at 15:12
  • @Mico Maybe consecutively to my answer. – Tarass Mar 8 '18 at 15:26

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.