如何在長表頂部添加標題?

如何在長表頂部添加標題?

我想要一個像這樣的長桌:

\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

你應該把 after 放在\label後面\caption,這樣更有意義。

答案2

另一種方法:使用頭和第一個頭:

\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))

但問題是 pandas 以這種方式產生我的長表:

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

所以我嘗試將\labeland放在\caption之前\toprule,但它永遠不會編譯。所以我的解決方案是改變\toprule\hline然後你就會得到你的編譯,例如:

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

相關內容