I want to read a csv file and create a multi-page table using \longtable
.
I need to place the caption after the table. When the table is small and fits in one page, it works (the first table in the MWE). However, when I have a multi-page table, the caption is placed very close to the table (the second example). When I use \tabularnewline
or \\[0.2in]
to add some space between the table and the caption, 2 out of 3 vertical lines in the table increase their size and "exit" from the table.
When I load the caption
package and set belowskip=20pt
, I have correct caption location for the multi-page table, but the short table's caption becomes too far from the table. I need to have both tables with correct caption location in the document.
I also tried the etoolbox
package with \AtBeginEnvironment{longtable}{\linespread{1}\selectfont}
as mentioned here, but did not get the desired output.
How can I fix the multi-page table and have more space between the table and the caption without affecting the short table's caption?
Here are short and long table data files, and my MWE:
\documentclass[11pt,letterpaper,times]{article}
\usepackage{longtable}
\usepackage{pgfplots, pgfplotstable}
\usepackage{booktabs}
\usepackage{caption}
%\captionsetup[table]{belowskip=20pt, position=top}
\usepackage{etoolbox}
\AtBeginEnvironment{longtable}{\linespread{1}\selectfont}
\begin{document}
\pgfplotstableset{
begin table=\begin{longtable},
end table=\end{longtable},
}
%% Correct table
\paragraph{Table \ref{tab:correct} is correct.} There is some space between the table and the caption.
\begin{table}[h!]
\begin{center}
\footnotesize
\pgfplotstabletypeset[
col sep=comma,
every head row/.style={
before row={
\hline & \multicolumn{3}{c|}{\bf 3 Cols}\\
},
after row=\hline,
},
every last row/.style={
after row=\hline,
},
columns/col1/.style={
column name={\bf Col1},
column type={|l|},
string type,
},
columns/col2/.style={
column name={\bf Col2},
column type={c},
string type,
},
columns/col3/.style={
column name={\bf Col3},
column type={c},
string type,
},
columns/col4/.style={
column name={\bf Col4},
column type={c|},
string type,
},
]{data-short.csv}
\end{center}
\caption{This table has ``correct'' caption location.}
\label{tab:correct}
\end{table}
%% Incorrect
\paragraph{Table \ref{tab:incorrect} is Incorrect.} The caption is attached to the table. There should be some space between the table and the caption as in Table \ref{tab:correct}.
%\begin{table}[h!]
\begin{center}
\footnotesize
\pgfplotstabletypeset[
col sep=comma,
every head row/.style={
before row={
\hline & \multicolumn{3}{c|}{\bf 3 Cols}\\
},
after row=\hline,
},
every last row/.style={
after row={
\hline
%\tabularnewline
%\\[0.2in]
\caption{\normalsize \mbox{This table has ``incorrect'' caption location. It is attached to the table.}}
\label{tab:incorrect}
},
},
columns/col1/.style={
column name={\bf Col1},
column type={|l|},
string type,
},
columns/col2/.style={
column name={\bf Col2},
column type={l},
string type,
},
columns/col3/.style={
column name={\bf Col3},
column type={c},
string type,
},
columns/col4/.style={
column name={\bf Col4},
column type={c|},
string type,
},
]{data-long.csv}
\end{center}
%\end{table}
\end{document}