I'm trying to get 'labeled' frames on an lstlisting environment that looks like this:

enter image description here

The top and bottom frames are easy are fairly easy to achieve:


But after perusing through the listings documentation, I'm unsure how to achieve the "Some Text" labels on the bottom and top frames (it seems that kind of thing is not supported).

I've looked to alternative packages such as mdframed and fancyvrb. fancyvrb comes close to what I'm looking for, but from what I can by the options only allows the "Some Text" labels to be aligned in the center, i.e.:

enter image description here

Also, one consideration: I already have a nice (and working) lst style definition for my language, so It'd be nice if any solution to this could still using lstlisting (and hence my language definition).

  • 1
    Check the tcolorbox documentation. – Ulrike Fischer Apr 9 '17 at 21:08

A version with tcolorbox and its listings library (which was my first idea to realize this)

The appearance can be changed by styles or by changing the overlay portion of the code.



\newtcblisting[auto counter]{somecode}[2][]{%
  enhanced jigsaw,
  listing options={language={C}},
  listing only,
  sharp corners,
  overlay={\node[inner sep=2pt,xshift=-2cm,fill=tcbcol@back] (A) at (frame.south east) {#2};
           \node[inner sep=2pt,xshift=+2cm,fill=tcbcol@back] (B) at (frame.north west) {#2};


\begin{somecode}{Some Text}
int main

\begin{somecode}[colback=yellow!10!white]{Some Text}
int main


enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.