2

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:

\begin{lstlisting}[frame=tb]
  Code 
  Code 
  Code
\end{lstlisting}

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
3

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.

\documentclass{article}

\usepackage[most]{tcolorbox}

\makeatletter
\newtcblisting[auto counter]{somecode}[2][]{%
  enhanced jigsaw,
  boxrule=0pt,
  toprule=1pt,
  bottomrule=1pt,
  listing options={language={C}},
  listing only,
  colback=white,
  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};
  },
  #1,
}
\makeatother





\begin{document}

\begin{somecode}{Some Text}
int main
\end{somecode}

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

\end{document}

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.