2

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}

1 Answer 1

2

In fact, the extra spacing for the caption in the table environment is caused by the use of the center environment. In general this is discouraged because of this additional space (both above and below the table). Instead the use of the \centering command is recommended, but then the caption in the first table is also too close to the table. But see below how to solve this.

As you are using the caption package, you can use

\captionsetup[longtable]{position=bottom,skip=20pt}
\captionsetup[table]{position=bottom,skip=20pt}

Or whatever amount you prefer.

2
  • Inside a longtable the caption package utilizes both, first the table and afterwards the longtable options. So \captionsetup[table]{...} is sufficient in this case.
    – user2574
    Nov 21, 2016 at 16:30
  • OK, I didn't know. I suppose they don't add up :) Nov 21, 2016 at 16:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .