# Fixing vertical spacing after \captionof as a custom caption

I'm trying to get a single caption after two columns of code. For two columns, parcolumns is the only method I've found that allows a page break in the middle and is my current favorite. Using minipages is also possible and looks a lot nicer until it forces ugly whitespace when it can't flow across multiple pages.

Anyway, my main problem is the vertical space after captionof, which I'm attempting to use as a caption of both columns. Below is an example where this happens. How can I restore a normal spacing/margin after \captionof?

\documentclass{article}
\usepackage{parcolumns}
\usepackage{listings}
\usepackage{caption}

\lstset{
frame=lrtb,
captionpos=b,
}

\begin{document}

An initial paragraph.

\begin{lstlisting}[caption={Insertion sort using swaps}]
for i ← 1 to length(A)
j ← i
while j > 0 and A[j-1] > A[j]
swap A[j] and A[j-1]
j ← j - 1
\end{lstlisting}
A paragraph after lstlistings with normal indentation and vertical spacing.

\begingroup
%\noindent %this would cause extra vertical space before the parcolumns
\begin{parcolumns}[colwidths={1=.48\linewidth}]{2} %colwidths still leaves too much whitespace between columns
\footnotesize

\colchunk[1]{
\begin{lstlisting}
for i ← 1 to length(A)
j ← i
while j > 0 and A[j-1] > A[j]
swap A[j] and A[j-1]
j ← j - 1
\end{lstlisting}
}

\colchunk[2]{
\begin{lstlisting}
for i ← 1 to length(A)
x ← A[i]
j ← i
while j > 0 and A[j-1] > x
A[j] ← A[j-1]
j ← j - 1
A[j] ← x
\end{lstlisting}
}

\end{parcolumns}
\captionof{lstlisting}{Different insertion sorts in parcolumns.}
\endgroup

A paragraph after the caption for the parcolumns environment.
This paragraph is indented due to begingroup/endgroup but still has no vertical space/margin between
it and the caption.

\begingroup
\noindent
\begin{minipage}[t]{.48\textwidth} %t aligns top
\footnotesize
\begin{lstlisting}
for i ← 1 to length(A)
j ← i
while j > 0 and A[j-1] > A[j]
swap A[j] and A[j-1]
j ← j - 1
\end{lstlisting}
\end{minipage}
\hfill
\begin{minipage}[t]{.48\textwidth}
\footnotesize
\begin{lstlisting}
for i ← 1 to length(A)
x ← A[i]
j ← i
while j > 0 and A[j-1] > x
A[j] ← A[j-1]
j ← j - 1
A[j] ← x
\end{lstlisting}
\end{minipage}
\captionof{lstlisting}{Different insertion sorts in minipages.}
\endgroup

A paragraph after the caption for two minipages.
This paragraph is indented due to noindent and begingroup/endgroup but still has no vertical space/margin between
it and the caption.

\end{document}


Output:

(I'm not using frames on my lstlistings so the rendering bug isn't a problem, I hope)

-

This is a better method. Since both listings and caption packages act on lstlistings' captions, it is better to nullify the spacing introduced by listings between the listing and the text after it, by adding belowskip=0pt in the \lstset:

\lstset{
frame=lrtb,
captionpos=b,
belowskip=0pt
}


Then we set captions properties through \captionsetup

\captionsetup[lstlisting]{aboveskip=5pt,belowskip=\baselineskip}


In this way you have the same spacing either before and after the captions.

Full MWE:

\documentclass{article}
\usepackage{parcolumns}
\usepackage{listings}
\usepackage{caption}

\lstset{
frame=lrtb,
captionpos=b,
belowskip=0pt
}

\captionsetup[lstlisting]{aboveskip=5pt,belowskip=\baselineskip}

\begin{document}

An initial paragraph.

\begin{lstlisting}[caption={Insertion sort using swaps}]
for i ← 1 to length(A)
j ← i
while j > 0 and A[j-1] > A[j]
swap A[j] and A[j-1]
j ← j - 1
\end{lstlisting}
A paragraph after lstlistings with normal indentation and vertical spacing.

\begingroup
%\noindent %this would cause extra vertical space before the parcolumns
\begin{parcolumns}[colwidths={1=.48\linewidth}]{2} %colwidths still leaves too much whitespace between columns
\footnotesize

\colchunk[1]{
\begin{lstlisting}
for i ← 1 to length(A)
j ← i
while j > 0 and A[j-1] > A[j]
swap A[j] and A[j-1]
j ← j - 1
\end{lstlisting}
}

\colchunk[2]{
\begin{lstlisting}
for i ← 1 to length(A)
x ← A[i]
j ← i
while j > 0 and A[j-1] > x
A[j] ← A[j-1]
j ← j - 1
A[j] ← x
\end{lstlisting}
}

\end{parcolumns}
\captionof{lstlisting}{Different insertion sorts in parcolumns.}
\endgroup

A paragraph after the caption for the parcolumns environment.
This paragraph is indented due to begingroup/endgroup but still has no vertical space/margin between
it and the caption.

\begingroup
\noindent
\begin{minipage}[t]{.48\textwidth} %t aligns top
\footnotesize
\begin{lstlisting}
for i ← 1 to length(A)
j ← i
while j > 0 and A[j-1] > A[j]
swap A[j] and A[j-1]
j ← j - 1
\end{lstlisting}
\end{minipage}
\hfill
\begin{minipage}[t]{.48\textwidth}
\footnotesize
\begin{lstlisting}
for i ← 1 to length(A)
x ← A[i]
j ← i
while j > 0 and A[j-1] > x
A[j] ← A[j-1]
j ← j - 1
A[j] ← x
\end{lstlisting}
\end{minipage}
\captionof{lstlisting}{Different insertion sorts in minipages.}
\endgroup

A paragraph after the caption for two minipages.
This paragraph is indented due to noindent and begingroup/endgroup but still has no vertical space/margin between
it and the caption.

\end{document}


Output:

-