¿Cómo colocar `\caption{}` delante de `\begin{longtable}`?

¿Cómo colocar `\caption{}` delante de `\begin{longtable}`?

¿Hay alguna forma de colocarlo \caption{}delante \begin{longtable}? Ilustrando con un ejemplo, en lugar de

\documentclass{article}
\usepackage{longtable}
\begin{document}
\begin{longtable}{c|c|c|c}
\caption{This is a very nice table}
A & B & A & B \\ \hline
\end{longtable}
\end{document}

Me gustaría usar algo como

\documentclass{article}
\usepackage{longtable}
\begin{document}
\caption{This is a very nice table}
\begin{longtable}{c|c|c|c}
A & B & A & B \\ \hline
\end{longtable}
\end{document}

Respuesta1

Puedes hacerlo con \captionof. Tenga en cuenta que aquí es posible realizar saltos de página entre el título y la tabla larga.

\documentclass{article}
\usepackage{longtable}
\usepackage{caption}


\begin{document}

\captionof{table}{This is a very nice table}
\begin{longtable}{c|c|c|c}
A & B & A & B \\ \hline
\end{longtable}
\addtocounter{table}{-1}

\captionof{table}{This is a very nice table}
\begin{longtable}{c|c|c|c}
A & B & A & B \\ \hline
\end{longtable}

\end{document}

ingrese la descripción de la imagen aquí

información relacionada