ロングテーブルの上にキャプションを表示するにはどうすればいいですか?

ロングテーブルの上にキャプションを表示するにはどうすればいいですか?

次のような長いテーブルが欲しいです:

\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

関連情報