# I am trying to create a caption on a table, but got the error: captions outside float. I have written the code below,

\documentclass{article}
\usepackage[english]{babel}
\usepackage{CJK}
\usepackage[table]{xcolor}
\usepackage{caption}
\usepackage{booktabs}
\begin{document}

\begin{CJK}{UTF8}{song}
\begin{center}
\rowcolors{1}{green}{pink}
\caption{Timetable for on line class (F=法学；Y=营养)}

\begin{tabular}{lllll}

\caption{Timetable for on line class (F=法学；Y=营养)}
& Mon  & Wed & Thurs & Fri \\
8：00-9:40am   & 法学1903  &  &法学1904 & 法学1901\\
9:55-11:35am& 法学1902  &  &  & 营养1901  \\

\end{tabular}
\end{center}
\end{CJK}
\end{document}

• Either load the caption package and use capionof-command, or enclose your tabular in an table-environment. – Sveinung Feb 21 at 17:56
• \caption{...} cannot be inside tabular environment ... – Zarko Feb 21 at 18:00
• @Zarko I updated my answer with you comments, since it make the answer more useful. However, I have credited you :=) – Sveinung Feb 21 at 18:22
• Off-topic, but please note that the CJK package is an outdated way to deal with Chinese input. Please use XeLaTeX and change the first line to \documentclass[UTF8]{ctexart} instead. Then, you can write 中文 without using any special environments. – L. F. Feb 22 at 5:50
• You can put \caption inside a minipage or a p{} column, but you need to specify a width. – John Kormylo Feb 22 at 16:29

Since you already load the caption package, use the command \capionof{table}{The caption text}, or enclose your tabular in an table-environment.

And, as @Zarko write in his comment, \caption{...} cannot be inside the tabular-environment (neither can \captionof).

I have corrected the spacing issue in the second row (8:00). Typographically, you should use en-space (-- or –), not hyphen - to indicate the timespan:

\documentclass{article}
\usepackage[english]{babel}
\usepackage{CJK}
\usepackage[table]{xcolor}
\usepackage{caption}
\usepackage{booktabs}
\begin{document}

\begin{CJK}{UTF8}{song}
\begin{center}
\rowcolors{1}{green}{pink}
\captionof{table}{Timetable for on line class (F=法学；Y=营养)}

\begin{tabular}{lllll}

& Mon  & Wed & Thurs & Fri \\
8:00--9:40am   & 法学1903  &  &法学1904 & 法学1901\\ %% Corrected space
9:55--11:35am& 法学1902  &  &  & 营养1901  \\

\end{tabular}
\end{center}
\end{CJK}
\end{document}

• I would recommend deleting the second \captionof directive. – Mico Feb 21 at 18:05

This happens because, as the error message is telling you, there is a caption placed outside of a float environment. You could simply place the tabular environment inside of a table environment.

\documentclass{article}
\usepackage[english]{babel}
\usepackage{CJK}
\usepackage[table]{xcolor}
\usepackage{caption}
\usepackage{booktabs}
\begin{document}

\begin{CJK}{UTF8}{song}
\begin{table}
\centering
\rowcolors{1}{green}{pink}
\caption{Timetable for on line class (F=法学；Y=营养)}
\begin{tabular}{lllll}
& Mon  & Wed & Thurs & Fri \\
8：00-9:40am   & 法学1903  &  &法学1904 & 法学1901\\
9:55-11:35am& 法学1902  &  &  & 营养1901  \\
\end{tabular}
\end{table}
\end{CJK}
\end{document}