列表:增加標題(頂部)和列表之間的內部邊距?

列表:增加標題(頂部)和列表之間的內部邊距?

下面的 MWE(改編自這裡),如何增加清單中的內部邊距,而不是像這樣:

在此輸入影像描述

它看起來像這樣(注意灰色背景已經被“拉伸”了一點):

在此輸入影像描述

我試過擺弄framextopmarginbelowcaptionskip但它似乎不適用於內部的利潤。

\documentclass{article}
\usepackage[svgnames]{xcolor} 
\usepackage{caption}
\usepackage{listings}
\usepackage{calc} 
\lstdefinestyle{outline}{
         basicstyle=\scriptsize\ttfamily,
         numberstyle=\tiny,
         numbersep=5pt,
         tabsize=2,
         extendedchars=true,
         breaklines=true,
         keywordstyle=\color{blue},
         frame=b,
         stringstyle=\color{green!40!black}\ttfamily,
         showspaces=false,
         showtabs=false,
         numbers=left,
         xleftmargin=17pt,
         framexleftmargin=17pt,
         showstringspaces=false,
         backgroundcolor=\color[RGB]{200,200,200},
         belowcaptionskip=-1pt
}

\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox[RGB]{60,100,180}{\parbox{\textwidth - 2 \fboxsep}{\hspace{14pt}#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white, singlelinecheck=false, margin=0pt, font={bf,footnotesize}}

\begin{document}
\begin{lstlisting}[style=outline,caption=Test]
First line.
Second line.
\end{lstlisting}
\end{document}

答案1

看起來頂部需要有一條框架線才能framextopmargin產生影響。除了標題和框架本身之間有一條細黑線之外,這正是您所要求的:

\documentclass{article}
\usepackage[svgnames]{xcolor} 
\usepackage{caption}
\usepackage{listings}
\usepackage{calc} 
\lstdefinestyle{outline}{
         basicstyle=\scriptsize\ttfamily,
         numberstyle=\tiny,
         numbersep=5pt,
         tabsize=2,
         extendedchars=true,
         breaklines=true,
         keywordstyle=\color{blue},
         frame=bt,  % <<<<<<<<<<<<<<<<<<<<<<<<<<
         stringstyle=\color{green!40!black}\ttfamily,
         showspaces=false,
         showtabs=false,
         numbers=left,
         xleftmargin=17pt,
         framexleftmargin=17pt,
         framextopmargin=1pt, % <<<<<<<<<<<<<<<<<<<<<<
         showstringspaces=false,
         backgroundcolor=\color[RGB]{200,200,200},
         belowcaptionskip=0pt
}

\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox[RGB]{60,100,180}{\parbox{\textwidth - 2 \fboxsep}{\hspace{14pt}#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white, singlelinecheck=false, margin=0pt, font={bf,footnotesize}}

\begin{document}
\begin{lstlisting}[style=outline,caption=Test]
First line.
Second line.
\end{lstlisting}
\end{document}

框架

編輯:

「黑線」問題的可能解決方案:

  1. 要刪除兩條黑線以使它們“不可見”,您只需rulecolor在清單樣式設定中新增關鍵字即可:

    rulecolor=\color[RGB]{200,200,200},
    
  2. 要實際刪除頂行以使其不列印,但保留底行,您需要保留frame=bt(因為這是間距所必需的),但將以下內容新增至樣式設定後的序言中,以將規則寬度設為0 僅表示“頂部”幀:

    \usepackage{etoolbox}
    \makeatletter
    \patchcmd{\lst@frameh}{\color@begingroup}{\color@begingroup\if#2T\let\lst@framerulewidth\z@ \fi}{}{}
    \makeatother
    

在這兩種解決方案中,我個人更喜歡第一種,因為我認為底部的單條黑線在視覺上沒有那麼吸引人。

相關內容