# Table content alignment to left/right using tabularx

I had tried several methods, but still unable to get the correct alignment. I want the first column to left hand side, then others toward right hand side.

\documentclass{article}
\usepackage{booktabs}
\usepackage[flushleft]{threeparttable}
\renewcommand{\TPTtagStyle}{\itshape} % optional
\usepackage{tabularx, ragged2e}
\usepackage{threeparttable}
\begin{document}

\begin{table}[!ht]
\centering

\begin{tablenotes}
\centering
\small
\item Dependent Variable: Testing1234
\end{tablenotes}
\begin{threeparttable}
%\newcolumntype{Y}{>{\RaggedRight\arraybackslash\hsize=2\hsize\linewidth=\hsize}X}
%\newcolumntype{W}{>{\RaggedLeft\arraybackslash\hsize=0.75\hsize\linewidth=\hsize}X}
%\begin{tabularx}{\textwidth}{@{}Y*{3}{W}@{}}
\begin{tabularx}{\textwidth}{l>{\raggedright\arraybackslash}Xlllll}
\toprule
\small {\textbf{Source}}
& \small {\textbf{Type III Sum of Squares}}
& \small {\textbf{df}}
& \small {\textbf{Mean Square}}
& \small {\textbf{F}}
& \small {\textbf{Sig.}}\\
\midrule
Corrected Model & 14573.300\tnote{a} & 3 & 4857.767 & 40.297 & .000 \\
Testing1234 & 214036.900 & 1 & 214036.900 & 1775.503 & .000\\
Testing1234 & .100 & 1 & .100 & .001 & .977\\
Testing1234Testing1234 & 14137.600 & 1 & 14137.600 & 117.276 & .000\\
Testing1234Testing1234Testing1234 & 435.600 & 1 & 435.600 & 3.613 & .065 \\
\bottomrule
\end{tabularx}
\footnotesize
\begin{tablenotes}
\item[a]Testing1234Testing1234Testing1234 % the footnote itself
\end{tablenotes}

\end{threeparttable}
\caption{Testing1234
\label{tab:test123} }
\end{table}

\end{document}


\newcolumntype{Y}{>{\RaggedRight\arraybackslash\hsize=2\hsize\linewidth=\hsize}X}
\newcolumntype{W}{>{\RaggedLeft\arraybackslash\hsize=0.75\hsize\linewidth=\hsize}X}
\begin{tabularx}{\textwidth}{@{}Y*{3}{W}@{}}


not working

\begin{tabularx}{\textwidth}{l>{\raggedright\arraybackslash}Xlllll}


not working too

\begin{tabularx}{\textwidth}{l>{\raggedright\arraybackslash}Xrrrrr}


Getting more confused.

• Since you have an l column, as first column, and a very long word, this it is set on one line giving a very wide column, which overwrite the other cells. You have to set first column as an p-column, i.e. X or something similar. Then you must allow hyphenation by using \RaggedRight and babel and set explicit hyphenation points using \-. The word Testing1234Testing1234Testing1234 is unknown for LaTeX and babel , so they do not know how to hyphenate it. – Sveinung Aug 8 '19 at 21:06

With \begin{tabularx}{\textwidth}{>{\raggedright\arraybackslash}Xrrrrr} you can get the desired result. I have also used the \thead command to replace repeated \small and \textbf commands and inserted manual linebreaks in the column headers of the second and fourth column:

\documentclass{article}
\usepackage{booktabs}
\usepackage[flushleft]{threeparttable}
\renewcommand{\TPTtagStyle}{\itshape} % optional
\usepackage{tabularx, ragged2e}

\usepackage{makecell}
\begin{document}

\begin{table}[!ht]
\centering
\small
Dependent Variable: Testing1234
\begin{threeparttable}
\begin{tabularx}{\textwidth}{>{\raggedright\arraybackslash}Xrrrrr}
\toprule
& \thead{Type III\\ Sum of\\ Squares}
\midrule
Corrected Model & 14573.300\tnote{a} & 3 & 4857.767 & 40.297 & .000 \\
Testing1234 & 214036.900 & 1 & 214036.900 & 1775.503 & .000\\
Testing1234 & .100 & 1 & .100 & .001 & .977\\
Testing 1234Tes ting1234 & 14137.600 & 1 & 14137.600 & 117.276 & .000\\
Testing 1234Test ing12 34Te ting1234 & 435.600 & 1 & 435.600 & 3.613 & .065 \\
\bottomrule
\end{tabularx}
\footnotesize
\begin{tablenotes}
\item[a]Testing1234Testing1234Testing1234 % the footnote itself
\end{tablenotes}
\end{threeparttable}
\caption{Testing1234\label{tab:test123}}
\end{table}

