# Formatting a table in a article

I'm new on latex and it is complicated to do some things. I'm using a documentclass article and I have the problem in the figure.

The table is too long and the last column doesn't appear, it is in the right side of the article. I used this code:

\begin{tabular}{lccr}
\toprule
\textit{Patient category} & \textit{Shapiro-Wilk Test} &  \textit{D’Agostino’s $K^2$ Test} & \textit{Anderson-Darling Test}\\
\midrule
\textbf{HC}        & 0.993 & 0.976 & 0.145\\
\textbf{PD ICD}    & 0.286 & 0.040 & 0.426\\
\textbf{PD No ICD} & 0.618 & 0.605 & 0.256\\
\bottomrule
\label{tab:t}
\end{tabular}


How can I solve it?

This is the final code (solved), but doesn't appear the caption. How want to add just it on the top of the table. Also the text after the table is not aligned with the text below the table. Finally, when I compile it return different warning and errors: undefined control sequence on \midskip... ; \caption outside float and Overfull hbox in \noindent

\noindent
\midskip\noindent\setlength\tabcolsep{3pt} % default: 6pt
\begin{tabularx}{\columnwidth}{@{}%
L{0.86} C{0.95}C{1.2}C{0.99}@{}} % Note: 0.86+0.95+1.2+0.99=4.0=# of X-type cols.
\toprule
\mycell{} & \multicolumn{3}{c@{}}{\textbf{Test}}\\
\cmidrule(l){2-4}
Patient Category & Shapiro-Wilk & D'Agostino's $K^2$ & Anderson-Darling\\
\midrule
HC        & 0.993 & 0.976 & 0.145\\
PD ICD    & 0.286 & 0.040 & 0.426\\
PD~No~ICD & 0.618 & 0.605 & 0.256\\
\bottomrule
\caption{Are shown the p-values for the different tests}
\label{table:1}
\end{tabularx}

exampleeeeasdasd


result:

What i want:

0.976
0.040**
0.605

• Welcome to TeX.SE. Please tell us more about your document setup. E.g., does it employ a two-column layout? How wide are the columns? Which font packages, if any, does the document load? What's the main font size?
– Mico
Apr 30 '20 at 21:41
• I use \documentclass[11pt,a4paper]{article}, so the article is divived in 2 columns
– Will
Apr 30 '20 at 21:48
• Regarding your recent edit: You can only use the \caption command inside of a table (or figure) environment. Thus, remove the \noindent, replace it with \begin{table} and add an \end{table} right after \end{tabularx}. And move the \caption command outside of tabularx but inside of table. May 1 '20 at 9:10
• Thanks @leandriis it works !!!
– Will
May 1 '20 at 9:14

