1

I want to create a table using TeXworks. I could do it using tabular environment, but when I put caption above the table I get an error. The code is as follows. Can anyone please help me solve this problem?

 \begin{center}

    \begin{tabular}{l l l l l l}
        {\caption{Table 1: The coeffcient of determination ($R^2$) for validation set (dataset C) obtained by applying different preprocessing techniques.}}

        {\label{Table 1}}
        \hline

 Trans & Dry & Moisture-1 & Moisture-2 & Moisture-3 & Moisture-4\\

        \hline

Raw & 0.79 & -184.32 & -100.21 &    -53.74 & -13.02\\

MSC & 0.76 & -5.82 & -4.58 & -3.02 & -1.88\\

SNV & 0.75 & -423.19 & -188.19 & -69.23 & -7.95\\

FD & 0.71 & -6.12 & -4.13 & -3.26 & -2.19\\

        \hline

    \end{tabular}
\end(center)
  • 1
    Oh my, there are some issues here ;-) \caption needs a table environment or use \captionof{table}{...} and \begin{center}` requires \end{center}, not \end(center) (however, the center - environment shouldn't be inside a floating environment like table then. – user31729 Sep 6 '15 at 10:27
  • this might be as simple as moving the \caption and its \label outside the tabular environment. (but if it's not within a table -- floating -- environment, you need \captionof, still outside the tabular.) – barbara beeton Sep 6 '15 at 12:31
6

It is good, that the code in the question does not contain vertical lines. That makes the table more professional looking.

I do not want to comment on the remaining issues, you might want to get a good LaTeX book. The table can be set as follows:

\documentclass{article}
\usepackage{booktabs}% nicer horizontal lines
\usepackage{caption}% fix vertical spacing of table captions
\usepackage{siunitx}% align numbers at the decimal point

\begin{document}
\begin{table}
  \centering
  \caption{The coeffcient of determination ($R^2$) for
    validation set (dataset~C) obtained by applying different preprocessing
    techniques.}
  \label{Table 1}
  \begin{tabular}{
    l
    S[table-format=1.2]
    S[table-format=-3.2]
    S[table-format=-3.2]
    S[table-format=-2.2]
    S[table-format=-2.2]
  }
    \toprule
    Trans & {Dry}
    & {Moisture-1} & {Moisture-2} & {Moisture-3} & {Moisture-4}\\
    \midrule
    Raw & 0.79 & -184.32 & -100.21 &    -53.74 & -13.02\\
    MSC & 0.76 & -5.82 & -4.58 & -3.02 & -1.88\\
    SNV & 0.75 & -423.19 & -188.19 & -69.23 & -7.95\\
    FD & 0.71 & -6.12 & -4.13 & -3.26 & -2.19\\
    \bottomrule
  \end{tabular}
\end{table}
\end{document}

Result

2

the key problem here is that the \caption must be outside the tabular environment.

there are also some other problems:

  • \end(center) should have braces, not parentheses: \end{center}
  • when the \caption is outside the tabular, it is numbered automatically, so you end up with "Table 1: Table 1:"; remove that string from the caption text
  • it's possibly better to enclose this in a "floating" environment, i.e. \begin{table} ... \end{table}; otherwise you will need \captionof{...} instead of just \caption (and also \usepackage{captionof})
  • if you do use the table environment, use \centering instead of \begin{center} ... \end{center} for better vertical spacing
  • the negative values in the table have hyphens instead of minus signs; for real minus signs, you need to put these values into math mode, e.g. $184.32, and for consistency, all numeric values in the table should be treated the same
  • numeric values in a table, especially when all have the same number of decimal places, really look better flush right than flush left

other improvements are possible, but that's enough for now.

here's the result:

output of example code

\documentclass{article}
\begin{document}
\begin{table}
\centering

 \caption{The coeffcient of determination ($R^2$) for validation set (dataset C) obtained by applying different preprocessing techniques.}
        \label{Table 1}

    \begin{tabular}{l r r r r r}
        \hline

 Trans & Dry & Moisture-1 & Moisture-2 & Moisture-3 & Moisture-4\\

        \hline

Raw & $0.79$ & $-184.32$ & $-100.21$ & $   -53.74$ & $-13.02$\\

MSC & $0.76$ & $-5.82$ & $-4.58$ & $-3.02$ & $-1.88$\\

SNV & $0.75$ & $-423.19$ & $-188.19$ & $-69.23$ & $-7.95$\\

FD & $0.71$ & $-6.12$ & $-4.13$ & $-3.26$ & $-2.19$\\

        \hline

    \end{tabular}
\end{table}
\end{document}
  • I'd remove the braces around \caption – egreg Sep 6 '15 at 19:31
  • @egreg -- right you are. \label too. (now why did i put them there in the first place??? shakes head, confused.) – barbara beeton Sep 6 '15 at 19:38
  • They were in the original code. ;-) – egreg Sep 6 '15 at 19:41
1

It would be helpful if you would mention the error. However, one thing which is wrong is that with \end{center} you used normal braces and not curly braces. As such your centre environment is not properly ended. Furthermore, let me propose the following solution:

\begin{table}
\centering
\caption{The coeffcient of determination ($R^2$) for validation set (dataset C) obtained by applying different preprocessing techniques.}\label{Table 1}
\begin{tabular}{l l l l l l}

    \hline

Trans & Dry & Moisture-1 & Moisture-2 & Moisture-3 & Moisture-4\\

    \hline

Raw & 0.79 & -184.32 & -100.21 &    -53.74 & -13.02\\

MSC & 0.76 & -5.82 & -4.58 & -3.02 & -1.88\\

SNV & 0.75 & -423.19 & -188.19 & -69.23 & -7.95\\

FD & 0.71 & -6.12 & -4.13 & -3.26 & -2.19\\

    \hline

\end{tabular}
\end{table}

This way you don't have to explicitly state Table 1, centering is done properly, and the caption will also not give you an error.

  • Is it necessary to put \begin(Table) to write the caption; I mean that cannot I just write the caption into tabular environment itself. – Abhinav Gupta Sep 6 '15 at 10:40
  • It's most certainly not required. For example, you could use \captionof{table}{My caption text}. For that macro to work you need to import the caption package (\usepackage{caption} in your preamble). The macro \captionof needs to be placed above your tabular. However the table environment is more clear and conventional. Furthermore, the table is a float object, while the tabular on its own is not. As such you can't really position(/float) the tabular on its own (as you would normally with other tables, figures and float objects). – QStar Sep 6 '15 at 11:22
  • @user147798 \begin(Table) won't work. You're probably mistaking it for \begin{table} – clemens Sep 6 '15 at 13:03
0

I propose three solutions to improve the table:

A first solution does not use the table environment, and uses the \ captionof` command. The caption width is largert than the table width.