\end{document}

• thanks. I am very confused. \begin{tabularx}{widthi}{preamblei} What is the {\raggedright\arraybackslash}Xrrrrr} and {1\raggedright\arraybackslash}Xrrrrr}in \begin{tabularx}{\textwidth}{>{\raggedright\arraybackslash}Xrrrrr} means? Especially the > sign. raggedright means Left justified – aan Aug 8 '19 at 21:04
• @aan: An X type column (column type of the first column) is exactly as wide as it is needed to make the whole table as wide as the spwcified width (textwidth in your case). The contents of the X type column are usually justifiied. With small amounts of text and narrow X type columns this will lead to quite large blanks spaces between words. Therefore, and because you requested left alignment in your question, I have used >{\raggedright\arraybackslash}X in order to make the contents of the X type column left aligned. – leandriis Aug 8 '19 at 21:07
• To sum it up: >{.....} basically contains the code that is added before the contents of every cell in the respective row. You can use this to alter the horizontal alignment or print all contents of a column in bold,... – leandriis Aug 8 '19 at 21:09
• thanks. where did I define textwidth? I cannot find this information? In 'main.tex? – aan Aug 8 '19 at 21:13
• @aan: Please see if \thead[r]{Source} gives you the desired result. If this does not what you wanted to achieve, please clarify the desired output. – leandriis Aug 16 '19 at 20:53

You can use also tabulary instead of tabularx because this environment have L,C, R, and J columns.

But instead of R column for numbers, why not the S column of siunitx?

\documentclass{article}
\usepackage{booktabs}
\usepackage{tabulary,siunitx,lipsum}
\begin{document}
\lipsum[1][1-4]
\begin{table}[h]
\begin{tabulary}{\textwidth}{@{}LS[table-format=6.3]
cS[table-format=6.3]S[table-format=4.3]c@{}}\toprule
& {Type III Sum} &       & {Mean}   &        &          \\
Source    & {of Squares}   & {df}  & {Square} & {F}    & {Sig.}   \\\midrule
Corrected Model & 14573.300$^a$ & 3 & 4857.767 & 40.297 & .000 \\
Testing1234 & 214036.900 & 1 & 214036.900 & 1775.503 & .000\\
Testing1234 & .100 & 1 & .100 & .001 & .977\\
Testing1234Testing1234 & 14137.600 & 1 & 14137.600 & 117.276 & .000\\
Testing1234Testing1234\-Testing1234 & 435.600 & 1 & 435.600 & 3.613 & .065 \\
\bottomrule
\end{tabulary}
$^a$ \footnotesize Testing1234Testing1234Testing1234
\end{table}
\lipsum[2][1-4]
\end{document}


tabularx is to table of a fixed width, with at least one X column of equal width (but a, ta take the make unequal columns with any justito adapt to right-justified text (R columns) Lef width to adapt to the cell contest try to make columns with unequal withat try to take the best width

• thanks. Actually I am very confused when to use tabular and tabularx. Could you give a simple explanation on this? The Package tabularx - CTAN documentation (ctan.org/pkg/tabularx?lang=en) is very hard to understand and follow. – aan Aug 9 '19 at 2:12
• @aan (1) tabularx are tables of fixed width, where each X column is expanded equally, and wrapped as needed. (2) tabulary is to make tables of a maximum (not fixed) width, where text is wrapped as needed at different widths in L,R,C and J columns. (2) siunitx allow S columns centered at the decimal points (4) tabular is the basic LaTeX table that cannot wrap the text in l,r and c columns and so you cannot fix the table width, except using p columns were you set the width manually (e.g. p{3cm}`, but not the whole width, that is set by the cell contents. – Fran Aug 9 '19 at 15:24