Something like this? (You didn't provide much information, forcing me to make quite a few assumptions which may not be correct.) The proposed solution employs a tabularx environments and that line breaks occur in all four columns of the header row.

Observe that I've also removed the italics from the header row and the bold-facing from the header column -- it shouldn't be necessary to use such highlighting for a table that's this small and compact. A simple, clear layout should suffice to guide your readers' eyes.

\documentclass[twocolumn,11pt,a4paper]{article}
\usepackage{newtxtext,newtxmath,booktabs,sectsty}
\paragraphfont{\mdseries\itshape}
\usepackage{tabularx,ragged2e}
\newcolumntype{L}[1]{>{\RaggedRight\hsize=#1\hsize}X}
\newcolumntype{C}[1]{>{\Centering\hsize=#1\hsize\hspace{0pt}}X}
\newcommand\mycell[1]{\smash{%
\begin{tabular}[t]{@{}>{\RaggedRight}p{\hsize}@{}} #1 \end{tabular}}}
\begin{document}

\noindent
\paragraph{Anderson-Darling Test.} The results are summed up in the following table.

\smallskip\noindent\setlength\tabcolsep{3pt} % default: 6pt
\begin{tabularx}{\columnwidth}{@{}%
L{0.86} C{0.95}C{1.2}C{0.99}@{}} % Note: 0.86+0.95+1.2+0.99=4.0=# of X-type cols.
\toprule
\mycell{Patient category} & \multicolumn{3}{c@{}}{Test}\\
\cmidrule(l){2-4}
& Shapiro-Wilk & D'Agostino's $K^2$ & Anderson-Darling\\
\midrule
HC        & 0.993 & 0.976 & 0.145\\
PD ICD    & 0.286 & 0.040 & 0.426\\
PD~No~ICD & 0.618 & 0.605 & 0.256\\
\bottomrule
\label{tab:t}
\end{tabularx}
\end{document}

• @jsbibra - I've gone ahead and updated the screenshot. Gotta give your eyes some rest. :-)
– Mico
May 1 '20 at 3:13
• @Will - To move the string "Patient category" down to the line of the subheader cells, just (a) delete \mycell{Patient category} and (b) insert Patient category (note: no \mycell wrapper needed) immediately before & Shapiro-Wilk. To increase the vertical separation, just replace \smallskip with either \medskip or \bigskip.
– Mico
May 1 '20 at 8:35
• @Will - Is there something stopping you from encasing the tabular-related material (including the \setlength\tabcolsep{3pt} directive) in a table environment and inserting a suitable \caption statement? (My mind-reading skills are sadly -- but probably not surprisingly -- non-existent. The code you posted contained no table and \caption material, and the accompanying screenshot contained but the barest hint of such material. Hence I didn't try to fathom what you were trying to do in that regard.)
– Mico
May 1 '20 at 8:45
• @Will - (a) The macro's name is \medskip, not \midskip. (b) The \caption and \label statements must be placed outside the tabularx environment. (c) It appears that you want the tabular material right where it is. Hence, you should not use a table environment. Instead, load the caption package (in the preamble) via \usepackage{caption}, delete caption{Are shown the p-values for the different tests}, and insert \captionof{table}{$p$-values for various tests} before the tabularx env.
– Mico
May 1 '20 at 9:14
• @Will - In a two-column document, figure and table environments span a single column, whereas figure* and table* documents span both columns. You should use the latter if (and only if) you need to display graphics or tables that cannot fit in a single column.
– Mico
May 1 '20 at 17:03

Here is a slightly different approach using makecell and tabular*:

\documentclass[11pt,a4paper, twocolumn]{article}
\usepackage{lipsum} % for dummy text using \lispum. remove in actual document.
\usepackage{booktabs} % for horizontal lines with imptoved spacing
\usepackage{makecell} % for the \thead command
\usepackage{caption} % for improved spacing around captions
\begin{document}

\begin{table}
\caption{Are shown the p-values for the different tests}  \label{table:1}
\setlength\tabcolsep{0pt}
\begin{tabular*}{\columnwidth}{@{}@{\extracolsep{\fill}}l ccc@{}}
\toprule
& \thead{D'Agostino's\\ $K^2$ Test}
\midrule
HC        & 0.993 & 0.976 & 0.145\\
PD ICD    & 0.286 & 0.040 & 0.426\\
PD~No~ICD & 0.618 & 0.605 & 0.256\\
\bottomrule
\end{tabular*}
\end{table}

\lipsum[1]
\end{document}


Another variant, using tabular*. I also took the liberty to add the tabular inside a table-environment and add a caption:

\documentclass{article}
\usepackage{array, booktabs, caption}
\captionsetup{singlelinecheck=off, font=raggedright, font=it,labelfont=bf, skip=0.5\baselineskip}

\begin{document}

\begin{table}[!ht]
\caption{Result of three tests types\label{tab:t}}

\setlength{\tabcolsep}{0pt}

\begin{tabular*}{\linewidth}{@{\extracolsep{\fill}}>{\itshape}wl{0.225\linewidth}wc{0.225\linewidth}wc{0.225\linewidth}wr{0.225\linewidth}}
\toprule
\theadl{Patient\\category} & \theadc{Shapiro \\Wilk} &  \theadc{D’Agostino’s\\$K^2$} & \theadr{Anderson-\\Darling}\\
\midrule
HC              & 0.993 & 0.976 & 0.145\\
PD ICD       & 0.286 & 0.040 & 0.426\\
PD No ICD & 0.618 & 0.605 & 0.256\\
\bottomrule

\end{tabular*}
\end{table}
\end{document}