2

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.
enter image description here

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

I add:

result:

enter image description here

What i want:

0.976
0.040**
0.605    
Will
  • 193
  • 10
  • 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
  • 1
    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. – leandriis May 01 '20 at 09:10
  • Thanks @leandriis it works !!! – Will May 01 '20 at 09:14

3 Answers3

3

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.

enter image description here

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}
Mico
  • 506,678
  • is there a vertical at 0,286 – js bibra May 01 '20 at 03:08
  • @jsbibra - I guess there is. Is it distracting? – Mico May 01 '20 at 03:09
  • nope thought my eyes were giving way – js bibra May 01 '20 at 03:09
  • 1
    @jsbibra - I've gone ahead and updated the screenshot. Gotta give your eyes some rest. :-) – Mico May 01 '20 at 03:13
  • Thanks, it works. Is there a way to have Patient category aligned with the name of the tests ? So more down. And also how can i create a empty space between the table and the text ? (not the description, but the paragraph above) – Will May 01 '20 at 08:13
  • 1
    @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 01 '20 at 08:35
  • Perfect thanks. How can i add the caption ? because the first part is the paragraph. I want to add something like: \caption{Are shown the p-values for the different tests.}. But this doen't appear in the pdf – Will May 01 '20 at 08:39
  • 1
    @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 01 '20 at 08:45
  • I edited the question – Will May 01 '20 at 08:57
  • 2
    @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 01 '20 at 09:14
  • @Mico last things. I added this line: \multicolumn{4}{l}{\footnotesize * p<0.10, ** p<0.05, *** p<0.01}\ after the \bottomrule. If a put & **0.040 (center of the table) it does not aligned with the others. How can i solve it ? – Will May 01 '20 at 10:04
  • @Will - Please clarify what If a put & **0.040 (center of the table) it does not aligned with the others might mean. – Mico May 01 '20 at 10:38
  • @Mico I edited again the question – Will May 01 '20 at 10:46
  • @Will - AFAICT, \multicolumn{4}{l}{\footnotesize * p<0.10, ** p<0.05, *** p<0.01} is completely separate and independent from the other parts. Is this correct? Second, you're not writing **0.040 but 0.040**, right? Finally, are you asking how to align numbers on their decimal markers instead of simply centering them in a column? – Mico May 01 '20 at 11:02
  • How can i center in a column the number ? – Will May 01 '20 at 12:22
  • @Will - the entries in the three data columns are centered in my answer. – Mico May 01 '20 at 12:26
  • @Mico - Yes, infact when i put 0.040, it center all the value with the . So it is not aligned respect to the first decimal. – Will May 01 '20 at 14:14
  • @Will -3 hours ago, I asked you whether the numbers should be centered or aligned on the decimal markers. To this, you replied 2 hours ago with the question "How can I center ... the number?" But now your most recent comment seems to want alignment on the decimal markers. Are we really decided that we want to align the numbers on their decimal markers? Please advise. – Mico May 01 '20 at 14:52
  • @Mico Yes, I want to align the numbers on their decimal markers – Will May 01 '20 at 15:37
  • @Mico - Ok thanks !!! If you have a good pdf or other things of latex please send me. Final things. Do you know what is the * in \begin{figure*} ?? What is the difference from \begin{figure} – Will May 01 '20 at 16:56
  • 1
    @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 01 '20 at 17:03
  • @Mico I have only one problem. If I put an image and it is doesn't not enter In the page; latex put the text, the title of the table (table 1: example), the image and then the table!!! So the caption is not in the top of the table. They are separated, why ? – Will May 05 '20 at 11:59
  • @Will - I'm afraid you haven't provided nearly enough information to allow a confident diagnosis, let alone a workable solution. For example, what does "if I put an image" entail? Is its width single-column or double-column? Please post a *new" query. Doing so will assure that it will be seen by many more people than just me. – Mico May 05 '20 at 12:06
  • @Mico I can't modify the question again, how can I contact you in private ? – Will May 05 '20 at 12:16
  • @Will - No, please post a *new query". – Mico May 05 '20 at 12:31
  • @Mico I made a new question: https://tex.stackexchange.com/q/542400/214295 – Will May 05 '20 at 13:23
  • @Mico can you help me with this: https://tex.stackexchange.com/q/542676/214295 thanks :) – Will May 07 '20 at 10:09
0

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

enter image description here

\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[l]{Patient\\ Category} 
      & \thead{Shapiro-\\Wilk Test} 
        & \thead{D'Agostino's\\ $K^2$ Test} 
          & \thead{Anderson-\\Darling 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}
leandriis
  • 62,593
0

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

enter image description here

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

\newcommand{\theadl}[1]{\multicolumn{1}{l}{\small\itshape\begin{tabular}{l} #1\end{tabular}}}
\newcommand{\theadc}[1]{\multicolumn{1}{c}{\small\itshape\begin{tabular}{c} #1\end{tabular}}}
\newcommand{\theadr}[1]{\multicolumn{1}{r}{\small\itshape\begin{tabular}{r}#1\end{tabular}}}

\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}
Sveinung
  • 20,355