标题插入问题

标题插入问题

我正在使用以下代码创建表格。我想为其添加标题,但当我使用该\caption命令时,它会发送以下错误消息:

\caption 位于浮动之外。 \caption

代码如下:

\documentclass{article}
\usepackage{enumitem,array}

\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash}p{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash}p{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash}p{#1}}
\begin{document}

\setlist[itemize]{leftmargin = *, itemsep=-3pt, before=\vspace*{-\dimexpr\baselineskip+\partopsep}, after=\vspace*{\dimexpr-\baselineskip+\partopsep}}

\begin{tabular}{| L{3cm} | L{3cm} | C{3cm} | R{3cm} |}
\hline
foo
&
\begin{itemize}
\item A cell with text that wraps around 
\item is raggedright and allows manual line breaks 
\end{itemize}
&
\begin{itemize}
\item A cell with text that wraps around 
\end{itemize} 
&
A cell with text that wraps around, is raggedleft and allows \newline
manual line breaks \\ \hline   
\end{tabular}

\caption{Table1 to test captions and labels}

\end{document}

如果有人能帮我解决这个问题,我将不胜感激。提前谢谢您。

答案1

如果你只想要一张带标题的表格就在此时在您的文本中,使用包captioncapt-of和命令\captionof{table}{...}代替\caption

在此处输入图片描述

(请注意,您的tabular对于当前的来说太宽\textwidth,因此表格不在中心。)

\documentclass{article}
\usepackage{enumitem,array}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash}p{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash}p{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash}p{#1}}
\usepackage{caption}% <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
\begin{document}

\setlist[itemize]{leftmargin = *, itemsep=-3pt, before=\vspace*{-\dimexpr\baselineskip+\partopsep}, after=\vspace*{\dimexpr-\baselineskip+\partopsep}}

\begin{tabular}{| L{3cm} | L{3cm} | C{3cm} | R{3cm} |}
\hline
foo
&
\begin{itemize}
\item A cell with text that wraps around 
\item is raggedright and allows manual line breaks 
\end{itemize}
&
\begin{itemize}
\item A cell with text that wraps around 
\end{itemize} 
&
A cell with text that wraps around, is raggedleft and allows \newline
manual line breaks \\ \hline   
\end{tabular}

\captionof{table}{Table1 to test captions and labels}% <<<<<<<<<<<<<<<<<

\end{document}

如果您想让表格自由浮动,以便更好地进行分页,只需将您的tabular和包装\captiontable环境中即可。(无需额外的包。)

\documentclass{article}
\usepackage{enumitem,array}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash}p{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash}p{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash}p{#1}}
\begin{document}

\setlist[itemize]{leftmargin = *, itemsep=-3pt, before=\vspace*{-\dimexpr\baselineskip+\partopsep}, after=\vspace*{\dimexpr-\baselineskip+\partopsep}}

\begin{table}% <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
\begin{tabular}{| L{3cm} | L{3cm} | C{3cm} | R{3cm} |}
\hline
foo
&
\begin{itemize}
\item A cell with text that wraps around 
\item is raggedright and allows manual line breaks 
\end{itemize}
&
\begin{itemize}
\item A cell with text that wraps around 
\end{itemize} 
&
A cell with text that wraps around, is raggedleft and allows \newline
manual line breaks \\ \hline   
\end{tabular}
\caption{Table1 to test captions and labels}
\end{table}% <<<<<<<<<<<<<<<<<<<<<<<<
\end{document}

相关内容