[Tex/LaTex] tabular can’t caption is separated

captionstables

I'm creating a table like this:

\captionof{table}{caption}
\begin{tabular}{@{} *5l @{}}\toprule
\emph{header1} & \emph{header2} & \emph{header3} \\\midrule
item1 & v1  & v2   \\
item2 & v1  & v2   \\
item2 & v1  & v2   \\
\hline
\label{table:label}
\end{tabular}

This way the caption gets separated from the table and if it reaches the end of the page the table might go to the next page while its caption remains on the previous page. If I move the caption inside tabular I get an error:

You can't use `\hrule' here except with leaders.
\caption@hrule ->\hrule

and if I use \caption instead:

Package caption Error: \caption outside float

I've added these lines to use caption for my tables:

\usepackage[hypcap]{caption}
\captionsetup[table]{name=table}

Best Answer

Have you considered using the booktabs package?

This is your example a little re-arranged:

\documentclass{article}
\usepackage{booktabs}
\usepackage{caption}

\captionsetup[table]{position=top,labelfont={sc},textfont={sl}}

\renewcommand{\thetable}{\Roman {table}}


\begin{document}


\begin{table}
    \centering
    \caption{Table's caption}\label{tab:label}
    \begin{tabular}{lll}
        \toprule
        \emph{header1} & \emph{header2} & \emph{header3} \\
        \midrule
        item1 & v1  & v2   \\
        item2 & v1  & v2   \\
        item2 & v1  & v2   \\
        \bottomrule
    \end{tabular}
    \end{table}

\end{document}

enter image description here

Related Question