1

With the MWE below (adapted from here), how do I increase the internal margin in the listing so that instead of looking like this:

enter image description here

It looks like this (note the gray background has been "stretched" a bit):

enter image description here

I have tried fiddling with framextopmargin, belowcaptionskip etc but it doesn't seem to work with the internal margin.

\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}
0

1 Answer 1

1

Looks like there needs to be a frame line on the top in order for framextopmargin to have any impact. With the exception of a thin black line between the caption and frame itself, this is exactly what you asked for:

\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}

frame

Edit:

Possible solutions to the "black line" problem:

  1. To remove both black lines so that they are "invisible", you can just add a rulecolor keyword to the listing style setup:

    rulecolor=\color[RGB]{200,200,200},
    
  2. To actually remove the top line so that it doesn't print, but keep the bottom line, you want to keep frame=bt (since this is needed for the spacing), but add the following to the preamble after the style settings to set the rulewidth to 0 for just "Top" frames:

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

Of these two solutions, I would personally prefer the first, as I don't think the single black line on the bottom is that appealing visually.

2
  • Yes, that seems so. Except from the black line between the caption and frame. Is there any way to remove it?
    – johnrl
    Mar 27, 2014 at 7:42
  • @johnrl, see edited answer. The second solution takes care of the single line through a patch to the internal command that draws the frame borders.
    – cslstr
    Mar 27, 2014 at 18:39

You must log in to answer this question.

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