lstlisting environment with caption on the first line separated with a dashed border

Still on my way to write my own LaTeX class file. My today's purpose is to write an environment similar to the result available here, in the Arch Linux Media Wiki.

My question is deeply linked with the solution proposed to this question.

Imagine an lstlisting environment, with the caption of the lstlisting glued with the environment itself. The lstlisting env and the caption must both have a simple border (frame), the border between the caption and the env is dashed.

The environment and the caption do not have the same font. In my case, the font I use for the lstlisting env is defined as it:

\setmonofont{Bitstream Vera Sans Mono}[Scale=MatchLowercase]


The caption must not be breakable from the environment. Currently, without using tcolorbox, in the example below, if I remove the newpage statement, the caption may be broken (and will be if you put some text after the section title).

I would like to have the line numbers inside the lstlisting environment and ideally, when we copy-paste the text from the lstlisting environment itself, the numbers do not have to be copied.

I still need to be able to use

moredelim={[is][keywordstyle]{@@}{@@}}


as environment parameter to my lstlisting, because I want to be able to specify some statements used to put some text in bold for example. As my LaTeX documents are mainly focused on IT technical documentation, I still want to be able to put an emphasis on some words or characters.

So the idea is to keep to use lstlisting directly and put inside some tcolorbox declarations.

In the future, the purpose is obviously rewrite a wrapper around the different lstlisting I have and have one different for standard files, code files with line numbers, code snipped without line number, configuration files, etc.

Here is my minimum working example I have from now. After having compiled with XeLaTeX, you can see the first to environments are using tcolorbox and the last ones are the 2 examples I tried to make before @Johannes_B (on TeX friends' chat) advised me to use tcolorbox. As you can see, this is deeply inspired from the aforementioned answer.

\documentclass[oneside]{report}
\usepackage{fontspec}
\setmonofont{Bitstream Vera Sans Mono}[Scale=MatchLowercase]
\usepackage[margin=2.5cm]{geometry}
\usepackage{listings}
\usepackage{tcolorbox}
\tcbuselibrary{breakable}
\tcbuselibrary{skins}
\tcbuselibrary{listings}
\usepackage{caption}

\usepackage{lipsum}

\definecolor{captionbox}{cmyk}{0.43, 0.35, 0.35,0.01}

%\tcbset{%
%    colback=captionbox!5!white,%
%    colframe=captionbox!75!black,%
%    top=1mm,%   %% Used to manually align the caption with the horizontal line
%    %
%    %% Create a new "style" for your titled listings tcolorbox
%    mylistingwithtitle/.style = {%
%        breakable,%
%        %% Use tcolorbox's internal tikz object name (frame) to draw a horizontal line
%        overlay unbroken and first={\draw[shorten >=1.4pt, shorten <=1.4pt] ([yshift=-3em]frame.north west) -- ([yshift=-3em]frame.north east);}%
%    }%
%}

\BeforeBeginEnvironment{lstlisting}{%
%    \begin{tcolorbox}[enhanced, toprule=0mm, mylistingwithtitle]%
\begin{tcolorbox} [enhanced, colback=white] %, toprule=0mm, mylistingwithtitle]%
%    \vskip-.5\baselineskip%

\tcblower

}

\AfterEndEnvironment{lstlisting}{\end{tcolorbox}}

