1

I have a threeparttable in which its cells are needed to be centered. I have tested all \centering and \begin{center}…\end{center} commands. But these commands do not work! Although there is no error, the texts in the cells are still left aligned. This is my table:

\documentclass{elsart5p}
\usepackage{subcaption}
\usepackage{booktabs}
\usepackage{threeparttable}
\begin{document}

\begin{threeparttable}
%\centering
\begin{center}
\caption{myTable}\label{tab:table1}
\setlength{\tabcolsep}{6pt}

\begin{tabular}[width=\linewidth]{p{0.6\linewidth}p{0.4\linewidth}}
\toprule
Header Header Header Header Header Header Header Header Header Header1&Header Header Header Header Header2\\
     \midrule 
col1 col1 col1 col1 col1 col1 col1 col1 col1 col1 col1 col1 col1 col1 col1 col1 col1 col1 col1 col1* & col2 col2 col2 \\
cell3 cell3& cell4 cell4 cell4 cell4 cell4\\
\bottomrule

\end{tabular}

\begin{tablenotes}
\item *Star: important
\end{tablenotes}

\end{center}
\end{threeparttable}


\end{document}

The texts to be written in some cells are too long, so I had to use \begin{tabular}[width=\linewidth]{p{0.6\linewidth}p{0.4\linewidth}} command to break the text of each cell into two or three lines.

How can I centerize this table?

  • Welcome to TeX.SE! The \begin{center}...\end{center} environment will centre the whole table, but not the cells whose alignment is specified in the options for \begin{tabular}. Specifically, you currently have two columns which are justified, given by p{0.6\linewidth}p{0.4\linewidth}. This answer will show you how to make these centered: https://tex.stackexchange.com/questions/5017/center-column-with-specifying-width-in-table-tabular-enviroment/5020#5020 – rbrignall Feb 11 '20 at 12:59
  • @rbrignall: Thank you for your welcome and your help. I tested the answer you guided, but it doesn't allow long cells to be broken automatically. The table gets larger than linewidth. – user206714 Feb 11 '20 at 13:25
  • @user206714 - The reason the overall width of the tabular environment exceeds \linewidth is that the total width of a cell is the sum of its usable width and the left-hand and right-hand padding (in the amount of \tabcolsep each). Hence, the width of your tabular environment is actually \linewidth+4\tabcolsep rather than just \linewidth. In the answer I just posted, I suggest you switch to a tabularx environment and specify (a) the intended overall width (here: \linewidth) and (b) the relative column widths (here: 3 to 2, or 1.2 to 0.8). – Mico Feb 11 '20 at 13:45

1 Answers1

1

Some comments and observations:

  • The tabular environment does not take an intended-width argument. The only arguments recognized by tabular affect the vertical positioning relative to neighboring elements: t, c (the default), and b. width=\linewidth is simply ignored.

  • You're probably thinking of the tabularx environment. By default, its X column type typesets the material fully-justified. To get a centered version of the X column type, define it via

    \newcolumntype{C}{>{\centering\arraybackslash}X}
    
  • If you want to make the first column 50% wider than the second, you should write

    \begin{tabularx}{\linewidth}{@{} >{\hsize=1.2\hsize}C >{\hsize=0.8\hsize}C @{}}
    

    Observe that 1.2=1.5*0.8 and also 1.2+0.8=2 -- the number of columns of type X.

  • The threeparttable environment should be embedded in a table environment. Use \tnote directives to place free-format footnote markers, and use \item statements within the tablenotes environment to start off the associated footnote text.


enter image description here

\documentclass{elsart5p}
\usepackage{caption,tabularx,booktabs}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\usepackage[flushleft]{threeparttable}
\begin{document}

\begin{table}
%% \setlength\tabcolsep{6pt} % that's the default!
\begin{threeparttable}

\caption{myTable}
\label{tab:table1}

\begin{tabularx}{\linewidth}{@{} >{\hsize=1.2\hsize}C >{\hsize=0.8\hsize}C @{}}
\toprule
Header Header Header Header Header Header Header Header Header Header1&
Header Header Header Header Header2\\
\midrule
col1 col1 col1 col1 col1 col1 col1 col1 col1 col1 col1 col1 col1 col1 col1 col1 col1 col1 col1 col1\tnote{*} &
col2 col2 col2 \\
cell3 cell3& 
cell4 cell4 cell4 cell4 cell4\\
\bottomrule
\end{tabularx}

\medskip
\begin{tablenotes}
\item[*]Star: important
\end{tablenotes}

\end{threeparttable}
\end{table}

\end{document} 
Mico
  • 506,678