0

I'd like to label a table without putting it into a table environment. From this I know that this wont work, but the solution offered there does not work either, because, if I put my table (tabularx) inside a table environment, the table does not continue on the next page but gives an overfull vbox error... How do you solve such problems? Thanks for any help!

This is the script I use:

\documentclass{article}
\usepackage{booktabs, tabularx, array}  

\newcolumntype{s}[1]{>{\hsize=#1\hsize}X}

\begin{document}    

\begin{tabularx}{\linewidth}{
        @{}
        s{0.5}
        s{2}
        >{\raggedright\arraybackslash}s{0.5}
        @{}
    }
\label{tab:primer}
    \caption[blabla]{blablabla}\\

    \toprule
    \textbf{Number} & \textbf{Sequence (5' $\rightarrow$ 3')} & \textbf{Application}\\[6pt]
    \midrule
    \endfirsthead

    \toprule
    \textbf{Number} & \textbf{Sequence (5' $\rightarrow$ 3')} & \textbf{Application}\\[6pt]
    \midrule
    \endhead

    1 & 2 & 3\\
\end{tabularx}
\end{document}
Sveinung
  • 20,355
prog2de
  • 193
  • 6
  • (i) caption had not to be inside tabularx environment (ii) label had to be after caption, (iii) you can use captionof{figure}{...} for caption. For this you need to load the caption or the capt-of package. – Zarko Aug 26 '19 at 14:38
  • please extend your code fragment to an MWE (Minimal Working Example: a small self contained document) which we can test. Missed are information about used packages in your document. – Zarko Aug 26 '19 at 14:52

2 Answers2

3

There are two reasons why your code fragment does not compile, even after adding the necessary document preamble:

  1. You have a mix of longtable and tabularx commands in your the table's preamble. The heading is for a longtable, but the column definitions are for a tabularx.
  2. You have placed the label at wrong place. The best place is inside the caption, i.e. \caption[blabla]{blablabla\label{tab:primer}}\\.

If you are going to using tabularx, I suggest that you use a KOMA-script-class and use the internal commands to set caption outside float, instead of the caption-package.

If you use longtable, it has its own caption commands (and KOMA-script can blend in). However, you cannot use X-columns in a longtable without additional packages, for example threeparttablex or xltabular, as Zarko suggests. I will add that xltabular is a drop-in replacement for longtable and tabularx. You code will compile with three minor changes:

  1. Replace tabularx with xltabular in your document's preamble
  2. Move the \label inside the caption as described above. You should change \\ to \\[10pt] at the end to have some space after the caption
  3. Change \begin{tabularx} with \begin{xltabular} and \end{tabularx} with \end{xltabular}

Here is your example in a scrartcl-class. This table will not break at page boundary. If you need that function, @Zarko has already given you a workable solution:

enter image description here

\documentclass{scrartcl}
\usepackage{booktabs, array, tabularx}

\begin{document}

\captionaboveof{table}{blablabla%
     \label{tab:primer}%
     }

\newcolumntype{s}[1]{>{\hsize=#1\hsize}X}
\begin{tabularx}{\linewidth}{
        @{}
        s{0.5}
        s{2}
        >{\raggedright\arraybackslash}s{0.5}
        @{}
    }

    \toprule
    \textbf{Number} & \textbf{Sequence (5' $\rightarrow$ 3')} & \textbf{Application}\\[6pt]
    \midrule


    1 & 2 & 3\\
\end{tabularx}
\end{document}
Sveinung
  • 20,355
  • I was waiting on OP response, but you encourage me to publish my prepared answer :-). Anyway, OP's anyway, the document's preamble is still a mystery :-(. +1 for nice answer! – Zarko Aug 26 '19 at 15:39
2

I would use xltabular, a nice combination of the longtable and tabularx. It enable use of caption inside table environment:

\documentclass{article}
\usepackage[margin=25mm]{geometry}
\usepackage{ragged2e}
\usepackage{booktabs, makecell, xltabular}
\renewcommand\theadfont{\bfseries\small}
\newcolumntype{R}{>{\RaggedRight}X}
\newcolumntype{s}[1]{>{\hsize=#1\hsize}X}
\usepackage[skip=1ex]{caption}

\usepackage{lipsum}

\begin{document}
\begin{xltabular}{\linewidth}{@{}  c
                >{\hsize=1.6\hsize}R
                >{\hsize=0.4\hsize}R
                             @{} }
\caption[blabla]{blablabla}
\label{tab:primer}                  
\addtocounter{table}{-1}            \\
    \toprule
\thead{Number} 
    & \thead{Sequence ($5'\rightarrow 3'$)} 
        & \thead{Application}       \\ 
    \midrule
\endfirsthead
    \caption[]{blablabla (cont.)}   \\
\thead{Number}
    & \thead{Sequence ($5'\rightarrow 3'$)}
        & \thead{Application}       \\
    \midrule
    \addlinespace[-3pt]
\endhead
    \midrule[0.8pt]
\multicolumn{3}{r}{continue on the next page}     
\endfoot
    \bottomrule
\endlastfoot
 1  & 2 & 3                         \\
    \addlinespace
 2  & \lipsum[1-2]  &               \\
    \addlinespace
 3  & \lipsum[3]    &               \\
    \addlinespace
 4  & \lipsum[4]    &               \\
    \addlinespace
 5  & \lipsum[5]    &               \\
    \end{xltabular}
\end{document}

enter image description here

enter image description here

enter image description here

Zarko
  • 296,517