TransWikia.com

Adding Horizontal lines before and after listings frame

TeX - LaTeX Asked on June 22, 2021

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 worldn");
    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

One Answer

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)$)
    -- +(1textwidth, 0);
    draw ($(frame.south west)+(0, 2ex)$)
    -- +(1textwidth, 0);    
  }]
}{endtcolorbox}

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

usepackage{lipsum}

begin{document}
begin{center}largebfseries
  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 worldn");
    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 worldn");
    return 0;
  }
end{lstlisting}

lipsum[4]
end{document}

Correct answer by Daniel N on June 22, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP