0

I have a listing, and I would like to add a hline before and after the listing. Using the frames style parameter do not work as intended for two reasons: 1. I want to keep a left bar as a separator from numbering and the source code; and 2. There is no space between the frame top/bottom line and the left one.

The sample code is

\documentclass{article}
\usepackage{listings}
\usepackage{lipsum}

\lstdefinestyle{CEE}{language=C, frame=l,  numbers=left,  numbersep=1em,  xleftmargin=2em} 

\begin{document}

\lipsum[1]

\begin{lstlisting}[style=CEE, caption={Hello world}]
#include <stdio.h>

int main(int argc, char *argv[])
{
    printf("Hello world\n");
    return 0;
}
\end{lstlisting}

\lipsum[2]
\end{document}

That gives us:

enter image description here

But I wish a \hline that should fill the entire \textwidth, not only the frame width, as in (GIMP Edited)

enter image description here

Pay attention that spacing and line width are not correct since is just a gimp edit. The idea is the same spacing found in table environment. As:

enter image description here

1

enter image description here

I used the package tcolorbox to obtain the lines. You can modify the options of the environment tmpbox to suit your needs. There is one problem though; I don't think it is possible to define a new environment that puts together the tmpbox and the lstlisting. So you have to call tmpbox each time explicitly.

\documentclass[11pt, a4paper]{article}
\usepackage{geometry}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{tcolorbox}
\tcbuselibrary{skins,breakable}

\newenvironment{tmpbox}{%
  \tcolorbox[%
  empty,
  parbox=false,
  noparskip,
  enhanced,
  breakable,
  frame hidden,
  boxrule=0pt,
  colback=white,
  left=-.5ex, % right=-4pt,
  before skip=.1ex plus 2pt,
  after skip=1ex plus 2pt,
  overlay unbroken and last={%
    \draw ($(frame.north west)+(0, -6ex)$)
    -- +(1\textwidth, 0);
    \draw ($(frame.south west)+(0, 2ex)$)
    -- +(1\textwidth, 0);    
  }]
}{\endtcolorbox}

\usepackage{listings}
\lstdefinestyle{CEE}{%
  frame=l, language=C, numbers=left, numbersep=1em, xleftmargin=2em
}

\usepackage{lipsum}

\begin{document}
\begin{center}\large\bfseries
  Adding horizontal lines about listings
\end{center}


% See \verb|https://en.wikibooks.org/wiki/LaTeX/Source_Code_Listings|.

\lipsum[1]

\begin{tmpbox}
  \begin{lstlisting}[style=CEE, caption={Hello world with hlines}]
  #include <stdio.h>
  int main(int argc, char *argv[])
  {
    printf("Hello world\n");
    return 0;
  }
\end{lstlisting}
\end{tmpbox}

\lipsum[3]

\begin{lstlisting}[style=CEE, caption={Hello world}]
  #include <stdio.h>
  int main(int argc, char *argv[])
  {
    printf("Hello world\n");
    return 0;
  }
\end{lstlisting}

\lipsum[4]
\end{document}
2
  • does tcolorbox needs tikz? or is just for tikz-calc ?
    – Lin
    Sep 10 '20 at 15:12
  • TikZ is needed since we use the instruction overlay unbroken and last.
    – Daniel N
    Sep 11 '20 at 12:10

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.