A second solution uses the threeparttable environment. Thus the caption width is excatly the table width. You can use the \caption command.

A third solution is based on the \ltablex package, which combines the possibilities of tabularx and longtable. You have a control on the table width, and the caption is included in the first head of the table, as in a lontable environment.

\documentclass{article}
\usepackage{booktabs}% nicer horizontal lines
\usepackage{caption}% fix vertical spacing of table captions
\usepackage{siunitx}% align numbers at the decimal point
\usepackage{threeparttable, ltablex}
\usepackage{showframe}

\begin{document}

\captionof{table}{The coefficient of determination ($R^2$) for
  validation set (dataset~C) obtained by applying different preprocessing
  techniques.}\vskip\abovecaptionskip
\label{Table 1a}
\begin{tabular}{
    l
    S[table-format=1.2]
    S[table-format=-3.2]
    S[table-format=-3.2]
    S[table-format=-2.2]
    S[table-format=-2.2]
    }
    \toprule
    Trans & {Dry}
    & {Moisture-1} & {Moisture-2} & {Moisture-3} & {Moisture-4}\\
    \midrule
    Raw & 0.79 & -184.32 & -100.21 & -53.74 & -13.02 \\
    MSC & 0.76 & -5.82 & -4.58 & -3.02 & -1.88 \\
    SNV & 0.75 & -423.19 & -188.19 & -69.23 & -7.95 \\
    FD & 0.71 & -6.12 & -4.13 & -3.26 & -2.19 \\
    \bottomrule
  \end{tabular}
  \vskip1cm

  \begin{threeparttable}

    \caption{The coefficient of determination ($R^2$) for
      validation set (dataset~C) obtained by applying different preprocessing
      techniques.}\vskip\abovecaptionskip
    \label{Table 1b}
    \begin{tabular}{
        l
        S[table-format=1.2]
        S[table-format=-3.2]
        S[table-format=-3.2]
        S[table-format=-2.2]
        S[table-format=-2.2]
        }
        \toprule
        Trans & {Dry}
        & {Moisture-1} & {Moisture-2} & {Moisture-3} & {Moisture-4}\\
        \midrule
        Raw & 0.79 & -184.32 & -100.21 & -53.74 & -13.02 \\
        MSC & 0.76 & -5.82 & -4.58 & -3.02 & -1.88 \\
        SNV & 0.75 & -423.19 & -188.19 & -69.23 & -7.95 \\
        FD & 0.71 & -6.12 & -4.13 & -3.26 & -2.19 \\
        \bottomrule
      \end{tabular}
    \end{threeparttable}
    \vskip1cm

    \keepXColumns
    \begin{tabularx}{0.85\linewidth}{
        X
        S[table-format=1.2]
        S[table-format=-3.2]
        S[table-format=-3.2]
        S[table-format=-2.2]
        S[table-format=-2.2]
        }
        \captionsetup{width=0.85\linewidth}
        \caption{The coefficient of determination ($R^2$) for
        validation set (dataset~C) obtained by applying different preprocessing
        techniques.}\label{Table 1c}
        \endhead
        \addlinespace[\abovecaptionskip]
        \toprule
        Trans & {Dry}
        & {Moisture-1} & {Moisture-2} & {Moisture-3} & {Moisture-4}\\
        \midrule
        Raw & 0.79 & -184.32 & -100.21 & -53.74 & -13.02 \\
        MSC & 0.76 & -5.82 & -4.58 & -3.02 & -1.88 \\
        SNV & 0.75 & -423.19 & -188.19 & -69.23 & -7.95 \\
        FD & 0.71 & -6.12 & -4.13 & -3.26 & -2.19 \\
        \bottomrule
      \end{tabularx}

\end{document} 

enter image description here

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.