将 \captionof 后的垂直间距修复为自定义标题

将 \captionof 后的垂直间距修复为自定义标题

我试图在两列代码后添加一个标题。对于两列,parcolumns这是我发现的唯一允许在中间分页的方法,也是我目前最喜欢的方法。使用 minipages 也是可行的,而且看起来要好看得多,除非它在无法跨多页流动时强制使用难看的空白。

无论如何,我的主要问题是 captionof 之后的垂直空间,我试图将其用作两个都列。下面是发生这种情况的一个例子。如何才能恢复正常间距/边距\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}

输出:

在此处输入图片描述

(我没有在我的列表上使用框架,所以我希望渲染错误不是问题)

答案1

这是一个更好的方法。由于listingscaption包都对 s 的标题起作用lstlisting,因此最好通过添加来消除列表和其后的文本之间引入的listings间距:belowskip=0pt\lstset

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

然后我们通过以下方式设置字幕属性\captionsetup

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

这样,字幕前后的间距就相同了。

完整 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} 

输出:

在此处输入图片描述

相关内容