\DeclareCaptionFormat{listing}{\parbox{\textwidth}{#1#2#3}}
\captionsetup[lstlisting]{format=listing}

\lstset{
moredelim={[is][keywordstyle]{@@}{@@}},
basicstyle=\normalsize\ttfamily,
breaklines=true,
breakautoindent=false,
breakindent=0pt,
}

\DeclareCaptionFormat{listing}{\rule{\dimexpr\textwidth\relax}{0.4pt}\par\vskip1pt#1#2#3}
\captionsetup[lstlisting]{
format=listing,
singlelinecheck=false,
margin=0pt,
font={sf},
labelsep=space,
labelfont=bf}

\lstnewenvironment{code}[2][]{%
\lstset{%
frame=single,
numbers = left,
caption = #2,
#1,
}%
}{}

\lstnewenvironment{someTest}[1][]{%
\lstset{%
frame=top,
frame=bottom,
#1,
}%
}{}

\begin{document}

%% This following line is only useful to execute \lipsum[1-4] inside the listing
\lstset{numbers=none, escapeinside={(*}{*)}}

\begin{lstlisting}[caption=Sample code block]
This is a code block

@@Hello world@@

(*\lipsum[1-4]*)
\end{lstlisting}

\begin{tcblisting}{colback=red!5!white,colframe=red!25,left=6mm,
listing options={style=tcblatex,numbers=left,numberstyle=\tiny\color{red!75!black}}}
This is a \LaTeX\ example which displays the text as source code
and in compiled form. Additionally, we use line numbers here.
\end{tcblisting}

\newpage
\section{Objective}

\begin{code}{Some great caption}
Hey there, you see the border above is a bit shorter and has 2 borders missing.
\end{code}

\begin{someTest}[caption=Some great caption]
Without vertical border, the caption border is just fine
\end{someTest}

\end{document}

• do you need to allow page breaking within the listing? – David Carlisle Nov 23 '15 at 21:24
• You can define your own tcblisting environment where you can specify all options possible for listings as well -- no need to mix them additionally, in my point of view – user31729 Nov 23 '15 at 21:24
• @DavidCarlisle Yep. Because for now, in my technical documentation, I have several long lstlistings containing whole configuration file content, or some long snipped of code. – wget Nov 23 '15 at 22:09
• @ChristianHupfer I've read the documentation from tcolorbox too. I've seen that ability to define (inherit from) the tcolorbox environment, but when I wanted to specify moredelim={[is][keywordstyle]{@@}{@@}} as a parameter to that environment, this didn't work. I think too, tcolorbox has been designed in a way to avoid mixing both environment, as tcolorbox can use feature from listings directly. If you find a way to achieve that in my use case, let me know. – wget Nov 23 '15 at 22:12
• @wget: I've some familiarity with tcolorbox but I am not aware of any way and feature. I'll try and I do not find another way, I'll leave a note to the package author Thomas Sturm. – user31729 Nov 23 '15 at 22:14

Something like this?

The style is 'very' special, in my point of view!

\documentclass[oneside]{report}
\usepackage{fontspec}
\setmonofont{Bitstream Vera Sans Mono}[Scale=MatchLowercase]
\usepackage[margin=2.5cm]{geometry}
\usepackage{listings}
\usepackage{tcolorbox}
\tcbuselibrary{breakable}
\tcbuselibrary{skins}
\tcbuselibrary{listings}
\usepackage{caption}

\usepackage{lipsum}

\definecolor{captionbox}{cmyk}{0.43, 0.35, 0.35,0.01}

\newtcblisting[auto counter]{wgetlisting}[2][]{%
listing only,
breakable,
top=0.5pt,
bottom=0.5pt,
colback=red!5!white,
colframe=red!25,
left=6mm,
sharp corners,
boxrule=0pt,
bottomrule=1pt,
toprule=1pt,
enhanced jigsaw,
listing options={%style=tcblatex,
numbers=left,
numberstyle=\tiny\color{red!75!black},
moredelim={[is][keywordstyle]{@@}{@@}},
basicstyle=\normalsize\ttfamily,
breaklines=true,
breakautoindent=false,
breakindent=0pt,
escapeinside={(*}{*)},
},%
lefttitle=0pt,
coltitle=black,
colbacktitle=white,
title={Listing \thetcbcounter:  #2},#1%
borderline north={1pt}{14.4pt}{red!25,dashed},
}
%

\begin{document}

\begin{wgetlisting}{Sample code block}
This is a \LaTeX\ example which displays the text as source code
and in compiled form. Additionally, we use line numbers here.

@@Hello world@@
\end{wgetlisting}

\end{document}


• Thanks. Do you have a way to remove the same text in the last part of the lstlisting? I only need the one that is formated (with @@ interpreted). And I checked page breaks are supported \o/ great! – wget Nov 23 '15 at 23:26
• @wget: Ah, ok, I thought you want to have it -- Add the option listings only to the option list in the definition of wgetlisting – user31729 Nov 23 '15 at 23:27
• @wget: Sorry, I meant listing only, not listings only – user31729 Nov 23 '15 at 23:33
• Ok. I began to implement my own version, but I realized I've an issue. Like precised in my question, when I try to copy the text from the lstinline environment, the numbers are copied too. I was hoping your solution did integrate that requirement, but it isn't :-( Do you have any idea to circumvent or fix that issue? – wget Nov 24 '15 at 16:09
• Ok thanks anyway. Btw, I've just seen in the tcolorbox package some examples where numbers are not copied when I try to make a copy-paste. Gonna investigate. And regarding the edit, no problem. – wget Nov 24 '15 at 17:01