如何在长表顶部添加标题?

如何在长表顶部添加标题?

我想要一个像这样的长桌:

\begin{center}
\begin{longtable}{|p{2cm}|p{3cm}|p{7cm}|p{3cm}|}
\caption{my caption}
\hline
1 & 2 & 3 & 4\\ 
\hline 
\hline
1 & 2 & 3 & 4\\
\hline
1 & 2 & 3 & 4\\
\hline
.
.
.
1 & 2 & 3 & 4\\
\hline

\label{variability_impl_mech}
\end{longtable}
\end{center}

但是当我尝试编译它时,出现此错误:!Misplaced \noalign

当我在最后一行 \hline 之后写标题时,它工作正常。我想将标题放在表格顶部,但出现了此错误。

答案1

\begin{longtable}{|p{2cm}|p{3cm}|p{7cm}|p{3cm}|}
\caption{my caption}\\    %%%%<===
\hline

并且您应该将其放在\label后面\caption,这样更有意义。

答案2

另一种方法:使用 head 和 first head:

\documentclass{article}
\usepackage{longtable}
\begin{document}

\begin{longtable}{|p{2cm}|p{3cm}|p{7cm}|p{3cm}|}
\caption{my caption}
\label{variability_impl_mech}
\endfirsthead
\endhead
\hline
1 & 2 & 3 & 4\\ 
%\hline 
\hline
1 & 2 & 3 & 4\\
\hline
1 & 2 & 3 & 4\\
\hline
.
.
.
1 & 2 & 3 & 4\\
\hline
\end{longtable}

\end{document}

答案3

我遇到了一个问题,因为我用 python 的 pandas 库来生成表格。用这种方法。

print(tabla_1.to_latex(index = False, longtable=True))

但问题是熊猫以这种方式生成我的长表:

\begin{longtable}{lrrr}
\toprule
                  Nombre &  No datos &  P. rango &  total\_isnull \\
\midrule

所以我尝试在 之前放置\label和,但它从未编译过。所以我的解决方案是更改为然后你就会有编译,例如:\caption\toprule\toprule\hline

\begin{longtable}{lrrr}
\label{YourLabel}
\Caption{YourCaption}
\hline
                  Nombre &  No datos &  P. rango &  total\_isnull \\
\midrule

相关内容